Qt serial port: write and read data

廉价感情. 提交于 2019-12-11 14:26:44

问题


I'm able to connect to my serial port, but I need to read data from it all the time.
It's working for about 39 minutes, but after it stops at line serial.flush().
When I stop Qt and start it again it also stops at serial.flush(). I need to restart my modem to let it work again...

I'm not sure I'm executing this properly...

// Open Serial connexion
QSerialPort serial;
serial.setPortName("usbserial-26214A");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::HardwareControl);

if (serial.isOpen() && serial.isWritable())
{
qDebug() << "Serial is open";

QByteArray output;
QByteArray input;

  while(true)
  {
  output = "AT+CPMS=\"SM\"\r";
  serial.write(output);
  serial.flush();
  serial.waitForBytesWritten(1000);
  serial.waitForReadyRead(1000);
  input = serial.readAll();
  }
}

//EDIT

if deleting flush it's working, but code goes really fast. It don't wait for WaitFor...
After 2nd loop he doesn't has time to read data.

serial.isOpen() && serial.isWritable() always sends true in while loop.

Program stops at flush after 30 minutes!


回答1:


If serial.isOpen() && serial.isWritable() is true in while loop, but you can't access it could come from you preferences on your computer.

On OSX: System preferences -> network -> select serial -> advanced -> PPP:
able: Ask to stay connected every 10 minutes,
disable: disconnect at close of session,
disable: disconnect when changing of user



来源:https://stackoverflow.com/questions/26459788/qt-serial-port-write-and-read-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!