基于linux使用C语言写一个可以调节波特率的串口驱动; 写一个调用此驱动的应用程序,改变波特率时显示打印
时间: 2023-12-14 13:38:58 浏览: 33
以下是基于Linux使用C语言写一个可以调节波特率的串口驱动的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int set_interface_attribs(int fd, int speed, int parity)
{
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0)
{
fprintf(stderr, "error %d from tcgetattr", errno);
return -1;
}
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // ignore break signal
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0)
{
fprintf(stderr, "error %d from tcsetattr", errno);
return -1;
}
return 0;
}
void set_blocking(int fd, int should_block)
{
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0)
{
fprintf(stderr, "error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr(fd, TCSANOW, &tty) != 0)
fprintf(stderr, "error %d setting term attributes", errno);
}
int main()
{
const char *portname = "/dev/ttyUSB0";
int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
fprintf(stderr, "error %d opening %s: %s", errno, portname, strerror(errno));
return -1;
}
set_interface_attribs(fd, B9600, 0); // set speed to 9600 bps, 8n1 (no parity)
set_blocking(fd, 0); // set no blocking
// Change baudrate to 115200 bps
set_interface_attribs(fd, B115200, 0);
printf("Baudrate changed to 115200 bps\n");
close(fd);
return 0;
}
```
上面的代码中,`set_interface_attribs()` 函数用于设置串口的波特率、校验位等参数,`set_blocking()` 函数用于设置读写操作是否阻塞。在 `main()` 函数中,首先打开串口设备文件,然后调用 `set_interface_attribs()` 函数设置初始的波特率(这里设置为 9600 bps),接着调用 `set_interface_attribs()` 函数再次设置波特率(这里设置为 115200 bps),最后关闭串口设备文件。
下面是一个调用这个串口驱动的示例应用程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int set_interface_attribs(int fd, int speed, int parity)
{
// ...
}
void set_blocking(int fd, int should_block)
{
// ...
}
int main()
{
const char *portname = "/dev/ttyUSB0";
int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
fprintf(stderr, "error %d opening %s: %s", errno, portname, strerror(errno));
return -1;
}
set_interface_attribs(fd, B9600, 0); // set speed to 9600 bps, 8n1 (no parity)
set_blocking(fd, 0); // set no blocking
char buffer[255];
int n;
while (1)
{
n = read(fd, buffer, sizeof(buffer));
if (n > 0)
{
printf("Received %d bytes: %s\n", n, buffer);
}
// Change baudrate to 115200 bps
set_interface_attribs(fd, B115200, 0);
printf("Baudrate changed to 115200 bps\n");
// Wait for a while
sleep(5);
// Change baudrate back to 9600 bps
set_interface_attribs(fd, B9600, 0);
printf("Baudrate changed back to 9600 bps\n");
// Wait for a while
sleep(5);
}
close(fd);
return 0;
}
```
这个应用程序不断读取串口数据并显示,然后每隔 5 秒钟改变一次波特率(先改为 115200 bps,再改回 9600 bps)。在控制台输出中可以看到波特率的变化情况。