上篇文章搞定了虚拟摇杆以及左右轮速度算法,接下来就是连接硬件以及向硬件提交信息
获得串口通信串儿
(1)串口通讯
(2)通讯规则:
1、协议格式:(8字节)
[startbyte] [data1][data2]…[data22][flags][endbyte]
startbyte=0x0f;
endbyte=0x00;
flags标志位我没有用到;
data1…data22:对应16个通道(ch1-ch16),每个通道11bit
2、需要的是data数据 22*8位
[startbyte] [data1] [data2] … [data22] [flags][endbyte]
startbyte = 11110000 b (0xF0)
//228==1611
[data1] [data2] … [data22] = [ch1,11bit] [ch2,11bit] … [ch16,11bit](ch#= 0 bis )
需要16个通道 规定ch1=左轮速度,ch2=右轮速度,ch5=武器速度
[ch1,11bit] [ch2,11bit] … [ch16,11bit](ch#= 0 bis )
通道为11位二进制数据
例如1200对应的二进制通道数据为10010110000
获取对应的data串儿代码
public static void main(code1Int:Int,code2Int:Int,code5Int:Int) throws IOException {
String code = "00000000000";
String code1 = Integer.toBinaryString(code1Int); //通道1--二进制
String code2 = Integer.toBinaryString(code2Int); //通道2
String code5 = Integer.toBinaryString(code5Int); //通道5
System.out.println(code1);
System.out.println(code2);
System.out.println(code5);
Integer i = 0;
String io = code1 + code2;
while(i < 14){
i++;
if(i.equals(2)){
io += code5;
continue;
}
io += code;
}
System.out.println(io);
i = 0;
Integer beginIndex = 0;
String[] str = new String[22];
while(i < 22){
Integer endIndex = beginIndex + 8;
str[i] = io.substring(beginIndex, endIndex);
beginIndex = endIndex;
i++;
}
System.out.println(Arrays.toString(str));
String endString = "";
for(String c : str){
Integer six = Integer.parseInt(c, 2);
String ipi = Integer.toHexString(six);
if(ipi.equals("0")){
ipi = "00";
}
endString += ipi;
System.out.println(ipi);
}
endString = "0f" + endString + "0000";
System.out.println(endString);
Log.i("App_Tcp==", "endString=" + endString);
}
== endString==
0fa95fcea80035f424000003f310f0540000000000000000000000000000000000
通信
由于项目要求实时性比较高,所以采取App直接连接硬件的方案
采用Socket直接连接IP和端口号
实现方法:
安卓Socket的使用