无法读取串行输入
我有一个 Arduino 通过 USB 连接到我的电脑。我尝试从中读取 COM 端口。
谁能看出这是否有什么明显的错误???
void main()
{
int exitStatus;
unsigned int bytesToRead = 1;
unsigned char *buffer = (unsigned char*) malloc(sizeof(unsigned char) + 1);
Serial *connection = new Serial("COM3");
if(connection->IsConnected()){
exitStatus=connection->ReadData(buffer, bytesToRead);
if( *buffer > 0)
<statement I'm trying to hit>
}
}
现在它总是会击中“我试图击中的陈述”,即使它不应该如此。调试它总是显示缓冲区的内容是很多垃圾。我知道来自串行输入的内容应该是正确的,因为从我在串行监视器中看到的情况来看,一切看起来都很好。
想法?
I have an Arduino hooked up via usb to my computer. From it I try to read the from the COM port.
Can anyone see if theres anything blatantly wrong with this???
void main()
{
int exitStatus;
unsigned int bytesToRead = 1;
unsigned char *buffer = (unsigned char*) malloc(sizeof(unsigned char) + 1);
Serial *connection = new Serial("COM3");
if(connection->IsConnected()){
exitStatus=connection->ReadData(buffer, bytesToRead);
if( *buffer > 0)
<statement I'm trying to hit>
}
}
Right now it always hits the 'statement I'm trying to hit' even when it's no supposed to. Debugging it always shows that the contents of buffer is a lot of junk. I know what's coming in from the serial input should be right because from what I'm seeing in the serial monitor, it all looks good.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有评估
exitStatus
变量,这可能表明读取成功。另外,如果您只想读取一个字节,则不需要 malloc 内存,只需将指针传递给本地 char 变量即可。
当我这样做时,main 的类型是
int main()
或int main(int argc, char** argv)
update
将 != 0 检查更改为 != '0',因为我怀疑有一个 '0' 字符 (=0x30) 来自串行接口。
You are not evaluating the
exitStatus
variable, which might indicate it the read was successful.Also you don't need to malloc memory if you want to read just one byte, you can simply pass the pointer to a local char variable.
And while I'm at it, the type for main is either
int main()
orint main(int argc, char** argv)
update
Changed the != 0 check to != '0', since I suspect that there is a '0' character (=0x30) comming from the serial interface.