简体   繁体   English

Linux C ++写字符串到COM端口,回读响应?

[英]Linux C++ write string to COM port, read back response?

I am trying to write to the /dev/ttyACM0 port. 我正在尝试写入/ dev / ttyACM0端口。 In the command line shell I can write the string ":35\\n" to the sensor I have plugged in to that port, and via putty listening on the port I can see it responds and gives me the data I am asking for (compass data). 在命令行shell中,我可以将字符串“:35 \\ n”写入我已插入该端口的传感器,并通过putty监听端口,我可以看到它响应并提供我要求的数据(罗盘)数据)。

I want to write a driver in C++ that writes this string to that port and reads in the response returned. 我想用C ++编写一个驱动程序,将该字符串写入该端口并读入返回的响应。 I have looked online but have been unsuccessful in writing this. 我已经在网上看过,但写这篇文章并不成功。 I feel like it should be an easy task. 我觉得这应该是一件容易的事。 I wouldn't think writing to the ttyACM port would be any different than writing to a ttyCOM or ttyS port. 我不认为写入ttyACM端口与写入ttyCOM或ttyS端口有什么不同。 Can anyone give me an example of this? 谁能举个例子呢? I wouldn't think it'd be more than 5 lines of code. 我认为它不会超过5行代码。

Thank you for any help. 感谢您的任何帮助。

As requested, five lines of code: 根据要求,五行代码:

#include <fstream>
std::fstream file("/dev/ttyACM0");
file << ":35" << std::endl; // endl does flush, which may be important
std::string response;
file >> response;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM