最近在做android手机蓝牙和单片机通信,一直被接收数据完整性这个问题困扰。
为了使一次通信的包保持完整,我就构思了发送12位的包,包头(1)+数据(8)+校验(2)+包尾(1),校验为求和校验。单片机发送数据的函数如下
void SendString(uchar *dat) //发送12位的包 包头(1)+数据(8)+校验(2)+包尾(1)
{
uchar i,j;
uint check=0; //求和校验
SendChar(0x01); //0x01 SOH --> start of heading
i=strlen(dat);
for(j=0;j<i;j++) //发送数据
{
SendChar(dat[j]);
check+=dat[j];
}
for(j=8-i;j>0;j--)
{
SendChar(0x20); //用0x20 space 补齐未满数据位
check+=0x20;
}
SendChar((uchar)(check>>8)); //校验位高位
SendChar((uchar)(check&0x00ff)); //校验位低位
SendChar(0x04); //0x04 EOT --> end of transmission
}
这段是单片机间隔一段时间发送包,已在电脑端串口助手里测试过了,发送正常
while(1)
{
delay_ms(1000);
SendString("test1234");
}
然后接收包的java代码
public void run()
{
Log.i(TAG, "BEGIN mConnectedThread");
StringBuffer buffer = new StringBuffer();
int SumCheck = 0, SumRec;
int i = 0, j, dat;
while (true)
{
try
{
int count = mmInStream.available();
while (count < 12)
{
count = mmInStream.available();
}
int SOH = mmInStream.read();
Log.i(TAG, "SOH=" + SOH);
while (SOH != 1)
{
i++;
if (i >10)
{
break;
}
SOH = mmInStream.read();
Log.i(TAG, "SOH=" + SOH);
}
count = mmInStream.available();
if ((i < 11) && (count <=11))
{
while (count < 11)
{
count = mmInStream.available();
}
} else
{
continue;
}
for (j = 0; j < 8; j++)
{
dat = mmInStream.read();
buffer.append(dat);
SumCheck += dat;
}
SumRec = mmInStream.read();
SumRec = (SumRec << 8) | (mmInStream.read());
Log.i(TAG, "SumCheck=" + SumCheck + "\nSumRec" + SumRec);
if ((SumCheck == SumRec) && (mmInStream.read() == 4))
{
String s = buffer.toString();
byte[] bits = s.getBytes("UTF-8");
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, 8, -1, bits).sendToTarget(); // 送入消息池
Log.i(TAG, "buffer=" + s);
}
buffer.delete(0, 7);
SumCheck = 0;
i = 0;
} catch (IOException e)
{
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
(其他部分的代码参考了android example API-9 bluetoothchat)
但是这段按照包头(1)+数据(8)+校验(2)+包尾(1)来接受校验数据,运行不正常
logcat输出如下
05-16 23:31:21.360: D/BluetoothChatService(17686): create ConnectedThread
05-16 23:31:21.360: I/BluetoothChatService(17686): END mAcceptThread
05-16 23:31:21.400: D/BluetoothChatService(17686): setState() 2 -> 3
05-16 23:31:21.400: I/BluetoothChatService(17686): BEGIN mConnectedThread
05-16 23:31:21.410: I/BluetoothChat(17686): MESSAGE_STATE_CHANGE: 3
05-16 23:31:21.480: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.500: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.500: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.500: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=51
logcat到此停了。。。内容可能有些多,但跪求各位帮忙看看,问题出在哪儿