无法读取串行输入

发布于 2024-12-10 01:12:47 字数 607 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-12-17 01:12:47

您没有评估 exitStatus 变量,这可能表明读取成功。

另外,如果您只想读取一个字节,则不需要 malloc 内存,只需将指针传递给本地 char 变量即可。

当我这样做时,main 的类型是 int main()int main(int argc, char** argv)

int main()
{   
    int exitStatus;
    unsigned int bytesToRead = 1;
    unsigned char buffer;
    Serial *connection = new Serial("COM3");

    if(connection->IsConnected()){
        exitStatus=connection->ReadData(&buffer, 1);
        if((exitStatus == <Insert the value for a correct read status>) && (buffer != '0'))
            <statement I'm trying to hit>
    }
    return 0;
}

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() or int main(int argc, char** argv)

int main()
{   
    int exitStatus;
    unsigned int bytesToRead = 1;
    unsigned char buffer;
    Serial *connection = new Serial("COM3");

    if(connection->IsConnected()){
        exitStatus=connection->ReadData(&buffer, 1);
        if((exitStatus == <Insert the value for a correct read status>) && (buffer != '0'))
            <statement I'm trying to hit>
    }
    return 0;
}

update
Changed the != 0 check to != '0', since I suspect that there is a '0' character (=0x30) comming from the serial interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文