Qt serial port: write and read data

2019-09-06 21:08发布

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条回答
乱世女痞
2楼-- · 2019-09-06 21:30

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

查看更多
登录 后发表回答