一、cserialport簡介
cserialport是一個基於C++語言編寫的串口通信庫,可以用於Windows、Linux和Mac OS X操作系統。它提供了簡單易用的介面,可以方便地實現串口通信功能。cserialport支持波特率、數據位、停止位、校驗位等常見串口參數的設置,同時支持同步和非同步模式的數據傳輸。在使用cserialport之前,需要先安裝相關的驅動和開發環境。
二、cserialport的安裝
在Windows操作系統中,可以從cserialport官方網站(https://github.com/manashmndl/cserialport)下載最新版的安裝程序,直接運行即可。在Linux和Mac OS X操作系統中,可以通過源代碼編譯和安裝cserialport。
// Linux系統下編譯和安裝cserialport $ tar zxvf cserialport-1.0.tar.gz $ cd cserialport-1.0 $ ./configure $ make $ sudo make install
在安裝完成後,可以在開發環境中引入相關頭文件和鏈接庫文件,就可以開始使用cserialport了。
三、cserialport的使用
1. 打開和關閉串口
cserialport提供了serialport類,可以通過該類的成員函數打開和關閉串口。
#include using namespace std; int main() { // 創建serialport對象 CSerialPort sp; // 打開串口 if (sp.Open("[SERIAL_PORT_NAME]")) { printf("Serial port opened.\n"); } else { printf("Failed to open serial port.\n"); } // 關閉串口 sp.Close(); printf("Serial port closed.\n"); return 0; }
其中,[SERIAL_PORT_NAME]是串口的名稱,如Windows系統下的COM1、Linux系統下的/dev/ttyS0等。
2. 設置串口參數
cserialport提供了SetBaudRate()、SetDataBits()、SetStopBits()和SetParity()等成員函數,可以設置串口的波特率、數據位、停止位和校驗位等參數。
#include using namespace std; int main() { // 創建serialport對象 CSerialPort sp; // 打開串口 if (sp.Open("[SERIAL_PORT_NAME]")) { printf("Serial port opened.\n"); // 設置串口參數 sp.SetBaudRate(CBR_9600); // 波特率9600 sp.SetDataBits(8); // 數據位8 sp.SetStopBits(STOPBITS_1); // 停止位1 sp.SetParity(NOPARITY); // 無校驗位 } else { printf("Failed to open serial port.\n"); } // 關閉串口 sp.Close(); printf("Serial port closed.\n"); return 0; }
3. 同步發送數據
通過WriteData()函數,可以在同步模式下發送數據。
#include using namespace std; int main() { // 創建serialport對象 CSerialPort sp; // 打開串口 if (sp.Open("[SERIAL_PORT_NAME]")) { printf("Serial port opened.\n"); // 設置串口參數 sp.SetBaudRate(CBR_9600); // 波特率9600 sp.SetDataBits(8); // 數據位8 sp.SetStopBits(STOPBITS_1); // 停止位1 sp.SetParity(NOPARITY); // 無校驗位 // 發送數據 char data[] = "Hello, world!"; DWORD dataSize = strlen(data); DWORD bytesWritten = 0; if (sp.WriteData(data, dataSize, bytesWritten)) { printf("%d bytes written.\n", bytesWritten); } else { printf("Failed to write data.\n"); } } else { printf("Failed to open serial port.\n"); } // 關閉串口 sp.Close(); printf("Serial port closed.\n"); return 0; }
4. 同步接收數據
通過ReadData()函數,可以在同步模式下接收數據。
#include using namespace std; int main() { // 創建serialport對象 CSerialPort sp; // 打開串口 if (sp.Open("[SERIAL_PORT_NAME]")) { printf("Serial port opened.\n"); // 設置串口參數 sp.SetBaudRate(CBR_9600); // 波特率9600 sp.SetDataBits(8); // 數據位8 sp.SetStopBits(STOPBITS_1); // 停止位1 sp.SetParity(NOPARITY); // 無校驗位 // 接收數據 char buf[256] = {0}; DWORD bytesRead = 0; if (sp.ReadData(buf, sizeof(buf) - 1, bytesRead)) { printf("%d bytes read: %s\n", bytesRead, buf); } else { printf("Failed to read data.\n"); } } else { printf("Failed to open serial port.\n"); } // 關閉串口 sp.Close(); printf("Serial port closed.\n"); return 0; }
5. 非同步發送數據
通過StartAsyncWrite()和StopAsyncWrite()函數,可以在非同步模式下發送數據。
#include using namespace std; void AsyncWriteCallback(DWORD bytesWritten, void* userData) { printf("%d bytes written.\n", bytesWritten); } int main() { // 創建serialport對象 CSerialPort sp; // 打開串口 if (sp.Open("[SERIAL_PORT_NAME]")) { printf("Serial port opened.\n"); // 設置串口參數 sp.SetBaudRate(CBR_9600); // 波特率9600 sp.SetDataBits(8); // 數據位8 sp.SetStopBits(STOPBITS_1); // 停止位1 sp.SetParity(NOPARITY); // 無校驗位 // 非同步發送數據 char data[] = "Hello, world!"; DWORD dataSize = strlen(data); if (sp.StartAsyncWrite(data, dataSize, AsyncWriteCallback, NULL)) { Sleep(1000); // 停止非同步發送數據 sp.StopAsyncWrite(); } else { printf("Failed to start async write.\n"); } } else { printf("Failed to open serial port.\n"); } // 關閉串口 sp.Close(); printf("Serial port closed.\n"); return 0; }
非同步發送數據需要指定回調函數AsyncWriteCallback(),回調函數會在數據發送完成時自動調用。
6. 非同步接收數據
通過StartAsyncRead()和StopAsyncRead()函數,可以在非同步模式下接收數據。
#include using namespace std; void AsyncReadCallback(char* buf, DWORD bytesRead, void* userData) { printf("%d bytes read: %s\n", bytesRead, buf); } int main() { // 創建serialport對象 CSerialPort sp; // 打開串口 if (sp.Open("[SERIAL_PORT_NAME]")) { printf("Serial port opened.\n"); // 設置串口參數 sp.SetBaudRate(CBR_9600); // 波特率9600 sp.SetDataBits(8); // 數據位8 sp.SetStopBits(STOPBITS_1); // 停止位1 sp.SetParity(NOPARITY); // 無校驗位 // 非同步接收數據 char buf[256] = {0}; if (sp.StartAsyncRead(buf, sizeof(buf) - 1, AsyncReadCallback, NULL)) { Sleep(1000); // 停止非同步接收數據 sp.StopAsyncRead(); } else { printf("Failed to start async read.\n"); } } else { printf("Failed to open serial port.\n"); } // 關閉串口 sp.Close(); printf("Serial port closed.\n"); return 0; }
非同步接收數據需要指定回調函數AsyncReadCallback(),回調函數會在數據接收完成時自動調用。
原創文章,作者:EIGMK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/331960.html