C++ 串口读写

这段代码演示了如何使用 Windows API 中的串口通信功能进行简单的数据发送和接收。它使用了串口的基本操作和设置,并通过 sendSizeCategory 函数实现了一个简单的串口通信示例,发送一个十六进制数据,并读取串口返回的数据。

_CRT_SECURE_NO_WARNINGS:这是针对使用 strcpy 等不安全的 C 标准库函数的警告定义,表示不生成相关的警告信息。

  • SerialPort 类封装了串口的打开、关闭、读取和写入操作。
  • HANDLE hComm:串口句柄,用于标识打开的串口。
  • DCB dcbSerialParamsCOMMTIMEOUTS timeouts:分别用于设置串口的参数和超时时间。
  • 构造函数初始化了 hCommINVALID_HANDLE_VALUE,并使用 SecureZeroMemory 清空了 dcbSerialParamstimeouts 的内存。
  • 析构函数在对象销毁时调用 close() 方法关闭串口。
  • bool open(const std::string& portName, int baudRate):打开指定端口名和波特率的串口,设置串口参数和超时时间。
  • bool close():关闭串口。
  • bool read(char* buffer, int bufferSize, int& bytesRead):从串口读取数据到指定的缓冲区。
  • bool write(const char* buffer, int bufferSize, int& bytesWritten):向串口写入指定的数据。
  • sendSizeCategory 函数通过串口发送和接收数据:
  • 首先创建一个 SerialPort 对象并打开串口 "COM5",波特率为 19200
  • 将传入的 size_category 字符串按十六进制转换为整数,然后将其作为一个字节写入串口。
  • 读取串口返回的数据到 readBuffer,并将读取的第一个字节返回作为整数。

#define _CRT_SECURE_NO_WARNINGS

#include <cstring>
#include <iostream>
#include <string>
#include <windows.h>
#include <cstring>
#include <sstream>
#include <iomanip>

class SerialPort {
private:
    HANDLE hComm;
    DCB dcbSerialParams;
    COMMTIMEOUTS timeouts;

public:
    SerialPort();
    ~SerialPort();
    bool open(const std::string& portName, int baudRate);
    bool close();
    bool read(char* buffer, int bufferSize, int& bytesRead);
    bool write(const char* buffer, int bufferSize, int& bytesWritten);
};

SerialPort::SerialPort() {
    hComm = INVALID_HANDLE_VALUE;
    SecureZeroMemory(&dcbSerialParams, sizeof(DCB));
    SecureZeroMemory(&timeouts, sizeof(COMMTIMEOUTS));
}

SerialPort::~SerialPort() {
    close();
}

bool SerialPort::open(const std::string& portName, int baudRate) {
    hComm = CreateFileA(portName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hComm == INVALID_HANDLE_VALUE) {
        std::cout << "无法打开串口!" << std::endl;
        return false;
    }
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    if (!GetCommState(hComm, &dcbSerialParams)) {
        std::cout << "获取串口状态失败!" << std::endl;
        close();
        return false;
    }
    dcbSerialParams.BaudRate = baudRate;//设置的波特率、数据位、停止位和校验方式。dcbSerialParams
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;
    if (!SetCommState(hComm, &dcbSerialParams)) {
        std::cout << "设置串口状态失败!" << std::endl;
        close();
        return false;
    }
    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;
    if (!SetCommTimeouts(hComm, &timeouts)) {
        std::cout << "设置串口超时失败!" << std::endl;
        close();
        return false;
    }
    return true;
}

bool SerialPort::close() {
    if (hComm != INVALID_HANDLE_VALUE) {
        CloseHandle(hComm);
        hComm = INVALID_HANDLE_VALUE;
    }
    return true;
}

bool SerialPort::read(char* buffer, int bufferSize, int& bytesRead) {
    return ReadFile(hComm, buffer, bufferSize, (LPDWORD)&bytesRead, NULL);
}

bool SerialPort::write(const char* buffer, int bufferSize, int& bytesWritten) {
    return WriteFile(hComm, buffer, bufferSize, (LPDWORD)&bytesWritten, NULL);
}

int sendSizeCategory(std::string size_category) {
    SerialPort serialPort;
    char readBuffer[256];

    if (serialPort.open("COM5", 19200)) {
        std::cout << "串口打开成功!" << std::endl;

        std::stringstream ss;
        ss << std::hex << size_category;
        int size_category_hex;
        ss >> size_category_hex;

        char writeBuffer[256];
        writeBuffer[0] = static_cast<char>(size_category_hex);

        int bytesWritten;
        serialPort.write(writeBuffer, 1, bytesWritten);
        std::cout << "已发送数据: 0x" << std::hex << static_cast<int>(writeBuffer[0]) << std::endl;

        int bytesRead;
        serialPort.read(readBuffer, sizeof(readBuffer), bytesRead);

        std::cout << "接收到数据: 0x" << std::hex << static_cast<int>(readBuffer[0]) << std::endl;

        serialPort.close();
    }

    return static_cast<int>(readBuffer[0]);
}

在Linux下,可以使用C++中的文件操作和多线程技术实现高并发读写串口。具体实现步骤如下: 1. 打开串口设备文件 使用C++中的文件操作函数`open()`打开串口设备文件,获取文件描述符。例如: ```c++ int fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY); if (fd < 0) { perror("open serial port error!"); return -1; } ``` 其中,`/dev/ttyS0`是串口设备文件路径,`O_RDWR`表示打开文件可读可写,`O_NOCTTY`表示不把串口设备作为控制终端,`O_NDELAY`表示非阻塞模式打开。如果打开失败,使用`perror()`函数输出错误信息并返回。 2. 配置串口属性 使用Linux系统调用函数`tty_ioctl()`配置串口属性,包括波特率、数据位、停止位和校验位等。例如: ```c++ struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cc[VTIME] = 0; options.c_cc[VMIN] = 1; tcsetattr(fd, TCSANOW, &options); ``` 其中,`cfsetispeed()`和`cfsetospeed()`函数设置输入输出波特率,`c_cflag`成员设置串口属性,`VTIME`和`VMIN`成员配置读取等待时间和最小字符数。如果配置失败,使用`perror()`函数输出错误信息并返回。 3. 创建读写线程 使用C++中的多线程技术创建读写线程,分别实现串口数据读取和写入。例如: ```c++ void* read_thread(void* arg) { int fd = *(int*)arg; char buf[1024]; while (1) { int n = read(fd, buf, sizeof(buf)-1); if (n > 0) { buf[n] = '\0'; printf("read data: %s\n", buf); } usleep(10000); } return NULL; } void* write_thread(void* arg) { int fd = *(int*)arg; char buf[1024]; while (1) { fgets(buf, sizeof(buf), stdin); int n = write(fd, buf, strlen(buf)); if (n < 0) { perror("write serial port error!"); break; } } return NULL; } int main() { int fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY); if (fd < 0) { perror("open serial port error!"); return -1; } pthread_t tid1, tid2; pthread_create(&tid1, NULL, read_thread, &fd); pthread_create(&tid2, NULL, write_thread, &fd); pthread_join(tid1, NULL); pthread_join(tid2, NULL); close(fd); return 0; } ``` 其中,`read_thread()`函数实现串口数据读取,`write_thread()`函数实现串口数据的写入。使用`pthread_create()`函数创建两个线程,并传递串口文件描述符作为参数。主线程使用`pthread_join()`函数等待两个线程退出。如果读写失败,使用`perror()`函数输出错误信息并退出。 以上就是Linux C++高并发读写串口的实现方法。需要注意的是,在多线程环境下,需要加锁保证数据的同步和互斥访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值