本章我们将以工业控制和嵌入式系统中运用极为广泛的串口通信为例讲述多线程的典型应用。
而网络通信也是多线程应用最广泛的领域之一,所以本章的最后一节也将对多线程网络通信进行简短的描述。
1.串口通信
在工业控制系统中,工控机(一般都基于pc windows平台)经常需要与单片机通过串口进行通信。因此,操作和使用pc的串口成为大多数单片机、嵌入式系统领域工程师必须具备的能力。
串口的使用需要通过三个步骤来完成的:
(1) 打开通信端口;
(2) 初始化串口,设置波特率、数据位、停止位、奇偶校验等参数。为了给读者一个直观的印象,下图从windows的"控制面板->系统->设备管理器->通信端口(com1)"打开com的设置窗口:
| hadle createfile(pctstr lpfilename, //通信端口名,如"com1" word dwdesiredaccess, //对资源的访问类型 word dwsharemode, //指定共享模式,com不能共享,该参数为0 psecurity_attributes lpsecurityattributes, //安全描述符指针,可为null word dwcreationdisposition, //创建方式 word dwflagsandattributes, //文件属性,可为null handle htemplatefile //模板文件句柄,置为null ); |
| bool winapi getcommstate( handle hfile, //标识通信端口的句柄 lpdcb lpdcb //指向一个设备控制块(dcb结构)的指针 ); |
| bool setcommstate( handle hfile, //标识通信端口的句柄 lpdcb lpdcb //指向一个设备控制块(dcb结构)的指针 ); |
| 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; |
| bool setcommmask( handle hfile, //标识通信端口的句柄 dword dwevtmask //能够使能的通信事件 ); |
| 值 | 事件描述 |
| 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. |
| bool waitcommevent( handle hfile, //标识通信端口的句柄 lpdword lpevtmask, //指向存放事件标识变量的指针 lpoverlapped lpoverlapped, // 指向overlapped结构 ); |
| bool readfile(handle hfile, //标识通信端口的句柄 lpvoid lpbuffer, //输入数据buffer指针 dword nnumberofbytestoread, // 需要读取的字节数 lpdword lpnumberofbytesread, //实际读取的字节数指针 lpoverlapped lpoverlapped //指向overlapped结构 ); bool writefile(handle hfile, //标识通信端口的句柄 lpcvoid lpbuffer, //输出数据buffer指针 dword nnumberofbytestowrite, //需要写的字节数 lpdword lpnumberofbyteswritten, //实际写入的字节数指针 lpoverlapped lpoverlapped //指向overlapped结构 ); |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器