C语言 实现串口收发通信

下面是一个简单的串口收发通信的程序,使用了Windows API函数实现。程序发送了一个包含三个字符的字符串"111",并且在接收到数据后将其打印到控制台上。

#include <windows.h>
#include <stdio.h>

int main()
{
HANDLE hComm; // 串口句柄
char comPort[] = "COM2"; // 串口名称
char writeBuff[] = "111"; // 发送的数据
DWORD bytesWritten, bytesRead; // 发送和接收的字节数
char readBuff[100]; // 接收缓冲区

// 打开串口
hComm = CreateFile(comPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hComm == INVALID_HANDLE_VALUE) {
printf("Failed to open serial port %s\n", comPort);
return 1;
}

// 配置串口
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hComm, &dcbSerialParams)) {
printf("Failed to get serial parameters\n");
CloseHandle(hComm);
return 1;
}
dcbSerialParams.BaudRate = CBR_115200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(hComm, &dcbSerialParams)) {
printf("Failed to set serial parameters\n");
CloseHandle(hComm);
return 1;
}

// 发送数据
if (!WriteFile(hComm, writeBuff, sizeof(writeBuff), &bytesWritten, NULL)) {
printf("Failed to write to serial port\n");
CloseHandle(hComm);
return 1;
}
printf("Sent %d bytes: %s\n", bytesWritten, writeBuff);

// 接收数据
if (!ReadFile(hComm, readBuff, sizeof(readBuff), &bytesRead, NULL)) {
printf("Failed to read from serial port\n");
CloseHandle(hComm);
return 1;
}
printf("Received %d bytes: %s\n", bytesRead, readBuff);

// 关闭串口
CloseHandle(hComm);
return 0;
}

该程序使用了Windows API中的CreateFileReadFileWriteFile等函数实现串口的打开、读写和关闭操作。需要注意的是,串口的配置参数需要在DCB数据结构中进行设置,并且设置完参数之后需要使用SetCommState函数将其应用到串口上。

可以下载虚拟串口助手与程序调试看看: Virtual Serial Port Driver 8.0、和 sscom5.13.1 (我用的是这两个,Virtual Serial Port Driver建立虚拟串口对(比如新建COM2、COM3),sscom可以和你建立的串口实现通信(比如sscom设置成COM3, 程序里面设置成COM2)),如图:
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
RS422通信协议是一种常用的串行通信协议,使用差分信号传输数据,具有高速传输、抗干扰能力强等优点,常用于长距离数据传输。以下是一个简单的C语言实现RS422通信协议的收发示例代码。 首先,需要打开串口设备文件,并进行串口的初始化,示例代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <termios.h> #include <unistd.h> int rs422_init(char *devname, int baudrate) { int fd; struct termios options; fd = open(devname, O_RDWR | O_NOCTTY | O_NDELAY); if(fd < 0) { perror("open failed"); return -1; } fcntl(fd, F_SETFL, 0); tcgetattr(fd, &options); cfsetispeed(&options, baudrate); cfsetospeed(&options, baudrate); options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CRTSCTS; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_iflag &= ~(IXON | IXOFF | IXANY); options.c_iflag &= ~(INLCR | ICRNL | IGNCR); options.c_oflag &= ~(ONLCR | OCRNL); options.c_cc[VTIME] = 0; options.c_cc[VMIN] = 1; tcsetattr(fd, TCSANOW, &options); return fd; } ``` 接下来,可以使用read和write函数进行数据的收发,示例代码如下: ```c int rs422_send(int fd, unsigned char *data, unsigned char len) { int nbytes; nbytes = write(fd, data, len); if(nbytes < 0) { perror("write failed"); return -1; } return 0; } int rs422_receive(int fd, unsigned char *data, unsigned char len) { int nbytes; nbytes = read(fd, data, len); if(nbytes < 0) { perror("read failed"); return -1; } return nbytes; } ``` 需要注意的是,以上代码仅供参考,实际使用时需要根据具体的应用场景进行修改和优化。同时,对于一个完整的RS422通信应用程序,还需要考虑各种异常情况的处理,如错误码的处理、超时的处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值