0

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...

Community
  • 1
  • 1
goulashsoup
  • 2,639
  • 2
  • 34
  • 60

2 Answers2

2

Not Necessarily an Answer

But needed this space to add an example.

Have you tried to use Qt functions to determine what ports are available? Here is an example of a function that I use to find USB serial ports (the loop uses c++11 can convert to back if you need)...

QList<QString> QSerialComs::getUsbSerialPortsList()
{
    QList<QString> usbSerialPorts;
    for (auto serialPortInfo: QSerialPortInfo::availablePorts())
    {
        qDebug() << "Port" << serialPortInfo.portName()
                 << "Desc: " << serialPortInfo.description()
                 << endl;

        if (serialPortInfo.description().contains("USB"))
        {
            usbSerialPorts.append(serialPortInfo.portName());
        }
    }

    return usbSerialPorts;
}

The important part here is to print out all the port-names and descriptions so that you can see what ports are available to you... I find this helpful when setting serial coms, you can use it to confirm your socat tool maybe...

code_fodder
  • 15,263
  • 17
  • 90
  • 167
0

The problem was a function filterPorts() in the class Serial which closed the open port in and was called in the testclass

goulashsoup
  • 2,639
  • 2
  • 34
  • 60