VC实现串口通信例程 作者:阮帮秋[1]

[入库:2005年8月18日] [更新:2007年3月27日]

本文简介:选择自 candycat 的 blog

vc实现串口通信例程 作者:阮帮秋 发布时间:2001/04/19
 
文章摘要:
  win95界面下的vc++串口通讯程序在win32下是不建议对端口进行操作的,在win32中所有的设备都被看成是文件,串行口也不例外也是作为文件来进行处理的。
  关键词
 串行口,dword,缓冲区
       

正文:  


vc实现串口通信例程 


  win95界面下的vc++串口通讯程序在win32下是不建议对端口进行操作的,在win32中所有的设备都被看成是文件,串行口也不例外也是作为文件来进行处理的。这是我的一份关于串口编程的读书笔记,对于使 用vc进行编程的同行应该有一定的帮助。

1.打开串口:

  在window 95下串行口作为文件处理,使用文件操作对串行口进行处理。使用createfile()打开串口,createfile()将返回串口的句柄。
  handle createfile(
  lpctstr lpfilename, // pointer to name of the file
  dword dwdesiredaccess, // access (read-write) mode
  dword dwsharemode, // share mode
  lpsecurity_attributes lpsecurityattributes, // pointer to security attributes
  dword dwcreationdistribution, // how to create
  dword dwflagsandattributes, // file attributes
  handle htemplatefile // handle to file with attributes to copy
  );
  lpfilename: 指明串口制备,例:com1,com2
  dwdesiredaccess: 指明串口存取方式,例:generic_read|generic_write
  dwsharemode: 指明串口共享方式
  lpsecurityattributes: 指明串口的安全属性结构,null为缺省安全属性
  dwcreateiondistribution: 必须为open_existin
  dwflagandattributes: 对串口唯一有意义的是file_flag_overlapped
  htemplatefile: 必须为null

2.关闭串口:

  closehandle(hcommdev);

3.设置缓冲区长度:

  bool setupcomm(
  handle hfile, // handle of communications device
  dword dwinqueue, // size of input buffer
  dword dwoutqueue // size of output buffer
  );

4.commprop结构:

  可使用getcommproperties()取得commprop结构,commprop结构中记载了系统支持的各项设置。
  typedef struct _commprop { // cmmp
  word wpacketlength; // packet size, in bytes
  word wpacketversion; // packet version
  dword dwservicemask; // services implemented
  dword dwreserved1; // reserved
  dword dwmaxtxqueue; // max tx bufsize, in bytes
  dword dwmaxrxqueue; // max rx bufsize, in bytes
  dword dwmaxbaud; // max baud rate, in bps
  dword dwprovsubtype; // specific provider type
  dword dwprovcapabilities; // capabilities supported
  dword dwsettableparams; // changeable parameters
  dword dwsettablebaud; // allowable baud rates
  word wsettabledata; // allowable byte sizes
  word wsettablestopparity; // stop bits/parity allowed
  dword dwcurrenttxqueue; // tx buffer size, in bytes
  dword dwcurrentrxqueue; // rx buffer size, in bytes
  dword dwprovspec1; // provider-specific data
  dword dwprovspec2; // provider-specific data
  wchar wcprovchar[1]; // provider-specific data
  } commprop;
  dwmaxbaud:
  baud_075 75 bps
  baud_110 110 bps
  baud_134_5 134.5 bps
  baud_150 150 bps
  baud_300 300 bps
  baud_600 600 bps
  baud_1200 1200 bps
  baud_1800 1800 bps
  baud_2400 2400 bps
  baud_4800 4800 bps
  baud_7200 7200 bps
  baud_9600 9600 bps
  baud_14400 14400 bps
  baud_19200 19200 bps
  baud_38400 38400 bps
  baud_56k 56k bps
  baud_57600 57600 bps
  baud_115200 115200 bps
  baud_128k 128k bps
  baud_user programmable baud rates available
  dwprovsubtype:
  pst_fax 传真设备
  pst_lat lat协议
  pst_modem 调制解调器设备
  pst_network_bridge 未指定的网桥
  pst_parallelport 并口
  pst_rs232 rs-232口
  pst_rs422 rs-422口
  pst_rs423 rs-432口
  pst_rs449 rs-449口
  pst_scanner 扫描仪设备
  pst_tcpip_telnet tcp/ip telnet协议
  pst_unspecified 未指定
  pst_x25 x.25标准
  dwprovcapabilities
  pcf_16bitmode 支持特殊的16位模式
  pcf_dtrdsr 支持dtr(数据终端就绪)/dsr(数据设备就绪)
  pcf_inttimeouts 支持区间超时
  pcf_parity_check 支持奇偶校验
  pcf_rlsd 支持rlsd(接收线信号检测)
  pcf_rtscts 支持rts(请求发送)/cts(清除发送)
  pcf_setxchar 支持可设置的xon/xoff
  pcf_specialchars 支持特殊字符
  pcf_totaltimeouts 支持总(占用时间)超时
  pcf_xonxoff 支持xon/xoff流控制
  标准rs-232和window支持除pcf_16bitmode和pcf_specialchar外的所有功能
  dwsettableparams
  sp_baud 可配置波特率
  sp_databits 可配置数据位个数
  sp_handshaking 可配置握手(流控制)
  sp_parity 可配置奇偶校验模式
  sp_parity_check 可配置奇偶校验允许/禁止
  sp_rlsd 可配置rlsd(接收信号检测)
  sp_stopbits 可配置停止位个数
  标准rs-232和window支持以上所有功能
  wsettabledata
  databits_5 5个数据位
  databits_6 6个数据位
  databits_7 7个数据位
  databits_8 8个数据位
  databits_16 16个数据位
  databits_16x 通过串行硬件线路的特殊宽度路径
  windows 95支持16的所有设置

