当前位置: 首页 > news >正文

电子商务网站建设基础步骤章丘网站开发

电子商务网站建设基础步骤,章丘网站开发,住房和城乡建设部网站主页,反向代理服务器做wordpress外网粘包解决方法 方法1 getXOR——是校验方法 /*** 最小数据包的长度(除开数据的N个字节#xff09;* 帧头 保留字节 协议控制字 地址字段 命令长度 命令码 命令数据 校验和* 2字节 3字节 1字节 2或8字节 2字节 2字节 0-1100字…粘包解决方法 方法1 getXOR——是校验方法 /*** 最小数据包的长度(除开数据的N个字节* 帧头 保留字节 协议控制字 地址字段 命令长度 命令码 命令数据 校验和* 2字节 3字节 1字节 2或8字节 2字节 2字节 0-1100字节 2字节* SYN RES PTROL ADDR SLEN COMMAND APPDATA CRC16*/ int MIN_PACK_LEN 2 3 1 2 2 2 2; /*** 数据0-N字节N小于等于250*/ int SLEN 1100; int MAX_DATA_N 255; private final ByteBuffer mByteBuffer;public SerialReadThread(InputStream is) {mInputStream new BufferedInputStream(is);mByteBuffer ByteBuffer.allocate(1024);mByteBuffer.clear(); } private boolean isValid(byte[] recvCheck, byte[] myCheck) {//LgqLogutil.d(rr MyByteUtil.bytes2HexStr(recvCheck),,,mMyByteUtil.bytes2HexStr(myCheck));for (int i 0, n recvCheck.length; i n; i) {if (recvCheck[i] ! myCheck[i]) {return false;}}return true; } /*** 处理获取到的数据** param received* param size*/ private void onDataReceive(byte[] received, int size) {// TODO: 2018/3/22 解决粘包、分包等try {//LogPlus.e(Receiver, 接收数据 ByteUtil.bytes2HexStr(bytes, offset, length));byte b;int readable;mByteBuffer.put(received, 0, size);mByteBuffer.flip();out:while ((readable mByteBuffer.remaining()) Protocol.MIN_PACK_LEN) { /*只处理数据帧, 不处理响应帧*/mByteBuffer.mark(); // 标记一下开始的位置int frameStart mByteBuffer.position();byte[] frameHead Protocol.FRAME_HEAD;for (byte h : frameHead) {b mByteBuffer.get();if (b ! h) { // 不满足帧头就跳到第二位重新开始mByteBuffer.position(frameStart 1);continue out;}}byte[] dataLengthBytes new byte[] { mByteBuffer.get(8), mByteBuffer.get(9) };//第9个字节 为 数据长度.两个字节所以是8,9int dataLen (int) ByteUtil.bytes2long(dataLengthBytes, 0, 2); /*dataLen已经包含了*/if (dataLen Protocol.MAX_DATA_N) {// 如果data n超过最大允许长度表示数据错乱了//则回到“第二位”继续找到下一个 帧头mByteBuffer.position(frameStart 2);continue;}int total Protocol.MIN_PACK_LEN dataLen - 2; /*-2 最小数据包长度中已经包含, dataLen中有最小数据包长度中的2个字节*/// 如果可读数据小于总数据长度表示不够,还有数据没接收if (readable total) {// 重置一下要处理的位置,并跳出循环mByteBuffer.reset();break;}mByteBuffer.reset();// 拿到整个包byte[] allPack new byte[total];mByteBuffer.get(allPack);byte[] recvCheck new byte[2];recvCheck[0] allPack[allPack.length - 2];recvCheck[1] allPack[allPack.length - 1];byte[] myCheck ByteUtil.getXOR(allPack, allPack.length - 2);if (isValid(recvCheck, myCheck)) {// 校验通过/**/LgqLogutil.d(#####校验通过,allPack: ByteUtil.bytes2HexStr(allPack));//validData.add(allPack);LogManager.instance().post(new RecvMessage(ByteUtil.bytes2HexStr(allPack)));} else {mByteBuffer.position(frameStart 2);}}} catch (Exception e) {e.printStackTrace();} finally {// 最后清掉之前处理过的不合适的数据mByteBuffer.compact();}//String hexStr ByteUtil.bytes2HexStr(received, 0, size);//String hexStr ByteUtil.bytes2HexStr(allPack, 0, size);//LogManager.instance().post(new RecvMessage(hexStr)); } 粘包解决方法2 try {//LogPlus.e(Receiver, 接收数据 ByteUtil.bytes2HexStr(bytes, offset, length));byte b;int readable;mByteBuffer.put(bytes, 0, length);mByteBuffer.flip();out:while ((readable mByteBuffer.remaining()) Protocol.MIN_PACK_LEN) { /*只处理数据帧, 不处理响应帧*/mByteBuffer.mark(); // 标记一下开始的位置int frameStart mByteBuffer.position();byte[] frameHead Protocol.FRAME_HEAD;for (byte h : frameHead) {b mByteBuffer.get();if (b ! h) { // 不满足帧头就跳到第二位重新开始mByteBuffer.position(frameStart 1);continue out;}}byte[] dataLengthBytes new byte[]{mByteBuffer.get(4)};//第5个字节 为 数据长度int dataLen (int) ByteUtil.bytes2long(dataLengthBytes, 0, 1); /*dataLen已经包含了*/if (dataLen Protocol.MAX_DATA_N) {// 如果data n超过最大允许长度表示数据错乱了//则回到“第二位”继续找到下一个 帧头mByteBuffer.position(frameStart 2);continue;}int total Protocol.MIN_PACK_LEN dataLen - 2; /*-2 最小数据包长度中已经包含, dataLen中有最小数据包长度中的2个字节*/// 如果可读数据小于总数据长度表示不够,还有数据没接收if (readable total) {// 重置一下要处理的位置,并跳出循环mByteBuffer.reset();break;}mByteBuffer.reset();// 拿到整个包byte[] allPack new byte[total];mByteBuffer.get(allPack);byte recvCheck allPack[allPack.length - 1];byte myCheck ByteUtil.getXOR(allPack, 0, allPack.length - 1);if (myCheck recvCheck) { // 校验通过/**/LogPlus.d(#####校验通过,allPack: ByteUtil.bytes2HexStr(allPack));validData.add(allPack);} else {mByteBuffer.position(frameStart 2);}} } catch (Exception e) {e.printStackTrace(); } finally {// 最后清掉之前处理过的不合适的数据mByteBuffer.compact(); } /*** 字节数组转换成对应的16进制表示的字符串** param src* return*/ public static String bytes2HexStr(byte[] src) {StringBuilder builder new StringBuilder();if (src null || src.length 0) {return ;}char[] buffer new char[2];for (int i 0; i src.length; i) {buffer[0] Character.forDigit((src[i] 4) 0x0F, 16);buffer[1] Character.forDigit(src[i] 0x0F, 16);builder.append(buffer);}return builder.toString().toUpperCase(); } 1、心跳数据强转为 byte[] 数据类型 Override public RecvCommand adaptReceive(byte[] allPack, Object... other) {int cmd (int) other[0];LogPlus.e( 收到的命令: cmd);byte[] data (byte[]) other[1]; 2、开始解析——必须根据解析协议才能解析数据 public class RecvA3Status extends BaseRecvCommand {private final int mResult;private final float mPowerFactorState;public ListAuxiliaryPlate auxiliaryPlates;public RecvA3Status(byte[] allPack, byte[] data) {super(allPack, data);mResult 0xff data[0];mPowerFactorState ByteUtil.getFloat(data, 25);auxiliaryPlates new ArrayList();for (int i 0; i (data.length - 29) / 107; i) {byte[] bytes new byte[107];System.arraycopy(data, 29 i * 107, bytes, 0, bytes.length);if (i0){LogPlus.e(几号门i, 门状态11接收数据2仓门 ByteUtil.bytes2HexStr(bytes));}else {LogPlus.i(几号门i, 门状态11接收数据2仓门 ByteUtil.bytes2HexStr(bytes));}auxiliaryPlates.add(new AuxiliaryPlate(bytes));}}/*** 仓控板状态表*/public static class AuxiliaryPlate {private int onlineState;private int doorState;private int batteryState;private int fireState;private int urgentState;private int temperature;private int batteryVoltage;private int chargerOnlineState;private ChargerState chargerState;private OnOffAmount onOffAmount;private AnalogQuantity analogQuantity;private int BMSOnlineState;private String BMSID;/**type 0 正常电池样式 1 显示屏样式 */public int type 0;public AuxiliaryPlate(byte[] bytes) {onlineState 0xff bytes[0];doorState 0xff bytes[1];batteryState 0xff bytes[2];fireState 0xff bytes[3];urgentState 0xff bytes[4];temperature 0xff bytes[5];batteryVoltage ByteUtil.bytes2long(bytes, 6, 2) / 100;chargerOnlineState 0xff bytes[8];byte[] bytes1 new byte[6];System.arraycopy(bytes, 9, bytes1, 0, bytes1.length);chargerState new ChargerState(bytes1);BMSOnlineState 0xff bytes[15];byte[] bytes2 new byte[26];System.arraycopy(bytes, 16, bytes2, 0, bytes2.length);BMSID new String(bytes2, StandardCharsets.US_ASCII).trim();byte[] bytes3 new byte[7];System.arraycopy(bytes, 42, bytes3, 0, bytes3.length);onOffAmount new OnOffAmount(bytes3);byte[] bytes4 new byte[58];System.arraycopy(bytes, 49, bytes4, 0, bytes4.length);analogQuantity new AnalogQuantity(bytes4);} 付解析协议 实现demohttps://download.csdn.net/download/meixi_android/12707609  bug在线交流 QQ1085220040
http://www.pierceye.com/news/4251/

相关文章:

  • 单页面网站推广方法企业网站策划论文
  • 建设网站需要多长时间网页设计与制作基础
  • 深圳网站建设服务商哪些好?wordpress 地址栏
  • 个人网站做企业备案吗图像生成器在线制作
  • 网站网站做任务佣金违法电子商务网站开发软件
  • 为什么做网站需要服务器宝安区网络公司
  • 广元建设网站要多少钱智慧团建网站注册
  • 购物网站开发小结wordpress 添加地图
  • 网站运营 网站建设免费的黄页推广软件哪个好
  • 厦门网站建设方案书怎样做一个简单的网站
  • 百度 网站添加wordpress直达链接404
  • 网站备案需要多少天许昌seo推荐
  • 网站建设主要有哪些成本阿树wordpress
  • 手机网站 好处佛山专业的网页制作
  • 做好公众号 网站建设网上注册公司价格
  • 网站维护服务器信阳做网站的公司
  • 石柱网站制作网页qq空间登录
  • 重慶网站建设微网站 域名
  • 网站类型有wordpress实现双语
  • 查建设公司年度保证金网站咨询公司组织架构
  • 网站创建后台做网站的服务器很卡怎么办
  • 杭州做产地证去哪个网站深圳招聘网站前十排名
  • 陕西企业网站建设价格帝国cms 网站地图 xml
  • 北京网站建设公司兴田德润电话wordpress页面的设置
  • 云南网站设计平台长沙网络公司推广
  • 网页设计教程网站c 小说网站开发教程
  • 网站收录排名怎么做项目建设环境影响登记表在哪个网站
  • 贵阳网站开发哪家便宜河北搜索引擎优化
  • 如何做网站 seo苏州建筑业网
  • 成都响应式网站开发免费个人网站怎么做