问题描述
大家好.
我这里有一个小问题.
我想做的是在串行端口上发送数据.我已经写了必要的代码,但是出现了调试错误.
错误文本是这个..
我写的代码如下:
Hey everyone.
I have a little problem here.
What I''m trying to do is sending data on serial port. I''ve written the necessary codes, but there appears a debug error.
The error text is this..
The Code that I wrote is below:
char a = 1;
printf("Press a key: \n");
scanf("%d", &a) ;
switch(a)
{
case 1:
{
char data1[8] = "___id:";
int size1 = 8;
ser.write(data1,size1);
break;
}
case 2:
{
char data2[8] = "___sr:";
int size2 = 8;
ser.write(data2,size2);
break;
}
case 3:
{
char data3[8] = "___np:";
int size3 = 8;
ser.write(data3,size3);
break;
}
case 4:
{
char data4[8] = "_cocn:";
int size4 = 8;
ser.write(data4,size4);
break;
}
}
通过串口写的代码如下:
The Code for write through serial port is given below:
int SerPort::write(const char *data, unsigned int size)
{
unsigned long ret;
if (myPort != INVALID_HANDLE_VALUE && myStatus == STATUS_OPEN)
{
if (!WriteFile(myPort, data, size, &ret, NULL))
{
printf("SerPort::write: Error on writing.\n");
return -1;
}
return ret;
}
printf("SerPort::write: Connection invalid.\n");
return -1;
}
有谁有主意吗?
我最好的问候...
Does anyone has an idea?
My best Regards...
推荐答案
int a = 1;
printf("Press a key: \n");
scanf("%d", &a) ;
//...
char data1[8] = "___id:";
int size1 = 8;
ser.write(data1,size1);
数据[0] =``_'';
数据[1] =``_'';
数据[2] =``_'';
数据[3] =``i'';
数据[4] =``d'';
data [5] ='';'';
数据[6] =``\ 0''; (空)
data [7] =(统一/剩余的垃圾字符)
然后您盲目地从串行端口发送8个字符.我无法告诉您正确的做法是什么或如何重新编写代码,因为我不知道在串行线上读取数据的其他程序/设备"期望什么,但我愿意打赌它不希望垃圾".
data[0] = ''_'';
data[1] = ''_'';
data[2] = ''_'';
data[3] = ''i'';
data[4] = ''d'';
data[5] = '';'';
data[6] = ''\0''; (null)
data[7] = (unitialized / leftover junk character)
and you blindly send 8 characters out the serial port. I can''t tell you what the right thing to do is or how to re-write your code because I do not know what the other "program / device" reading the data on the serial line is expecting but I''m willing to bet that it''s not expecting "junk".
这篇关于变量周围的堆栈损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!