接收串口数据,ACCESS里显示不正确
电脑接收从单片机串口来的数据,uchar code Buffer[] = "1111";//所要发送的数据,
我的接收程序是:(为什么我在ACCESS相应的字段里显示不出正确的数据:显示为F5 A1 96 7C 请老师指点 谢谢)
const BufSize = 4096;
unsigned char Buffer[BufSize];
AnsiString XH,XM,SX,YW;
YbCommDevice1->ReadPackage(Buffer,BufSize);
XH=IntToHex(Buffer[0],2);
XM=IntToHex(Buffer[1],2);
SX=IntToHex(Buffer[2],2);
YW=IntToHex(Buffer[3],2);
AnsiString sql;
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
sql="Insert Into XBBM(XH,XM,SX,YW) Values('"+XH+"' ,'"+XM+"','"+SX+"','"+YW+"')";
ADOQuery1->SQL->Add(sql);
ADOQuery1-> ExecSQL();
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
sql="Select * from XBBM";
ADOQuery1->SQL->Add(sql);
ADOQuery1->Open();
单片机发送程序如下:
uchar i = 0;
uchar code Buffer[] = "1111"; //所要发送的数据
uchar *p,num;
p = Buffer;
num=strlen(Buffer);
SBUF = *p;
while(1)
{
while(!TI) //如果发送完毕,硬件会置位TI
{
nop_();
}
p++;
i++;
TI = 0;//TI清零
if(i==num)
{
i=0;
p = Buffer;
TR1=0;
}
}
[解决办法]
1.用串口监视器看看是否有数据接收到
2看你程序是否收到了数据
3一般用memcpy复制数据到XH等
[解决办法]
YbCommDevice1->ReadPackage(Buffer,BufSize);
=>
BufSize = YbCommDevice1->ReadPackage(Buffer,BufSize);
然后判断BufSize!
uchar *p,num;
=>
uchar code *p;
size_t num;
[解决办法]
单步调试下