Nvidia TX1 TX2 串口通信方法
- 英伟达TX2串口
- 串口硬件
- 板子串口描述
- 硬件连接方式
- 测试工具及方法
- c++通信代码
英伟达TX2串口
使用英伟达嵌入式板进行 串口通信 。
串口硬件
串口位置为下图中红色方框位置,标示UARTQ20和J17。
板子串口描述
串口共6个引脚,靠近板子边缘处的第一个为GND引脚,接地;
第四个TX引脚;
第五个RX引脚;
通过USB转接连接到主机Host。
硬件连接方式
使用串口转USB转接器,将TX2引脚与主机USB连接。
测试工具及方法
在linux环境下,分别在Host 和 nvidia device 上使用cutecom:
- 使用 sudo apt-get install cutecom 即可安装;
- 安裝完成后,使用 sudo cutecom 启动程序;
启动界面如下图:
说明:
- . Device: 选择串口名称,Host上串口名称为ttyUSB*命名模式,在英伟达TX2上为ttyTHS2,可修改字符串名字。
- Settings: 设置波特率等,两者设置相同即可。
- 点击 Open启动通信。
- Input后输入内容,将会通过软件送到串口,开始通信。
- 上部分空白显示串口接收的内容,下方空白显示输入 Input历史。
c++通信代码
包含三个文件,分别为serialport.h、serialport.cpp、main.cpp文件。
serialport.h
:
#ifndef BINOCULAR_CUSTOM_SERIALPORT_H
#define BINOCULAR_CUSTOM_SERIALPORT_H#include <string>
#include <termios.h>#include <vector> struct termios;
class SerialPort {
public:enum BaudRate{ BR0 = 0000000, BR50 = 0000001, BR75 = 0000002,BR110 = 0000003, BR134 = 0000004, BR150 = 0000005, BR200 = 0000006,BR300 = 0000007, BR600 = 0000010, BR1200 = 0000011, BR1800 = 0000012, BR2400 = 0000013,BR4800 = 0000014, BR9600 = 0000015, BR19200 = 0000016, BR38400 = 0000017, BR57600 = 0010001,BR115200 = 0010002, BR230400 = 0010003, BR460800 = 0010004, BR500000 = 0010005, BR576000 = 0010006,BR921600 = 0010007, BR1000000 = 0010010, BR1152000 = 0010011, BR1500000 = 0010012, BR2000000 = 0010013,BR2500000 = 0010014, BR3000000 = 0010015, BR3500000 = 0010016, BR4000000 = 0010017 };enum DataBits { DataBits5, DataBits6, DataBits7, DataBits8, };enum StopBits { StopBits1, StopBits2 };enum Parity { ParityNone, ParityEven, PariteMark, ParityOdd, ParitySpace };struct OpenOptions {bool autoOpen;BaudRate baudRate;DataBits dataBits;StopBits stopBits;Parity parity;bool xon; bool xoff; bool xany; int vmin; int vtime; };static BaudRate BaudRateMake(unsigned long baudrate);static const OpenOptions defaultOptions;explicit SerialPort(const std::string& path, const OpenOptions options = defaultOptions);bool open();bool open(const std::string& path, const OpenOptions& options);bool isOpen() const;int write(const void *data, int length);int read(void *data, int length);void close();static std::vector<std::string > list();
protected:void termiosOptions(termios& tios, const OpenOptions& options);
private:std::string _path;OpenOptions _open_options;int _tty_fd; bool _is_open;
};
bool operator==(const SerialPort::OpenOptions& lhs, const SerialPort::OpenOptions& rhs);
bool operator!=(const SerialPort::OpenOptions& lhs, const SerialPort::OpenOptions& rhs);#endif //BINOCULAR_CUSTOM_SERIALPORT_H
serialport.cpp
:
//
// Created by yaqiang on 7/22/19.
//
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include "serialport.h"const SerialPort::OpenOptions SerialPort::defaultOptions ={ true, // bool autoOpen;SerialPort::BR9600, // BaudRate baudRate;SerialPort::DataBits8, // DataBits dataBits;SerialPort::StopBits1, // StopBits stopBits;SerialPort::ParityNone,// Parity parity;false, // input xonfalse, // input xofffalse, // input xany0, // c_cc vmin50, // c_cc vtime};SerialPort::SerialPort(const std::string &path, const OpenOptions options) : _path(path), _open_options(options)
{if(options.autoOpen){printf("opening!\n");_is_open = open(_path, _open_options);}
}bool SerialPort::open()
{return _is_open = open(_path, _open_options), _is_open;
}
bool SerialPort::open(const std::string &path, const OpenOptions &options)
{if(_path != path)_path = path;if(_open_options != options)_open_options = options;_tty_fd = ::open(path.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);if(_tty_fd < 0){return false;}struct termios tios;termiosOptions(tios, options);tcsetattr(_tty_fd, TCSANOW, &tios);tcflush(_tty_fd, TCIOFLUSH); return true;
}
void SerialPort::termiosOptions(termios &tios, const OpenOptions &options)
{tcgetattr(_tty_fd, &tios); tios.c_oflag = 0; tios.c_iflag = 0;tios.c_lflag = 0; cfsetispeed(&tios, options.baudRate);cfsetospeed(&tios, options.baudRate); tios.c_iflag |= (options.xon ? IXON : 0) | (options.xoff ? IXOFF: 0) | (options.xany ? IXANY : 0); // data bitsint databits[] = {CS5, CS6, CS7, CS8}; tios.c_cflag &= ~0x30; tios.c_cflag |= databits[options.dataBits]; // stop bitsif(options.stopBits == StopBits2) { tios.c_cflag |= CSTOPB; } else { tios.c_cflag &= ~CSTOPB; } // parityif(options.parity == ParityNone) { tios.c_cflag &= ~PARENB; } else { tios.c_cflag |= PARENB;if(options.parity == PariteMark) { tios.c_cflag |= PARMRK; } else { tios.c_cflag &= ~PARMRK; }if(options.parity == ParityOdd) { tios.c_cflag |= PARODD; } else { tios.c_cflag &= ~PARODD; }}tios.c_cc[VMIN] = options.vmin; tios.c_cc[VTIME] = options.vtime;
}
bool SerialPort::isOpen() const { return _is_open; }int SerialPort::write(const void *data, int length)
{return ::write(_tty_fd, data, length);
}int SerialPort::read(void *data, int length) { return ::read(_tty_fd, data, length); }void SerialPort::close() { ::close(_tty_fd); _is_open = false; }SerialPort::BaudRate SerialPort::BaudRateMake(unsigned long baudrate)
{switch (baudrate){case 50: return BR50; case 75: return BR75; case 134: return BR134; case 150: return BR150;case 200: return BR200; case 300: return BR300; case 600: return BR600; case 1200: return BR1200;case 1800: return BR1800; case 2400: return BR2400; case 4800: return BR4800; case 9600: return BR9600;case 19200: return BR19200; case 38400: return BR38400; case 57600: return BR57600; case 115200: return BR115200;case 230400: return BR230400; case 460800: return BR460800; case 500000: return BR500000; case 576000: return BR576000;case 921600: return BR921600; case 1000000: return BR1000000; case 1152000: return BR1152000; case 1500000: return BR1500000;case 2000000: return BR2000000; case 2500000: return BR2500000; case 3000000: return BR3000000;case 3500000: return BR3500000; case 4000000: return BR4000000; default: break;}return BR0;
}std::vector<std::string> SerialPort::list()
{DIR *dir; struct dirent *ent; dir = opendir("/dev");std::vector<std::string> ttyList;while(ent = readdir(dir), ent != nullptr){if("tty" == std::string(ent->d_name).substr(0, 3)){ttyList.emplace_back(ent->d_name);}}return ttyList;
}
bool operator==(const SerialPort::OpenOptions& lhs, const SerialPort::OpenOptions& rhs)
{return lhs.autoOpen == rhs.autoOpen && lhs.baudRate == rhs.baudRate && lhs.dataBits == rhs.dataBits && lhs.parity == rhs.parity && lhs.stopBits == rhs.stopBits && lhs.vmin == rhs.vmin && lhs.vtime == rhs.vtime && lhs.xon == rhs.xon && lhs.xoff == rhs.xoff && lhs.xany == rhs.xany;
}
bool operator!=(const SerialPort::OpenOptions& lhs, const SerialPort::OpenOptions& rhs)
{return !(lhs == rhs);
}
main.cpp
:
#include "serialport.h"typedef unsigned char uchar;
int main()
{printf("begin!\n");SerialPort serialPort = SerialPort("/dev/ttyUSB0");printf("connecting!\n");serialPort.open();if(serialPort.isOpen())printf("connected!\n");else {printf("connect failed!\n");return 0;}char* buffer = new char[10];for(int i=0;i<10;i++){buffer[i]='a';}printf("begin write!\n");serialPort.write(buffer,sizeof(char)*10);printf("write end!\n");
// serialPort.close();
// serialPort.open();// int* buffer2= new int[10];
// int* buffer2;
// serialPort.read(buffer2,sizeof(int)*10);
// for(int i=0;i<10;i++)
// {
// printf("%d \n",buffer2[i]);
// }serialPort.close();if(serialPort.isOpen())printf("still connecting!\n");else {printf("connect closed!\n");return 0;}}