Ubuntu C ++ termios.h示例程序

时间:2015-07-27 21:39:03

标签: c++ ubuntu serial-port serial-communication termios

我搜索了很多并尝试了很多不同的方法,但是我无法通过虚拟串行桥向gtkterm发送数据(用于测试!)。

我的想法是稍后与Atmega uC进行通信,但首先我想通过在soccat的帮助下设置虚拟串行桥并使用gtkterm控制输出串口来测试串行通信。问题是我只是在gtkterm中收到无用的东西......(见截图)

soccat命令:

socat -d -d PTY: PTY:

soccat虚拟串口桥似乎没问题,因为我可以将数据从一个串口终端发送到另一个串口终端......

gtkterm端口首选项:

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

我的小GUI编译并运行正常,输入路径为" / dev / pts / 6"程序似乎运行正常,但在gtkterm只是问号和四角形,每个角落都有符号出现。让我们说它不可解释和独立的符号作为输入,但gtkterm中输出的长度会根据输入的长度(我输入的符号数量)而变化。

最后,这是我的代码:

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_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_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;
}

所以...这就是我得到的一切。我尽了最大努力,加入了许多网站的信息并尝试了大量的示例代码。我的问题是我甚至不知道在哪里搜索,所以我很感激任何线索...... 如果有任何问题或信息丢失,请告诉我!

提前致谢 Thorben

顺便说一句:我没有经历过C ++的经历,我开始对我的风格发表评论,但这不应该是主要的问题...

0 个答案:

没有答案