I have a Class Serial
in which I can open a port via the member function Serial::openPort()
with private QSerialPort serial_stream
:
bool Serial::openPort(std::string port)
{
std::string realPort = "/dev/" + port;
if(isOpen()) {
return true;
}
serial_stream.setPortName(QString(realPort.c_str()));
bool loc = serial_stream.open(serial_stream.ReadWrite);
serial_stream.setFlowControl(serial_stream.HardwareControl);
serial_stream.setStopBits(serial_stream.OneStop);
serial_stream.setParity(serial_stream.NoParity);
serial_stream.setDataBits(serial_stream.Data8);
serial_stream.setBaudRate(defaultBaud);
open = loc;
return loc;
}
and the method send:
bool Serial::send(unsigned char data[])
{
if(!isOpen()) {
return false;
}
int size = BUFFER_SIZE;
char loc[size];
for(int p = 0; p<size; p++) {
loc[p] = data[p];
}
int check = serial_stream.write(loc, size);
if(check != size) {
return false;
}
return true;
}
I open a virtual port via socat:
Terminal 1: socat -d -d pty,raw,echo=0 pty,raw,echo=0
Terminal 2: cat < /dev/pts/2
Then in a test function I do the following:
bool b = Serial::instance().openPort("pts/3");
cout << b << endl;//-> "true"
unsigned char data[8] = {'a','b','c','d','e','f','g','h'};
bool sent = Serial::instance().send(data);
So what I wanted was that I can send data
to pts/2
via open the port pts/3
withQSerialPort
in the Serial
class. b is true
so QSerialPort
was able to open the port. But when I want send data
I get the following information:
QIODevice::write (QSerialPort): device not open
I think the issue must have something to do with using socat wrong, because we allready tested the code with a RSR232-cable and another computer.
Would be thankful for your help.
EDIT:
After the first (no)-answer from code_fodder I did what he told me to do and added a for loop in my initTestCase()
:
void tst_serial::initTestCase() {
cout << "Testfälle gestartet" << endl;
QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
cout << "for: " << ports.count() << endl;//-> for: 0
for (int n=0; n > ports.count(); n++)
{
cout << ports[n].portName().toStdString() << ports[n].description().toStdString() << endl;
}
}
Like I commented, the number of available ports is 0 (Expect this is not the right way to do it). But why does Serial::instance().openPort("pts/3");
return true ?
Like I just said, it must have something todo with socat...