在c ++和Linux中使用超时写入和读取串行端口

时间:2014-09-01 07:59:40

标签: c++ linux serial-port

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

  • 这项任务最简单的实施是什么?
  • 我需要使用像boost这样的东西吗?

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

很抱歉,如果我的问题很愚蠢,但我对此主题不熟悉,请提前感谢!

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

2 个答案:

答案 0 :(得分:1)

这应该与您的声明类似:

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编码的东西)< / p>

编辑 - 根据评论;

有关串行端口读/写超时的详细信息,请参阅: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,并且某些答案也可能对您有用! :)

答案 1 :(得分:1)

我最近遇到了同样的问题,我找到了一个很好的解决方案,使用select() 阅读此处的文档:manpages.courirer-mta.org/htmlman2/select2.html

在您的情况下,您需要设置超时,这是select的第5个参数:

    struct timeval timeout;

    timeout.tv_sec = 0 ; // seconds

    timeout.tv_usec = 1000 ; // microseconds

希望这有帮助。