Ubuntu C ++ termios.h example program
I've searched a lot and tried many different ways, but I can't seem to send data to gtkterm over a virtual serial bridge (for testing!).
My idea is to contact Atmega uC later, but first I wanted to test the serial communication by setting up a virtual serial bridge using soccat and controlling the output serial port with gtkterm. The problem is I just get useless stuff in gtkterm ... (see screenshots)
Soccat command:
socat -d -d PTY: PTY:
The soccat virtual serial port bridge looks ok because I can send data from one serial terminal to another ...
Gtkterm port settings:
Port: /dev/pts/6
Baudrate: 9600
Parity: none
Bits: 8
Stopbits: 1
Flow control: none
My little GUI compiles and works fine, with an input path "/ dev / pts / 6" and an input speed of 9600. The program works fine, but in gtkterm there are only question marks and squares with symbols in each corner suitable. Let's say it is not interpretable and sign independent as input, BUT the output lengths in gtkterm vary with the length of the input (the number of characters I entered).
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 ... that's all I have. I tried my best and joined information from many websites and tried many code examples. My problem is I don't even know where to look, so I appreciate any hint ... If you have any questions or information, please let me know!
Thanks in advance Thorben
BTW: I AM NOT THAT CASE WITH C ++ and I'm open to comments about my style, but that shouldn't be the main issue ...
source to share
No one has answered this question yet
Check out similar questions: