做一个类似地铁里自动售卖机的系统,机器内有一个安卓系统,现在需要通过安卓系统控制机器货道电机转动。机器那边给了下面这样的接口文档。安卓串口通信的相关库android_serialport_api这些已配置好,采用的是SerialPortUtil别人封装好的类。请问一下,文档里最后一个启动电机那里,校验码是怎么设置的啊,随便设吗

解决方案 »

  1.   

    是你发送CRC校验码给机器,机器会回传CRC校验码,不用手动设置,CRC校验码的算法,百度一下有很多的
      

  2.   

    看你的文档,校验码应该是crc校验码,就是把之前的数据用crc算法计算出来的结果。这种问题你只要查一查crc是什么就好了啊。
    剩下的那些图,你直接问硬件商就好了。
    业务太具体了,很难得到确实回复
      

  3.   

    校验码采用CRC算法,算法代码可以到 github 上搜CRC关键字,找java代码下载就可以了
      

  4.   

    CRC算法下载链接 https://download.csdn.net/download/alphen/11853719
      

  5.   

    这么简单的东西,看文档就知道是这个是CRC16校验算法,CRC16 就是两个字节的,人家都给例子,自己用手算一下不就知道了吗,
    不要听那些说话不负责任的,随便乱写,人家定义了这个协议,定义了校验码,就是用于通讯过程中,为了避免数据传输过程中出错,才需要的。
      

  6.   

    CRC16  是有很多种,有IBM 的、有USB的、有MODBUS 的、有DNP 的 等等。
    算了,告诉你你这个算法的具体是 CRC16 MODBUS模式的。
      

  7.   


    算了,今天太闲了,帮你找了几个算法java源码,自己与验证下
    https://www.cnblogs.com/lujiannt/p/9246256.html
    https://www.cnblogs.com/koneloong/p/5218136.html
    https://segmentfault.com/a/1190000019393677?utm_source=tag-newest
    https://github.com/jichengyue/ModbusCRC16
      

  8.   

    新手刚接触安卓串口,现在发送数据遇到了问题,发送了没反应。请问一下要以16进制发送“0x010xF00xF0”,要怎么转换啊。
    byte[] mBuffer = new byte[3];        Arrays.fill(mBuffer, (byte) 0x01);
            Arrays.fill(mBuffer, (byte) 0xF0);
            Arrays.fill(mBuffer, (byte) 0xF0);我是这么转的,但是发送了机器没有回应
      

  9.   

    感觉最后一个链接上是github的 靠谱点这么简单, /**
     *  strings to Hexadecimal array,seperate string by space  
     * @param strHexValue   Hexadecimal string
     * @return byte array
     */
    public static byte[] stringToByteArray(String strHexValue) {
    String[] strAryHex = strHexValue.split(" ");
            byte[] btAryHex = new byte[strAryHex.length];        try {
    int nIndex = 0;
    for (String strTemp : strAryHex) {
        btAryHex[nIndex] = (byte) Integer.parseInt(strTemp, 16);
        nIndex++;
    }
            } catch (NumberFormatException e) {        }        return btAryHex;
        }
      

  10.   

    发送指令首先要算出crc啊。  import java.util.Arrays;
    public class hello {
        public static int getCrc(byte[] crcb) {
            int crc = 0xFFFF;
            byte Len;
            boolean flag;
            for (byte item : crcb) {
                crc ^= ((int) item & 0x00FF);
                Len = 8;
                while (Len > 0) {
                    flag = (crc & 1) == 1;
                    crc >>= 1;
                    if (flag) {
                        crc ^= 0xA001;
                    }
                    Len--;
                }
            }
            return crc;
        }
        private static String GetHexStr(byte[] crcb) {
            int crci=getCrc(crcb);
            String crcStr = Integer.toHexString(crci).toUpperCase();
            int size = 4 - crcStr.length();
            StringBuilder builder = new StringBuilder();
            while (size > 0) {
                builder.append("0");
                size--;
            }
            return builder.append(crcStr).toString();
        }
        public static void main(String args[]) {
            byte test1[]= {0x02,0x05,0x00}; //测试发送crc
            byte test2[]= {0x00,0x05,0x00}; //测试接收crc        String str1=GetHexStr(test1); //调试输出:50D3
            String str2=GetHexStr(test2); //调试输出:9072        System.out.println("hex:" + str1); //所以机器是先低位后高位
            System.out.println("hex:" + str2);        byte[] send = new byte[5];//定义byte发送数组        send[0]=(byte)0x01; //定义发送指令
            send[1]=(byte)0xF0;
            send[2]=(byte)0xF0;        byte check[]={send[0],send[1],send[2]};
            String sendcrc=GetHexStr(check); //获取指令crc        String hexL=sendcrc.substring(2,4); //取crc低位
            String hexH=sendcrc.substring(0,2); //取crc高位        send[3]=(byte)Integer.parseInt(hexL, 16); //填入crc低位
            send[4]=(byte)Integer.parseInt(hexH, 16); //填入crc高位        System.out.println("send:" + Arrays.toString(send)); 
            /* 调试输出: send: [1, -16, -16, 100, 68]
               为什么会有负数?因为byte类型取值范围为:-128~127;
               所以在转换为byte类型时,如果数值超出范围就会自动转换
               在这个取值范围内。
            */
            int Receive= -16 & 0xff; //若接收到负数怎么办,先转换再取十六进制;
            System.out.println(Integer.toHexString(Receive).toUpperCase());
            // 调试输出: F0
      

  11.   


    CRC16 很多种模式的,他用的是MODBUS的,你这种是吗?
      

  12.   

    你不认真看代码的吗?我测试结果都出来了。还问是不是“MODBUS”
      

  13.   

    这个我前段时间写过, 把要发送的字节进行crc转换,发给另一端,另一端会比较,然后返回成功或失败。
      

  14.   

    这个我前段时间写过, 把要发送的字节进行crc转换,再拼一起发给另一端,另一端会比较,然后返回成功或失败。