3

I searched a lot and tried many different ways, but I cannot send data to gtkterm via virtual serial bridge (for testing!).

My idea is to communicate with an Atmega uC later on, but first I wanted to test the serial communication by setting up a virtual serial bridge with the help of soccat and controlling the output serial port with gtkterm. The problem is that I'm just receiving useless things in gtkterm... (see screenshots)

soccat command:

socat -d -d PTY: PTY:

The soccat virtual serial port bridge seems to be ok, because I can send data from one serial terminal to another...

gtkterm port preferences:

Port: /dev/pts/6
Baudrate: 9600
Parity: none
Bits: 8
Stopbits: 1
Flow control: none

My little GUI compiles and runs fine, with the input path "/dev/pts/6" and the input baudrate 9600. The program seems to run fine, but in gtkterm are just question marks and quadangles with symbols in every corner coming up. Let's say its not interpretable and independent by the signs as input, BUT the lengths of the output in gtkterm changes by the length of the input (the amount of signs I type in).

Finally here's my code:

main.cpp:

#include <iostream>
#include "serial/serial_communication.cpp"

std::string inputStringUser = "";
int inputIntUser = 0;
std::string pathSerial = "/dev/...";
int baudrate = 19200;

int main()
{
    std::cout << "main() [communication_serial_uC] started..." <<        std::endl;

    //GETTING STARTED
    std::cout << "Use default port? " << pathSerial << " (Yes = y/ change port = insert the new path" << std::endl;
    std::cin >> inputStringUser;
    if(inputStringUser != "y" && inputStringUser != "Y")
        pathSerial = inputStringUser;
    std::cout << "Serial Port is set to: " + pathSerial << std::endl;

    std::cout << "Use default baudrate? " << baudrate << "Yes = 0/     change baudrate = insert new baudrate" << std::endl;
    std::cin >> inputIntUser;

    if(inputIntUser > 0)
        baudrate = inputIntUser;
    std::cout << "Baudrate is set to: " << baudrate << std::endl;

    Serial_communication myPort(pathSerial, baudrate);

    //OPEN/ CONFIGURATE PORT
    if(myPort.openPort(pathSerial, baudrate) < 0)
    {
        std::cout << "Error: opening" << std::endl;
        return -1;
    }

    //WRITE PORT
    std::cout << "Insert your 'message': (exit = 'exit')" << std::endl;
    std::cin >> inputStringUser;
    while(inputStringUser != "exit")
    {
        if(myPort.sendPort(inputStringUser) < 0)
        {
            std::cout << "Error: sending" << std::endl;
            return -1;
        }
        std::cout << "Insert your 'message': (exit = 'exit')" << std::endl;
        std::cin >> inputStringUser;
    }

    //CLOSE PORT
    if(myPort.closePort() < 0)
    {
        std::cout << "Error: closing" << std::endl;
        return -1;
    }

    std::cout << "main() [communication_serial_uC] beendet..." << std::endl;
    return 0;
}

serial/serial_communication.hpp:

#include <iostream>
#include <string>
#include <cstring>
#include <fcntl.h>
#include <termios.h>

class Serial_communication{

    public:
        Serial_communication(std::string paramPathSerial, int paramBaudrate);
        ~Serial_communication();

        int openPort(std::string pathSerial, int baudrate);
        int sendPort(std::string testString);
        int closePort();

    private:
        std::string pathSerial;
        int baudrate;

        //filedescriptors
        int fd;
};

serial/serial_communcation.cpp

#include <iostream>

#include "serial_communication.h"

Serial_communication::Serial_communication(std::string paramPathSerial, int paramBaudrate)
{
    fd = 0;
    pathSerial = paramPathSerial;
    baudrate = paramBaudrate;
}

Serial_communication::~Serial_communication()
{
}

int Serial_communication::openPort(std::string pathSerial, int baudrate)
{
    std::cout << "openPort() [serial_communication] started with the following paramters... pathSerial = " << pathSerial << ", baudrate = " << baudrate << std::endl;

    //OPENING PORT

    //open serial port
    fd = open(pathSerial.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd < 0)
    {
        std::cout << "Error [serial_communcation]: opening Port: " << pathSerial << std::endl;
        return -1;
    }

    //struct termios
    struct termios serial, serial_old;

    //get parameters associated with the terminal
    if(tcgetattr(fd, &serial) < 0)
    {
        std::cout << "Error [serial_communication]: getting configuration" << std::endl;
        return -1;
    }

    //safe old parameters
    serial_old = serial;

    std::cout << "[serial_communication]: Port opened" << std::endl;
    //SERIAL CONFIGURATION
    /* Set Baud Rate */
    cfsetospeed (&serial, (speed_t)baudrate);
    cfsetispeed (&serial, (speed_t)baudrate);

//     Setting other Port Stuff
    serial.c_cflag     &=  ~PARENB;            // Make 8n1
    serial.c_cflag     &=  ~CSTOPB;
    serial.c_cflag     &=  ~CSIZE;
    serial.c_cflag     |=  CS8;

    serial.c_cflag     &=  ~CRTSCTS;           // no flow control
    serial.c_cc[VMIN]   =  1;                  // read doesn't block
    serial.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
    serial.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines



    /* Make raw */
    cfmakeraw(&serial);

    /* Flush Port, then applies attributes */
    tcflush( fd, TCIFLUSH );

    //set attributes to port
    if(tcsetattr(fd, TCSANOW, &serial) < 0)
    {
        std::cout << "Error [serial_communication]: set attributes" << std::endl;
        return -1;
    }

    //CONFIGURATION FINISHED
    std::cout << "openPort() [serial_communication] finished..." << std::endl;
    return 1;
}

int Serial_communication::sendPort(std::string textString)
{
    std::cout << "write() [Serial_communication] started with the following parameter... textString = " << textString << std::endl;
    //attempt to send
    if(write(fd, &textString, std::strlen(textString.c_str())) < 0)
    {
        std::cout << "Error [serial_communcation]: write";
        return -1;
    }

    //SENDING FINISHED
    std::cout << "write() [serial_communcation] finished..." << std::endl;
    return 1;
}

int Serial_communication::closePort()
{
    close(fd);
    return 1;
}

So... thats all I got. I tried my best and joined the information from many websites and tried a lots of example codes. My problem is that I dont even know where to search for, so I'm appreciate for any clue... If there are any questions or information missing, pls let me know it!

Thanks in advance Thorben

BTW: I'm not THAT experienced with C++ and I'm opened up for comments about my style, but that should not be the main problem...

Martin G
  • 17,357
  • 9
  • 82
  • 98
T_Born
  • 31
  • 1
  • 2

0 Answers0