一、串口的基本认知
二、串口关于电气标准和协议标准
三、串口通信协议
四、Linux系统中使用串口通信
五、orangepi zern2 两组串口
六、串行测试serialtest
七、基于wiringPi的串口开发
八、通过分析源码来实现的串口通信
串口(Serial Port),也被称为串行端口,是一种用于数据通信的物理接口,允许数据以连续的比特流形式在计算机和外部设备之间进行传输。串口通信在各种应用中非常常见,包括传感器连接、嵌入式系统、通信设备、GPS接收器、打印机等。
以下是关于串口的一些基本认知:
串口的特点:
串口通信的标准:
串口通信参数:
数据传输:
流控制:
串口设备:
/dev
目录下。应用:
编程:
串口通信是一种非常有用的通信方式,特别适合连接外部设备和嵌入式系统。了解串口通信的基本概念和参数设置对于成功进行串口通信非常重要。
串口通信涉及两个主要方面的标准:电气标准和协议标准。电气标准规定了物理层的信号电平和电气特性,而协议标准规定了数据的格式和传输方式。以下是关于这两个方面的一些常见标准:
电气标准:
RS-232:RS-232 是一种常见的串口电气标准,它定义了串行通信的电气和机械特性。RS-232 接口使用正负电平来表示逻辑值,通常用于连接计算机和外部设备。
RS-485:RS-485 是一种多点半双工串口标准,通常用于工业控制和数据通信应用。RS-485 具有更远的传输距离和更高的噪声抗干扰能力。
TTL(Transistor-Transistor Logic):TTL 是一种数字电平标准,通常用于嵌入式系统和数字电路中。TTL 电平通常以0V表示逻辑低电平,以3.3V或5V表示逻辑高电平。
CMOS(Complementary Metal-Oxide-Semiconductor):CMOS 也是一种数字电平标准,通常用于数字电路和微控制器中。CMOS 电平通常以0V表示逻辑低电平,以接近电源电压的值表示逻辑高电平。
LVDS(Low Voltage Differential Signaling):LVDS 是一种差分信号标准,通常用于高速数据传输,如显示器连接。
协议标准:
UART(Universal Asynchronous Receiver/Transmitter):UART 是一种常见的串口通信协议,通常用于数据的异步传输。它包括起始位、数据位、停止位和可选的校验位。
SPI(Serial Peripheral Interface):SPI 是一种同步串行通信协议,通常用于连接微控制器、传感器和外围设备。SPI 使用主从结构,包括时钟、数据输入和数据输出线。
I2C(Inter-Integrated Circuit):I2C 是一种串行通信协议,通常用于连接多个设备到相同的总线上。它使用双线制,包括数据线(SDA)和时钟线(SCL)。
RS-232、RS-485协议:除了电气标准,RS-232 和 RS-485 还包括通信协议,用于定义数据帧格式和流控制。
Modbus:Modbus 是一种常用于工业自动化的串行通信协议,支持多种物理层标准,包括串口通信。
CAN(Controller Area Network):CAN 是一种串行通信协议,通常用于汽车和工业控制领域。它支持高噪声环境下的可靠通信。
USB(Universal Serial Bus):USB 是一种通用的串行通信标准,用于连接计算机和外部设备。它支持高速、全速和低速传输模式。
不同的应用和硬件要求通常需要不同的电气标准和协议标准。因此,在进行串口通信时,你需要了解你所使用的设备的规格和要求,以便正确配置串口参数并选择适当的通信协议。
串口通信协议中的波特率、奇偶检验位和停止位等参数是非常重要的,它们决定了数据的传输方式和数据的完整性检查。以下是一些常见的串口通信参数:
波特率(Baud Rate):
数据位(Data Bits):
奇偶检验位(Parity):
停止位(Stop Bits):
这些参数通常一起组合,以确定数据的帧格式。例如,常见的串口通信设置是:
这意味着每个数据帧由 10 位组成:1 位起始位、8 位数据位、无奇偶检验位和 1 位停止位。
正确配置这些参数对于串口通信非常重要,因为通信的双方必须使用相同的参数,否则会导致数据传输错误。通常,串口设备的规格表明了所需的通信参数设置。在编程中,你需要使用相应的库函数来设置这些参数,以确保正确的数据传输和校验。
串口通信是一种通过串行连接传输数据的通信方式,它通常用于连接计算机和外部设备,如传感器、嵌入式系统、GPS接收器、微控制器等。在Linux系统中,你可以使用串口通信来与这些设备进行数据交换。
以下是在Linux系统中使用串口通信的一般步骤:
打开串口设备:
/dev
目录下,例如 /dev/ttyS0
表示第一个串口设备。你可以使用C语言中的 open
函数打开串口设备文件,类似于打开文件。配置串口参数:
termios
结构和 tcsetattr
函数来配置串口通信参数,如波特率、数据位、停止位、校验位等。这些参数需要与目标设备匹配,以确保正确的通信。读取和写入数据:
read
函数从串口读取数据,使用 write
函数将数据写入串口。你可以使用这些函数来与设备进行数据交换。关闭串口:
close
函数关闭串口设备。以下是一个示例C程序,演示如何在Linux系统中打开串口、配置串口参数、读取和写入数据:
#include
#include
#include
#include
#include
#include
int main() {
int fd;
struct termios serial;
// 打开串口设备
fd = open("/dev/ttyS0", O_RDWR);
if (fd == -1) {
perror("open");
return 1;
}
// 配置串口参数
tcgetattr(fd, &serial);
serial.c_cflag = B9600; // 波特率 9600
serial.c_cflag |= CS8; // 8位数据位
serial.c_cflag &= ~PARENB; // 无奇偶校验
tcsetattr(fd, TCSANOW, &serial);
char data_to_send[] = "Hello, Serial!";
write(fd, data_to_send, strlen(data_to_send));
char buffer[100];
ssize_t n = read(fd, buffer, sizeof(buffer));
if (n > 0) {
buffer[n] = '\0';
printf("Received data: %s\n", buffer);
}
close(fd); // 关闭串口
return 0;
}
这个示例演示了如何与串口设备进行基本的数据交换。请注意,串口设备的配置参数需要与你连接的设备匹配,因此根据设备的要求来调整波特率和其他参数。此示例中使用的是 /dev/ttyS0
,你可能需要根据你的系统和硬件配置来指定正确的串口设备。
/*
* serialTest.c:
* Very simple program to test the serial port. Expects
* the port to be looped back to itself
*
* Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see .
***********************************************************************
*/
#include
#include
#include
#include
#include
int main ()
{
int fd ;
int count ;
unsigned int nextTime ;
if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
nextTime = millis () + 300 ;
for (count = 0 ; count < 256 ; )
{
if (millis () > nextTime)
{
printf ("\nOut: %3d: ", count) ;
fflush (stdout) ;
serialPutchar (fd, count) ;
nextTime += 300 ;
++count ;
}
delay (3) ;
while (serialDataAvail (fd))
{
printf (" -> %3d", serialGetchar (fd)) ;
fflush (stdout) ;
}
}
printf ("\n") ;
return 0 ;
}
#include
#include
#include
#include
#include
#include
#include
#include
int fd;
void* Sendhandler()
{
char *sendBuf;
sendBuf = (char *)malloc(32*sizeof(32));
while (1){
memset(sendBuf, '\0', 32);
scanf("%s", sendBuf);
while (*sendBuf){
serialPutchar(fd, *sendBuf++);
}
}
}
void* Revhandler()
{
while (1){
while (serialDataAvail(fd)){
printf("%c", serialGetchar(fd));
fflush(stdout);
}
}
}
int main()
{
int count;
unsigned int nextTime;
pthread_t idSend;
pthread_t idRev;
if ((fd = serialOpen("/dev/ttyS5", 115200)) < 0){
fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
return 1;
}
pthread_create(&idSend, NULL, Sendhandler, NULL);
pthread_create(&idRev, NULL, Revhandler, NULL);
if (wiringPiSetup() == -1){
fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
return 1 ;
}
while (1){sleep(10);}
printf("\n");
return 0;
}
/*
* wiringSerial.c:
* Handle a serial port
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see .
***********************************************************************
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "wiringSerial.h"
/*
* serialOpen:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
*********************************************************************************
*/
int serialOpen (const char *device, const int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
switch (baud)
{
case 50: myBaud = B50 ; break ;
case 75: myBaud = B75 ; break ;
case 110: myBaud = B110 ; break ;
case 134: myBaud = B134 ; break ;
case 150: myBaud = B150 ; break ;
case 200: myBaud = B200 ; break ;
case 300: myBaud = B300 ; break ;
case 600: myBaud = B600 ; break ;
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 4800: myBaud = B4800 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
case 57600: myBaud = B57600 ; break ;
case 115200: myBaud = B115200 ; break ;
case 230400: myBaud = B230400 ; break ;
case 460800: myBaud = B460800 ; break ;
case 500000: myBaud = B500000 ; break ;
case 576000: myBaud = B576000 ; break ;
case 921600: myBaud = B921600 ; break ;
case 1000000: myBaud = B1000000 ; break ;
case 1152000: myBaud = B1152000 ; break ;
case 1500000: myBaud = B1500000 ; break ;
case 2000000: myBaud = B2000000 ; break ;
case 2500000: myBaud = B2500000 ; break ;
case 3000000: myBaud = B3000000 ; break ;
case 3500000: myBaud = B3500000 ; break ;
case 4000000: myBaud = B4000000 ; break ;
default:
return -2 ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
fcntl (fd, F_SETFL, O_RDWR) ;
// Get and modify current options:
tcgetattr (fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
tcsetattr (fd, TCSANOW, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ; // 10mS
return fd ;
}
/*
* serialFlush:
* Flush the serial buffers (both tx & rx)
*********************************************************************************
*/
void serialFlush (const int fd)
{
tcflush (fd, TCIOFLUSH) ;
}
/*
* serialClose:
* Release the serial port
*********************************************************************************
*/
void serialClose (const int fd)
{
close (fd) ;
}
/*
* serialPutchar:
* Send a single character to the serial port
*********************************************************************************
*/
void serialPutchar (const int fd, const unsigned char c)
{
int ret;
ret = write (fd, &c, 1) ;
if (ret < 0)
printf("Serial Putchar Error\n");
}
/*
* serialPuts:
* Send a string to the serial port
*********************************************************************************
*/
void serialPuts (const int fd, const char *s)
{
int ret;
ret = write (fd, s, strlen (s));
if (ret < 0)
printf("Serial Puts Error\n");
}
/*
* serialPrintf:
* Printf over Serial
*********************************************************************************
*/
void serialPrintf (const int fd, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
serialPuts (fd, buffer) ;
}
/*
* serialDataAvail:
* Return the number of bytes of data avalable to be read in the serial port
*********************************************************************************
*/
int serialDataAvail (const int fd)
{
int result ;
if (ioctl (fd, FIONREAD, &result) == -1)
return -1 ;
return result ;
}
/*
* serialGetchar:
* Get a single character from the serial device.
* Note: Zero is a valid character and this function will time-out after
* 10 seconds.
*********************************************************************************
*/
int serialGetchar (const int fd)
{
uint8_t x ;
if (read (fd, &x, 1) != 1)
return -1 ;
return ((int)x) & 0xFF ;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "wiringSerial.h"
int my_serialOpen (const char *device, const int baud) ;
void my_serialSendstring (const int fd, const char *s) ;
int my_serialGetstring (const int fd, char *buffer) ;
#include "wiringSerial.h"
#include "uartTool.h"
int my_serialOpen (const char *device, const int baud)
{
struct termios options ; // 创建一个termios结构体,用于串口参数设置
speed_t myBaud ; // 创建一个速度类型的变量 myBaud,用于保存波特率
int status, fd ; // 创建整数类型的变量 status 和 fd,用于保存状态和文件描述符
switch (baud){ // 根据传入的波特率参数选择合适的波特率常数
case 9600: myBaud = B9600 ; break ;
case 115200: myBaud = B115200 ; break ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) // 打开串口设备,设置打开选项
return -1 ; // 如果打开失败,返回错误代码 -1
fcntl (fd, F_SETFL, O_RDWR) ; // 设置文件状态标志
// Get and modify current options: 获取并修改当前的串口参数:
tcgetattr (fd, &options) ; // 获取当前的串口参数
cfmakeraw (&options) ; // 初始化 termios 结构体为原始模式
cfsetispeed (&options, myBaud) ; // 设置输入波特率
cfsetospeed (&options, myBaud) ; // 设置输出波特率
options.c_cflag |= (CLOCAL | CREAD) ; // 本地连接和使能接收
options.c_cflag &= ~PARENB ; // 禁用奇偶校验
options.c_cflag &= ~CSTOPB ; // 1位停止位
options.c_cflag &= ~CSIZE ; // 用数据位掩码清空数据位设置
options.c_cflag |= CS8 ; // 设置8位数据位
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ; // 禁用规范输入
options.c_oflag &= ~OPOST ; // 禁用输出处理
options.c_cc [VMIN] = 0 ; // 读取数据的最小字符数
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds) 超时等待时间(十分之一秒100ms)
tcsetattr (fd, TCSANOW, &options) ; // 设置新的串口参数
ioctl (fd, TIOCMGET, &status); // 获取串口控制模式状态
status |= TIOCM_DTR ; // 设置 DTR(数据终端就绪)位
status |= TIOCM_RTS ; // 设置 RTS(请求发送)位
ioctl (fd, TIOCMSET, &status); // 设置串口控制模式状态
usleep (10000) ; // 暂停 10 毫秒
return fd ; // 返回串口文件描述符
}
void my_serialSendstring (const int fd, const char *s)
{
int ret ;
ret = write (fd, s, strlen (s)) ;
if (ret < 0)
printf ("Serial Sendstring Error\n") ;
}
int my_serialGetstring (const int fd, char *buffer)
{
int n_read ;
n_read = read (fd, buffer, 32) ;
return n_read ;
}
#include
#include "uartTool.h"
int fd;
void* readSerial ()
{
char buffer [32] ;
while (1) {
memset (buffer, '\0', sizeof(buffer)) ;
my_serialGetstring (fd, buffer) ;
printf ("GET->%s\n", buffer) ;
}
}
void* sendSerial ()
{
char buffer [32] ;
while (1) {
memset (buffer,'\0', sizeof(buffer)) ;
scanf ("%s", buffer) ;
my_serialSendstring (fd, buffer) ;
}
}
int main (int argc, char **argv)
{
char deviceName [32] = {'\0'} ;
pthread_t readt ;
pthread_t sendt ;
if (argc < 2) {
printf ("uage:%s /dev/ttyS?\n", argv[0]) ;
return -1 ;
}
strcpy (deviceName, argv[1]) ;
if ((fd = my_serialOpen (deviceName, 115200)) == -1) {
printf ("open %s error\n", deviceName) ;
return -1;
}
pthread_create (&readt, NULL, readSerial, NULL) ;
pthread_create (&sendt, NULL, sendSerial, NULL) ;
while (1) {sleep (10);}
}
gcc uartTool.c uartTool.h uartTest.c -lpthread
./a.out /dev/ttyS5