Linux serial port listener and interpreter?

前端 未结 4 868
抹茶落季
抹茶落季 2020-12-08 16:56

I\'m using a serial device for a project, and what I\'m trying to accomplish PC side, is listening for a command sent by the serial device, interpreting the query, running s

相关标签:
4条回答
  • 2020-12-08 17:41

    I would recommend to use C/C++ with Qt 5.1.1, it's really easy and if you are familiar with the framework it'll be a piece of cake!!! Here you can find more information and here more helpful examples, give it a try, it's really pain free!! Also you can develop on win and then port your code to linux...straight forward.

    Declare an object like this:

    QSerialPort mPort; //remember to #include <QtSerialPort/QSerialPort>
    //also add QT += serialport to your .pro file
    

    Then add this code:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        setupUi(this);
    
    
        connect(this->pushButton,SIGNAL(clicked()),this,SLOT(sendData()));
    
        mPort.setPortName("ttyS0");
        mPort.setBaudRate(QSerialPort::Baud115200);
        mPort.setParity(QSerialPort::EvenParity);
    
        if(!mPort.open(QSerialPort::ReadWrite))
        {
            this->label->setText(tr("unable to open port, %1").arg(mPort.error()));
        }
    
        connect(&(this->mPort),SIGNAL(readyRead()),this,SLOT(readData()));
    }   
    
    void MainWindow::sendData()
    {
    
        QByteArray data = lineEdit->text().toLatin1();
        if(mPort.isOpen())
        {
            mPort.write(data);
        }
        else
        {
            this->label->setText(tr("port closed %1").arg( mPort.error()));
    
        }
    }
    
    
    void MainWindow::readData()
    {
    
        QString newData;
        int bread=0;
        while(bread < mPort.bytesAvailable() ){
            newData += mPort.readAll();
            bread++;
        }
      this->textEdit->insertPlainText("\n" + newData);
    }
    
    0 讨论(0)
  • 2020-12-08 17:45

    If you use right tools, it is possible to actually have your CPU usage to be exactly 0 when your device does not have any output.

    To accomplish this, you should use some higher level language (Perl, Python, C/C++ would do, but not bash) and use select loop on top of file handle of your serial device. This is an example for Perl http://perldoc.perl.org/IO/Select.html, but you can use any other language as long as it has support for select() syscall.

    0 讨论(0)
  • 2020-12-08 17:51

    To remain fairly system independent, use a cross platform programming language: like Python, use a cross platform serial library like : pySerial and do the processing inside a script. I have used pySerial and I could run the script cross platform with almost no changes in source code. By using BASH you're limiting yourself a fair little.

    0 讨论(0)
  • 2020-12-08 17:55

    Is this not what you're looking for?

    while read -r line < /dev/ttyS2; do
      # $line is the line read, do something with it
      # which produces $result
      echo $result > /dev/ttyS2
    done
    
    0 讨论(0)
提交回复
热议问题