选择显示字体大小

深入浅出win32多线程程序设计之综合实例


  本章我们将以工业控制和嵌入式系统中运用极为广泛的串口通信为例讲述多线程的典型应用。

  而网络通信也是多线程应用最广泛的领域之一,所以本章的最后一节也将对多线程网络通信进行简短的描述。

  1.串口通信

  在工业控制系统中,工控机(一般都基于pc windows平台)经常需要与单片机通过串口进行通信。因此,操作和使用pc的串口成为大多数单片机、嵌入式系统领域工程师必须具备的能力。

  串口的使用需要通过三个步骤来完成的:

  (1) 打开通信端口;

  (2) 初始化串口,设置波特率、数据位、停止位、奇偶校验等参数。为了给读者一个直观的印象,下图从windows的"控制面板->系统->设备管理器->通信端口(com1)"打开com的设置窗口:



  (3) 读写串口。

  在win32平台下,对通信端口进行操作跟基本的文件操作一样。

  创建/打开com资源

  下列函数如果调用成功,则返回一个标识通信端口的句柄,否则返回-1:

hadle createfile(pctstr lpfilename, //通信端口名,如"com1"
word dwdesiredaccess, //对资源的访问类型
word dwsharemode, //指定共享模式,com不能共享,该参数为0
psecurity_attributes lpsecurityattributes,
//安全描述符指针,可为null
word dwcreationdisposition, //创建方式
word dwflagsandattributes, //文件属性,可为null
handle htemplatefile //模板文件句柄,置为null
);

  获得/设置com属性

  下列函数可以获得com口的设备控制块,从而获得相关参数:

bool winapi getcommstate(
 handle hfile, //标识通信端口的句柄
 lpdcb lpdcb //指向一个设备控制块(dcb结构)的指针
);

  如果要调整通信端口的参数,则需要重新配置设备控制块,再用win32 api setcommstate()函数进行设置:

bool setcommstate(
 handle hfile, //标识通信端口的句柄
 lpdcb lpdcb //指向一个设备控制块(dcb结构)的指针
);

  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;

  读写串口

  在读写串口之前,还要用purgecomm()函数清空缓冲区,并用setcommmask ()函数设置事件掩模来监视指定通信端口上的事件,其原型为:

bool setcommmask(
 handle hfile, //标识通信端口的句柄
 dword dwevtmask //能够使能的通信事件
);

  串口上可能发生的事件如下表所示:

事件描述
ev_breaka break was detected on input.
ev_cts the cts (clear-to-send) signal changed state.
ev_dsrthe dsr(data-set-ready) signal changed state.
ev_erra 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_rxchara 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_txemptythe last character in the output buffer was sent.

  在设置好事件掩模后,我们就可以利用waitcommevent()函数来等待串口上发生事件,其函数原型为:

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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons