安卓串口开发 做一个类似地铁里自动售卖机的系统,机器内有一个安卓系统,现在需要通过安卓系统控制机器货道电机转动。机器那边给了下面这样的接口文档。安卓串口通信的相关库android_serialport_api这些已配置好,采用的是SerialPortUtil别人封装好的类。请问一下,文档里最后一个启动电机那里,校验码是怎么设置的啊,随便设吗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是你发送CRC校验码给机器,机器会回传CRC校验码,不用手动设置,CRC校验码的算法,百度一下有很多的 看你的文档,校验码应该是crc校验码,就是把之前的数据用crc算法计算出来的结果。这种问题你只要查一查crc是什么就好了啊。剩下的那些图,你直接问硬件商就好了。业务太具体了,很难得到确实回复 校验码采用CRC算法,算法代码可以到 github 上搜CRC关键字,找java代码下载就可以了 CRC算法下载链接 https://download.csdn.net/download/alphen/11853719 这么简单的东西,看文档就知道是这个是CRC16校验算法,CRC16 就是两个字节的,人家都给例子,自己用手算一下不就知道了吗,不要听那些说话不负责任的,随便乱写,人家定义了这个协议,定义了校验码,就是用于通讯过程中,为了避免数据传输过程中出错,才需要的。 CRC16 是有很多种,有IBM 的、有USB的、有MODBUS 的、有DNP 的 等等。算了,告诉你你这个算法的具体是 CRC16 MODBUS模式的。 算了,今天太闲了,帮你找了几个算法java源码,自己与验证下https://www.cnblogs.com/lujiannt/p/9246256.htmlhttps://www.cnblogs.com/koneloong/p/5218136.htmlhttps://segmentfault.com/a/1190000019393677?utm_source=tag-newesthttps://github.com/jichengyue/ModbusCRC16 新手刚接触安卓串口,现在发送数据遇到了问题,发送了没反应。请问一下要以16进制发送“0x010xF00xF0”,要怎么转换啊。byte[] mBuffer = new byte[3]; Arrays.fill(mBuffer, (byte) 0x01); Arrays.fill(mBuffer, (byte) 0xF0); Arrays.fill(mBuffer, (byte) 0xF0);我是这么转的,但是发送了机器没有回应 感觉最后一个链接上是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; } 发送指令首先要算出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 CRC16 很多种模式的,他用的是MODBUS的,你这种是吗? 你不认真看代码的吗?我测试结果都出来了。还问是不是“MODBUS” 这个我前段时间写过, 把要发送的字节进行crc转换,发给另一端,另一端会比较,然后返回成功或失败。 这个我前段时间写过, 把要发送的字节进行crc转换,再拼一起发给另一端,另一端会比较,然后返回成功或失败。 不能自动写入R.java 【60分】ViewHolder模式为啥要用static类? android开发的几个问题 请教GridView的一个问题 类似wifi设置页面的实现 如何设置appwidget的高度 懂GUI的MY欢迎指教, DPI 问题 [Android]如何修改源代码,去掉系统通讯功能? 好基友勿相忘,关于云上的日子 关于Eclipse for android的问题 如何延长中国移动长连接维持时间 View.OnClickListener()被禁用,怎么办
剩下的那些图,你直接问硬件商就好了。
业务太具体了,很难得到确实回复
不要听那些说话不负责任的,随便乱写,人家定义了这个协议,定义了校验码,就是用于通讯过程中,为了避免数据传输过程中出错,才需要的。
算了,告诉你你这个算法的具体是 CRC16 MODBUS模式的。
算了,今天太闲了,帮你找了几个算法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
byte[] mBuffer = new byte[3]; Arrays.fill(mBuffer, (byte) 0x01);
Arrays.fill(mBuffer, (byte) 0xF0);
Arrays.fill(mBuffer, (byte) 0xF0);我是这么转的,但是发送了机器没有回应
* 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;
}
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
CRC16 很多种模式的,他用的是MODBUS的,你这种是吗?