在嵌入式系统、工业控制、通信等领域,串口通信是不可或缺的一部分。串口调试是开发者日常工作中的一项基本技能,掌握好串口调试,可以大大提高工作效率。本文将为你详细讲解串口调试的发送和接收过程,帮助你轻松入门,告别小白烦恼。
1. 串口通信基础
1.1 串口定义
串口,全称为串行通信接口,是一种数据传输方式。它通过串行传输数据,即数据按位顺序传输,每次只传输一位。串口通信广泛应用于嵌入式系统、工业控制、通信等领域。
1.2 串口标准
串口通信遵循一系列标准,如RS-232、RS-485、RS-422等。其中,RS-232是最常用的串口标准。
1.3 串口参数
串口通信涉及多个参数,包括波特率、数据位、停止位、校验位等。以下是对这些参数的简要介绍:
- 波特率:表示每秒钟传输的位数,单位为bps(比特每秒)。
- 数据位:表示每次传输的数据位数,通常为8位。
- 停止位:表示数据传输结束后,用于表示传输结束的位,通常为1位。
- 校验位:用于检测数据传输过程中是否出现错误,通常有奇校验、偶校验和无校验三种。
2. 串口调试工具
在进行串口调试时,需要使用串口调试工具。以下是一些常用的串口调试工具:
- 串口调试助手:一款免费的串口调试工具,功能强大,操作简单。
- PuTTY:一款开源的串口调试工具,支持SSH、Telnet等多种协议。
- 串口监视器:用于实时监视串口数据传输的工具。
3. 串口发送数据
3.1 发送数据流程
- 初始化串口:设置串口参数,如波特率、数据位、停止位、校验位等。
- 打开串口:建立与串口的连接。
- 发送数据:将数据写入串口缓冲区。
- 关闭串口:断开与串口的连接。
3.2 发送数据示例(C语言)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd;
struct termios tty;
fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open");
exit(-1);
}
if(tcgetattr(fd, &tty) != 0) {
perror("tcgetattr");
exit(-1);
}
cfsetospeed(&tty, B9600);
cfsetispeed(&tty, B9600);
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag &= ~CSIZE; // Clear all the size bits, then use one of the statements below
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_lflag &= ~ICANON; // Disable canonical mode
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
if (tcsetattr(fd,TCSANOW,&tty) != 0) {
perror("tcsetattr");
exit(-1);
}
char *data = "Hello, world!";
write(fd, data, strlen(data));
close(fd);
return 0;
}
4. 串口接收数据
4.1 接收数据流程
- 初始化串口:设置串口参数,如波特率、数据位、停止位、校验位等。
- 打开串口:建立与串口的连接。
- 接收数据:从串口缓冲区读取数据。
- 关闭串口:断开与串口的连接。
4.2 接收数据示例(C语言)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd;
struct termios tty;
fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open");
exit(-1);
}
if(tcgetattr(fd, &tty) != 0) {
perror("tcgetattr");
exit(-1);
}
cfsetospeed(&tty, B9600);
cfsetispeed(&tty, B9600);
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag &= ~CSIZE; // Clear all the size bits, then use one of the statements below
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_lflag &= ~ICANON; // Disable canonical mode
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
if (tcsetattr(fd,TCSANOW,&tty) != 0) {
perror("tcsetattr");
exit(-1);
}
char buffer[1024];
int n = read(fd, buffer, sizeof(buffer));
if (n > 0) {
printf("Received: %s\n", buffer);
}
close(fd);
return 0;
}
5. 总结
通过本文的学习,相信你已经对串口调试的发送和接收过程有了较为全面的了解。在实际应用中,你可以根据自己的需求选择合适的串口调试工具和编程语言,进行串口通信的开发。希望本文能帮助你轻松掌握串口调试,告别小白烦恼。
