c++ - 在 C++ 和 Linux 中通过超时写入和读取串口

标签 c++ linux serial-port

我正在尝试将一些十六进制字符串发送到串行端口,并直接在 C++ 中读取它的答案。如果没有应答,它应该在给定时间后超时。

  • 这种任务最简单的实现是什么?
  • 我需要使用 boost 之类的东西吗?

明确一点:我正在寻找实现此目标的最简单方法。

如果我的问题很愚蠢,我很抱歉,但我是这个话题的新手,在此先感谢!

编辑:抱歉,我忘了说,它应该在 Linux 上运行。

最佳答案

这应该类似于您的声明:

Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);

Setup(CSerial::EBaudrate(9600),
      CSerial::EDataBits(8),
      CSerial::EParity(NOPARITY),
      CSerial::EStopBits(ONESTOPBIT));

读取数据:

// Read data, until there is nothing left
    DWORD dwBytesRead = 0;
    BYTE  abBuffer[100];
    do
    {
        // Read data from the COM-port
        serial.Read(abBuffer,sizeof(abBuffer),&dwBytesRead);
        if (dwBytesRead > 0)
        {
            // TODO: Process the data
        }
    }
    while (dwBytesRead == sizeof(abBuffer));

可以在代码项目中找到更多详细信息:http://www.codeproject.com/Articles/992/Serial-library-for-C

请注意:我习惯于使用 C# 编写串行端口,因此(据我所知)相信这对您有用。 (我还想指出所有串口通信实际上是通过十六进制发送的,但可以通过缓冲区读取为十进制值(请引用 http://www.asciitable.com/ 进行转换,或使用类似于 UTF8 编码的编码)

编辑 - 根据评论;

请引用:http://msdn.microsoft.com/en-gb/library/windows/hardware/hh439614(v=vs.85).aspx有关串行端口读/写超时的详细信息

写超时将类似于;

[BrowsableAttribute(true)]
public:
property int WriteTimeout {
    int get ();
    void set (int value);
}

它允许您获取或设置超时属性

完整程序

public:
    static void Main()
    {
        String^ name;
        String^ message;
        StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
        Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));

        // Create a new SerialPort object with default settings.
        _serialPort = gcnew SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort->PortName = SetPortName(_serialPort->PortName);
        _serialPort->BaudRate = SetPortBaudRate(_serialPort->BaudRate);
        _serialPort->Parity = SetPortParity(_serialPort->Parity);
        _serialPort->DataBits = SetPortDataBits(_serialPort->DataBits);
        _serialPort->StopBits = SetPortStopBits(_serialPort->StopBits);
        _serialPort->Handshake = SetPortHandshake(_serialPort->Handshake);

        // Set the read/write timeouts
        _serialPort->ReadTimeout = 500;
        _serialPort->WriteTimeout = 500;

        _serialPort->Open();
        _continue = true;
        readThread->Start();

        Console::Write("Name: ");
        name = Console::ReadLine();

        Console::WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console::ReadLine();

            if (stringComparer->Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort->WriteLine(
                    String::Format("<{0}>: {1}", name, message) );
            }
        }

        readThread->Join();
        _serialPort->Close();
    }

    static void Read()
    {
        while (_continue)
        {
            try
            {
                String^ message = _serialPort->ReadLine();
                Console::WriteLine(message);
            }
            catch (TimeoutException ^) { }
        }
    }

编辑 2

请引用Linux Serial Port: Blocking Read with Timeout这已在问题中声明,一些答案也可能对您有用! :)

关于c++ - 在 C++ 和 Linux 中通过超时写入和读取串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25601269/

相关文章:

c++ - 3D图形网格法 vector 旋转加倍

c++ - 如何调用继承类的函数?

.net - 为什么 SerialPort 类没有 RIHolding 属性?

C# 读取HEX串口数据并发送给Streamwriter

Linux:用一个进程从串口读取数据,用另一个进程写入

C++ gamma_distribution 返回无穷大

c++ - 使用 ffmpeg-lib 实现一个简单的 MPEG-TS muxer

python - 如何使用Python将文件/目录从Linux远程复制到AWS EC2实例上的Windows?

ruby - 如何将电子邮件从 cPanel 传输到 Ruby 脚本

linux - 恢复linux中的arp缓存