一、有关串口通信基本知识
- 有关波特率计算(参考链接)
第一个字节的10位(1位起始位-0,8位数据位和1位停止位-1)共占约1.05ms,这样可计算出其波特率约为:10bit / 1.05ms X 1000 ≈ 9600 bit/s;
- 有关数据的传输顺序 是LSB先出的;(参考链接)
第一个数据 01010101 反过来就是10101010 =0xAA
第二个10101010 反过来01010101 0x55;
案例:
11 23 ---2020-6-25 10:55 byte发送;
int command[]=new int[]{0x01,0x03,0x00,0x01,0x00,0x04,0x15,0xC9};
0 1000 0000 1 * 0 1100 0000 1 * 0 0000 0000 1 *
01 03 00
0 100 0000 1 * 0 0000 0000 1* 0 0010 0000 1*
01 00 04
0 1010 1000 1* 0 1001 0011 1*
15 C9

二、代码讲解
对于倾角模块Bwh527,每发送一次查询数据,串口才能接收到一次数据;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115//在js中代码 this.printLog('开发串口发送数据'); Utils.sendAngleCommandProductBwh527Data(); Utils.sendAngleCommandProductListener(); //倾角 setTimeout(()=>{ Utils.sendAngleCommandProductBwh527Data(); },100); //在RCTUtilsModule文件中代码; @ReactMethod public void sendAngleCommandProductBwh527Data(){ //发送数据 byte command[]=new byte[]{0x77,0x04,0x00,0x04,0x08}; serialPortUtils.sendSerialPortByte(command); } final int adressAngleRs485=0x00; //本地485地址; @ReactMethod public void sendAngleCommandProductListener(){ //开监听器; serialPortUtils.setOnDataReceiveListener(new SerialPortUtils.OnDataReceiveListener() { @Override public void onDataReceive(byte[] buffer, int size) { Log.d(TAG, "进入数据监听事件中" + changeTool.ByteArrToHex(buffer)); StringBuffer sBuffer = new StringBuffer(); //适用于BWH526--圆形串口TTL输出 if((buffer[2]==adressAngleRs485)){ if((buffer[0]==0x77)&&(buffer[1]==0x10)){ switch (buffer[3]){ case (byte)0x84: { //x轴角度 sBuffer.append(changeTool.Byte2Hex(buffer[4])); sBuffer.append(changeTool.Byte2Hex(buffer[5])); sBuffer.append(changeTool.Byte2Hex(buffer[6])); sBuffer.append(changeTool.Byte2Hex(buffer[7])); if((buffer[4]&0x10)==0x10){ sBuffer.setCharAt(0,'-'); }else{ sBuffer.setCharAt(0,' '); } sBuffer.insert(4,'.'); Log.d(TAG, "倾角:x轴角度:" + sBuffer); //y轴角度 sBuffer.delete(0,sBuffer.length()); sBuffer.append(changeTool.Byte2Hex(buffer[8])); sBuffer.append(changeTool.Byte2Hex(buffer[9])); sBuffer.append(changeTool.Byte2Hex(buffer[10])); sBuffer.append(changeTool.Byte2Hex(buffer[11])); if((buffer[8]&0x10)==0x10){ sBuffer.setCharAt(0,'-'); }else{ sBuffer.setCharAt(0,' '); } sBuffer.insert(4,'.'); Log.d(TAG, "倾角:y轴角度:" + sBuffer); sBuffer.delete(0,sBuffer.length()); break; } default: ; } } } //适用于BWM427--RS485输出 if((buffer[0]==adressAngleRs485)&&(buffer[1]==0x03)){ if((buffer[2]==0x04)){ int temp =(buffer[3]<<8)|buffer[4]; float tempx=(temp-10000)/100; sBuffer.append(tempx); Log.d(TAG, "倾角:x轴角度:" +sBuffer); sBuffer.delete(0,sBuffer.length()); temp =(buffer[5]<<8)|buffer[6]; float tempy=(temp-10000)/100; sBuffer.append(tempy); Log.d(TAG, "倾角:x轴角度:" + sBuffer); sBuffer.delete(0,sBuffer.length()); } } } }); } //字节数组转转hex字符串 //------------------------------------------------------- //1字节转2个Hex字符 public static String Byte2Hex(Byte inByte) { return String.format("%02x", new Object[]{inByte}).toUpperCase(); } //------------------------------------------------------- //字节数组转转hex字符串 public static String ByteArrToHex(byte[] inBytArr) { StringBuilder strBuilder = new StringBuilder(); for (byte valueOf : inBytArr) { strBuilder.append(Byte2Hex(Byte.valueOf(valueOf))); strBuilder.append(" "); } return strBuilder.toString(); }
最后
以上就是专一世界最近收集整理的关于安卓串口调试记录(包括串口通信底层知识点)的全部内容,更多相关安卓串口调试记录(包括串口通信底层知识点)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复