串口发送双精度数组问题?
下位机采集到的数据需要传送到上位机处理;下位机一次采集1万个双精度数据,需要1次传输到上位机;我用CserialPort试写了个串口传送程序,如下:
1.发送程序:
void CSerialPortTestDlg::OnButtonSend()
{
if (!m_bSerialPortOpened) return;
double a[20],b[20];
a[0]=1.125;
a[1]=2.225;
a[2]=3.223;
a[3]=4.223;
a[4]=5.223;
a[5]=6.125;
a[6]=7.225;
a[7]=8.223;
a[8]=9.223;
a[9]=10.223;
a[10]=11.125;
a[11]=12.225;
a[12]=13.223;
a[13]=14.223;
a[14]=15.223;
a[15]=16.125;
a[16]=17.225;
a[17]=18.223;
a[18]=19.223;
a[19]=20.223;
int len=20*sizeof(double);
int len1=len+2;
char * byte=new char[len1];
memset(byte,0,len1);
memcpy(&byte[1],a,len);
byte[0]='$';
byte[len1-1]='*';
memcpy(b,&byte[1],len);
m_SerialPort.WriteToPort((char*)byte,len1);
}
2.接收程序:
LONG CSerialPortTestDlg::OnComm(WPARAM ch, LPARAM port)
{
static char buf[162];
static int count=0;
switch(ch)
{
case '$':
count=0;
buf[count]=(char)ch;
break;
case '*':
{
int len=20*sizeof(double);
double b[20];
memcpy(b,&buf[1],len);
display();
}
break;
default:
count++;
buf[count]=(char)ch;
break;
}
return 0;
}
现在的问题是:
1.发送程序一次发送数据小于10个double时,接收正确;
2.发送程序一次发送数据大于10个double时,接收不正确;
3.发送程序一次发送数据20个double时,接收不正确,并且messagebox跳出来两次;
求求各位高手给点建议,多谢!!!!!!!!!!!