本章我們將以工業(yè)控制和嵌入式系統(tǒng)中運用極為廣泛的串口通信為例講述多線程的典型應(yīng)用。
而網(wǎng)絡(luò)通信也是多線程應(yīng)用最廣泛的領(lǐng)域之一,所以本章的最后一節(jié)也將對多線程網(wǎng)絡(luò)通信進行簡短的描述。
1.串口通信
在工業(yè)控制系統(tǒng)中,工控機(一般都基于PC Windows平臺)經(jīng)常需要與單片機通過串口進行通信。因此,操作和使用PC的串口成為大多數(shù)單片機、嵌入式系統(tǒng)領(lǐng)域工程師必須具備的能力。
串口的使用需要通過三個步驟來完成的:
(1) 打開通信端口;
(2) 初始化串口,設(shè)置波特率、數(shù)據(jù)位、停止位、奇偶校驗等參數(shù)。為了給讀者一個直觀的印象,下圖從Windows的"控制面板->系統(tǒng)->設(shè)備管理器->通信端口(COM1)"打開COM的設(shè)置窗口:
(3) 讀寫串口。
在WIN32平臺下,對通信端口進行操作跟基本的文件操作一樣。
創(chuàng)建/打開COM資源
下列函數(shù)如果調(diào)用成功,則返回一個標(biāo)識通信端口的句柄,否則返回-1:
HADLE CreateFile(PCTSTR lpFileName, //通信端口名,如"COM1" WORD dwDesiredAccess, //對資源的訪問類型 WORD dwShareMode, //指定共享模式,COM不能共享,該參數(shù)為0 PSECURITY_ATTRIBUTES lpSecurityAttributes, //安全描述符指針,可為NULL WORD dwCreationDisposition, //創(chuàng)建方式 WORD dwFlagsAndAttributes, //文件屬性,可為NULL HANDLE hTemplateFile //模板文件句柄,置為NULL ); |
獲得/設(shè)置COM屬性
下列函數(shù)可以獲得COM口的設(shè)備控制塊,從而獲得相關(guān)參數(shù):
BOOL WINAPI GetCommState( HANDLE hFile, //標(biāo)識通信端口的句柄 LPDCB lpDCB //指向一個設(shè)備控制塊(DCB結(jié)構(gòu))的指針 ); |
如果要調(diào)整通信端口的參數(shù),則需要重新配置設(shè)備控制塊,再用WIN32 API SetCommState()函數(shù)進行設(shè)置:
BOOL SetCommState( HANDLE hFile, //標(biāo)識通信端口的句柄 LPDCB lpDCB //指向一個設(shè)備控制塊(DCB結(jié)構(gòu))的指針 ); |
DCB結(jié)構(gòu)包含了串口的各項參數(shù)設(shè)置,如下:
typedef struct _DCB { // dcb DWORD DCBlength; // sizeof(DCB) DWORD BaudRate; // current baud rate DWORD fBinary: 1; // binary mode, no EOF check DWORD fParity: 1; // enable parity checking DWORD fOutxCtsFlow: 1; // CTS output flow control DWORD fOutxDsrFlow: 1; // DSR output flow control DWORD fDtrControl: 2; // DTR flow control type DWORD fDsrSensitivity: 1; // DSR sensitivity DWORD fTXContinueOnXoff: 1; // XOFF continues Tx DWORD fOutX: 1; // XON/XOFF out flow control DWORD fInX: 1; // XON/XOFF in flow control DWORD fErrorChar: 1; // enable error replacement DWORD fNull: 1; // enable null stripping DWORD fRtsControl: 2; // RTS flow control DWORD fAbortOnError: 1; // abort reads/writes on error DWORD fDummy2: 17; // reserved WORD wReserved; // not currently used WORD XonLim; // transmit XON threshold WORD XoffLim; // transmit XOFF threshold BYTE ByteSize; // number of bits/byte, 4-8 BYTE Parity; // 0-4=no,odd,even,mark,space BYTE StopBits; // 0,1,2 = 1, 1.5, 2 char XonChar; // Tx and Rx XON character char XoffChar; // Tx and Rx XOFF character char ErrorChar; // error replacement character char EofChar; // end of input character char EvtChar; // received event character WORD wReserved1; // reserved; do not use } DCB; |
讀寫串口
在讀寫串口之前,還要用PurgeComm()函數(shù)清空緩沖區(qū),并用SetCommMask ()函數(shù)設(shè)置事件掩模來監(jiān)視指定通信端口上的事件,其原型為:
BOOL SetCommMask( HANDLE hFile, //標(biāo)識通信端口的句柄 DWORD dwEvtMask //能夠使能的通信事件 ); |
串口上可能發(fā)生的事件如下表所示:
值 | 事件描述 |
EV_BREAK | A break was detected on input. |
EV_CTS | The CTS (clear-to-send) signal changed state. |
EV_DSR | The DSR(data-set-ready) signal changed state. |
EV_ERR | A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY. |
EV_RING | A ring indicator was detected. |
EV_RLSD | The RLSD (receive-line-signal-detect) signal changed state. |
EV_RXCHAR | A character was received and placed in the input buffer. |
EV_RXFLAG | The event character was received and placed in the input buffer. The event character is specified in the device''''s DCB structure, which is applied to a serial port by using the SetCommState function. |
EV_TXEMPTY | The last character in the output buffer was sent. |
在設(shè)置好事件掩模后,我們就可以利用WaitCommEvent()函數(shù)來等待串口上發(fā)生事件,其函數(shù)原型為:
BOOL WaitCommEvent( HANDLE hFile, //標(biāo)識通信端口的句柄 LPDWORD lpEvtMask, //指向存放事件標(biāo)識變量的指針 LPOVERLAPPED lpOverlapped, // 指向overlapped結(jié)構(gòu) ); |
我們可以在發(fā)生事件后,根據(jù)相應(yīng)的事件類型,進行串口的讀寫操作:
BOOL ReadFile(HANDLE hFile, //標(biāo)識通信端口的句柄
|
(審核編輯: 智匯小新)
分享