1

I have a relatively gross device (two computers are connected through COM port) and I must get it work by QSerialPort. The idea is simple: data is going to send from one comp to another per COM-port. Transmitter is working okay, which was checked by side software, I have problems with receiving data. I am doing it by QSerialPort as follows:

At first I set up port:

QSerialPort *serialport = new QSerialPort();
serialport->open(QIODevice::ReadOnly);
serialport->setPortName("COM1");
serialport->setBaudRate(QSerialPort::Baud19200);
serialport->setDataBits(QSerialPort::Data8);
serialport->setParity(QSerialPort::NoParity);

and then I am preparing to catch data like this:

connect(serialport,SIGNAL(readyRead()),this, SLOT(change_gear()) );

and in slot change_gear yet I have nothing more than: qDebug()<<"Data has beed recieved",

but this slot have not been ever executed! So, I just cannot understand what is going wrong here and why I can not read data from COM-port such easy way..

OS - Windows 8, Qt 5.8.0 MinGW 32

frogatto
  • 28,539
  • 11
  • 83
  • 129
John
  • 451
  • 6
  • 21
  • 2
    The following instruction `serialport->open(QIODevice::ReadOnly);` must be executed after setting the parameters – eyllanesc Sep 19 '17 at 19:26
  • Current QSerialPort 5.9 does not require opening after parameter setup, but it is more convenient to do it that way. You can change the parameters at will once the port is open. How the device on the other end of the cable will react is another question. I felt this clarification to the above question was warranted as it implies that it is a hard and fast rule. – guitarpicva Nov 25 '17 at 14:27

1 Answers1

0

Try like this

QSerialPort *serialport = new QSerialPort();
serialport->setPortName("COM1");
serialport->setBaudRate(QSerialPort::Baud19200);
serialport->setDataBits(QSerialPort::Data8);
serialport->setParity(QSerialPort::NoParity);

connect(serialport,SIGNAL(readyRead()),this, SLOT(change_gear()) );

serialport->open(QIODevice::ReadOnly);

Open after port is configured and it signal is connected to slot

Vova Shevchyk
  • 138
  • 10
  • 1
    This is not a "rule". It should make no difference when you connect signals to slots with QSerialPort (>= 5.3) . I do it both ways and find no difference between the two. In fact, there is little desire to make connections when there is no open port, thus I typically save the connect statements until after I have verified that the serial port has been opened. – guitarpicva Nov 25 '17 at 14:31