以下是一个用C++编写的简单串口通信程序,使用API函数实现。这个程序可以打开并进行基本的串口通信操作。
#include <iostream>
#include <Windows.h>
// 串口句柄
HANDLE hSerial;
// 打开串口
bool OpenSerialPort(const char* portName) {
hSerial = CreateFileA(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hSerial == INVALID_HANDLE_VALUE) {
std::cout << "Failed to open the serial port!" << std::endl;
return false;
}
// 配置串口参数
DCB dcbSerialParams = { 0 };
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
std::cout << "Failed to get the current serial port parameters!" << std::endl;
CloseHandle(hSerial);
return false;
}
dcbSerialParams.BaudRate = CBR_9600; // 设置波特率
dcbSerialParams.ByteSize = 8; // 设置数据位数
dcbSerialParams.StopBits = ONESTOPBIT; // 设置停止位
dcbSerialParams.Parity = NOPARITY; // 设置校验位
if (!SetCommState(hSerial, &dcbSerialParams)) {
std::cout << "Failed to configure serial port!" << std::endl;
CloseHandle(hSerial);
return false;
}
// 设置读写超时时间
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hSerial, &timeouts)) {
std::cout << "Failed to set serial port timeouts!" << std::endl;
CloseHandle(hSerial);
return false;
}
return true;
}
// 关闭串口
void CloseSerialPort() {
CloseHandle(hSerial);
}
// 发送数据
bool WriteData(const unsigned char* data, unsigned int dataSize) {
DWORD bytesWritten;
if (!WriteFile(hSerial, data, dataSize, &bytesWritten, NULL)) {
std::cout << "Failed to write data to serial port!" << std::endl;
return false;
}
std::cout << "Successfully written " << bytesWritten << " bytes to serial port." << std::endl;
return true;
}
// 读取数据
bool ReadData(unsigned char* buffer, unsigned int bufferSize, unsigned int& bytesRead) {
if (!ReadFile(hSerial, buffer, bufferSize, reinterpret_cast<LPDWORD>(&bytesRead), NULL)) {
std::cout << "Failed to read data from serial port!" << std::endl;
return false;
}
std::cout << "Successfully read " << bytesRead << " bytes from serial port." << std::endl;
return true;
}
int main() {
const char* portName = "COM1";
if (OpenSerialPort(portName)) {
unsigned char data[] = { 0x01, 0x02, 0x03 };
if (WriteData(data, sizeof(data))) {
unsigned char buffer[256];
unsigned int bytesRead;
if (ReadData(buffer, sizeof(buffer), bytesRead)) {
// 处理读取到的数据
for (unsigned int i = 0; i < bytesRead; i++) {
std::cout << "Received data: 0x" << std::hex << static_cast<int>(buffer[i]) << std::dec << std::endl;
}
}
}
CloseSerialPort();
}
return 0;
}
请注意,这只是一个简单的例子,可能需要根据你的具体需求进行修改和扩展。
以下是一个基于C++的串口通信程序的示例:
#include <iostream>
#include <Windows.h>
// 打开串口
HANDLE OpenSerialPort(const char* portName)
{
HANDLE hSerial = CreateFileA(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hSerial == INVALID_HANDLE_VALUE)
{
std::cerr << "Failed to open serial port." << std::endl;
return NULL;
}
DCB dcbSerialParams = { 0 };
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
{
std::cerr << "Failed to get serial port state." << std::endl;
CloseHandle(hSerial);
return NULL;
}
dcbSerialParams.BaudRate = CBR_9600; // 设置波特率为9600
dcbSerialParams.ByteSize = 8; // 设置数据位为8位
dcbSerialParams.StopBits = ONESTOPBIT; // 设置停止位为1位
dcbSerialParams.Parity = NOPARITY; // 设置奇偶校验位为无校验
if (!SetCommState(hSerial, &dcbSerialParams))
{
std::cerr << "Failed to set serial port state." << std::endl;
CloseHandle(hSerial);
return NULL;
}
return hSerial;
}
// 关闭串口
void CloseSerialPort(HANDLE hSerial)
{
CloseHandle(hSerial);
}
// 读取串口数据
bool ReadSerialPort(HANDLE hSerial, char* buffer, int bufferSize, DWORD* bytesRead)
{
if (!ReadFile(hSerial, buffer, bufferSize, bytesRead, NULL))
{
std::cerr << "Failed to read serial port." << std::endl;
return false;
}
return true;
}
// 写入串口数据
bool WriteSerialPort(HANDLE hSerial, const char* data, int dataSize, DWORD* bytesWritten)
{
if (!WriteFile(hSerial, data, dataSize, bytesWritten, NULL))
{
std::cerr << "Failed to write serial port." << std::endl;
return false;
}
return true;
}
int main()
{
const char* portName = "COM1"; // 串口名
HANDLE hSerial = OpenSerialPort(portName);
if (!hSerial)
{
return 1;
}
char buffer[256]; // 读取缓冲区
DWORD bytesRead = 0;
if (!ReadSerialPort(hSerial, buffer, sizeof(buffer), &bytesRead))
{
CloseSerialPort(hSerial);
return 1;
}
std::cout << "Received: " << buffer << std::endl;
const char* sendData = "Hello, serial port!";
DWORD bytesWritten = 0;
if (!WriteSerialPort(hSerial, sendData, strlen(sendData), &bytesWritten))
{
CloseSerialPort(hSerial);
return 1;
}
std::cout << "Sent: " << sendData << std::endl;
CloseSerialPort(hSerial);
return 0;
}
该示例中,OpenSerialPort函数用于打开指定的串口,参数为串口名称字符串。CloseSerialPort函数用于关闭已打开的串口。ReadSerialPort函数用于从串口读取数据,参数为读取缓冲区、缓冲区大小和读取到的字节数。WriteSerialPort函数用于写入数据到串口,参数为要发送的数据、数据大小和写入的字节数。
在主函数中,程序首先调用OpenSerialPort函数打开COM1串口,然后调用ReadSerialPort函数从串口中读取数据并输出。接着,程序使用WriteSerialPort函数向串口发送数据,并输出发送的数据。最后,程序调用CloseSerialPort函数关闭串口。
需要注意的是,本示例仅适用于Windows操作系统。如果在其他操作系统上运行,需要使用相应的串口通信库或方法。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/135232.html