5.dcb结构:

  typedef struct _dcb {// dcb
  dword dcblength; // sizeof(dcb)
  dword baudrate; // current baud rate
  指定当前的波特率
  dword fbinary: 1; // binary mode, no eof check
  指定是否允许二进制模式,
  windows 95中必须为true
  dword fparity: 1; // enable parity checking
  指定奇偶校验是否允许
  dword foutxctsflow:1; // cts output flow control
  指定cts是否用于检测发送控制。
  当为true是cts为off,发送将被挂起。
  dword foutxdsrflow:1; // dsr output flow control
  指定cts是否用于检测发送控制。
  当为true是cts为off,发送将被挂起。
  dword fdtrcontrol:2; // dtr flow control type
  dtr_control_disable值将dtr置为off, dtr_control_enable值将dtr置为on, dtr_control_handshake允许dtr"握手",dword fdsrsensitivity:1; // dsr sensitivity 当该值为true时dsr为off时接收的字节被忽略
  dword ftxcontinueonxoff:1; // xoff continues tx
  指定当接收缓冲区已满,并且驱动程序已经发
  送出xoffchar字符时发送是否停止。
  true时,在接收缓冲区接收到缓冲区已满的字节xofflim且驱动程序已经发送出xoffchar字符中止接收字节之后,发送继续进行。
  false时,在接收缓冲区接收到代表缓冲区已空的字节xonchar且驱动程序已经发送出恢复发送的xonchar之后,发送继续进行。
  dword foutx: 1; // xon/xoff out flow control
  true时,接收到xoffchar之后便停止发送
  接收到xonchar之后将重新开始
  dword finx: 1; // xon/xoff in flow control
  true时,接收缓冲区接收到代表缓冲区满的xofflim之后,xoffchar发送出去
  接收缓冲区接收到代表缓冲区空的xonlim之后,xonchar发送出去
  dword ferrorchar: 1; // enable error replacement
  该值为true且fparity为true时,用errorchar 成员指定的字符代替奇偶校验错误的接收字符
  dword fnull: 1; // enable null stripping
  true时,接收时去掉空(0值)字节
  dword frtscontrol:2; // rts flow control
  rts_control_disable时,rts置为off
  rts_control_enable时, rts置为on
  rts_control_handshake时,
  当接收缓冲区小于半满时rts为on
  当接收缓冲区超过四分之三满时rts为off
  rts_control_toggle时,
  当接收缓冲区仍有剩余字节时rts为on ,否则缺省为off
  dword fabortonerror:1; // abort reads/writes on error
  true时,有错误发生时中止读和写操作
  dword fdummy2:17; // reserved
  未使用
  word wreserved; // not currently used
  未使用,必须为0
  word xonlim; // transmit xon threshold
  指定在xon字符发送这前接收缓冲区中可允许的最小字节数
  word xofflim; // transmit xoff threshold
  指定在xoff字符发送这前接收缓冲区中可允许的最小字节数
  byte bytesize; // number of bits/byte, 4-8
  指定端口当前使用的数据位
  byte parity; // 0-4=no,odd,even,mark,space
  指定端口当前使用的奇偶校验方法,可能为:
  evenparity,markparity,noparity,oddparity
  byte stopbits; // 0,1,2 = 1, 1.5, 2
  指定端口当前使用的停止位数,可能为:
  onestopbit,one5stopbits,twostopbits
  char xonchar; // tx and rx xon character
  指定用于发送和接收字符xon的值
  char xoffchar; // tx and rx xoff character
  指定用于发送和接收字符xoff值
  char errorchar; // error replacement character
  本字符用来代替接收到的奇偶校验发生错误时的值
  char eofchar; // end of input character
  当没有使用二进制模式时,本字符可用来指示数据的结束
  char evtchar; // received event character
  当接收到此字符时,会产生一个事件
  word wreserved1; // reserved; do not use 未使用
  } dcb;

6.改变端口设置

  使用如下的两个方法
  bool getcommstate(hcomm,&dcb);
  bool setcommstate(hcomm,&dcb);

7.改变普通设置

  buildcommdcb(szsettings,&dcb);
  szsettings的格式:baud parity data stop
  例: "baud=96 parity=n data=8 stop=1"
  简写:"96;,n,8,1"
  szsettings 的有效值
  baud:
  11 or 110 = 110 bps
  15 or 150 = 150 bps
  30 or 300 = 300 bps
  60 or 600 = 600 bps
  12 or 1200 = 1200 bps
  24 or 2400 = 2400 bps
  48 or 4800 = 4800 bps
  96 or 9600 = 9600 bps
  19 or 19200= 19200bps
  parity:
  n=none
  e=even
  o=odd
  m=mark
  s=space
  data:
  5,6,7,8
  stopbit
  1,1.5,2

8.commconfig结构:

  typedef struct _comm_config {
  dword dwsize;
  word wversion;
  word wreserved;
  dcb dcb;
  dword dwprovidersubtype;
  dword dwprovideroffset;
  dword dwprovidersize;
  wchar wcproviderdata[1];
  } commconfig, *lpcommconfig;
  可方便的使用bool commconfigdialog(
  lptstr lpszname,
  hwnd hwnd,
  lpcommconfig lpcc);
  来设置串行口。

9.超时设置:

  可通过commtimeouts结构设置超时,
  typedef struct _commtimeouts {
  dword readintervaltimeout;

本文关键:,串行口,DWORD,缓冲区,
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top