方法一、采用 axMSComm 控件
添加COM组件 Microsoft Communication Control 组件,自动生成axMSComm类,该类包含的属性、方法大致为:
属性:
PortOpen 端口是否打开
CommPort 端口号
Settings 通信参数设置
InputMode 接收数据的类型
RThreshold (产生事件的模式)
OnComm 事件\代理
SThreshold (发送缓冲相关)
RTSEnable 是否启用请求发送
DTREnable 是否接收就绪
InputLen
NullDiscard
Output
Input
通信方式RS232串口,C#程序为:
if (this.axMSComm1.PortOpen == true)
{
{
this.axMSComm1.PortOpen = false;
this.axMSComm1.CommPort = 1;//端口
this.axMSComm1.Settings = "9600,n,8,1";//通信参数
this.axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;//接收数据的类型
this.axMSComm1.RThreshold = 1; //接收缓冲区中接收到一个字符,就产生一次OnComm事件
//this.axMSComm1.OnComm += new EventHandler(axMSComm1_OnComm); //注册一个OnComm事件
this.axMSComm1.SThreshold = 0;//当发送缓冲区中字符数小于该值时,MSComm控件设置Commevent属性为ComEvSend,并产生OnComm事件。若Sthreshold属性设置为0,则不产生OnComm事件
this.axMSComm1.RTSEnable = true;//启用请求发送
this.axMSComm1.DTREnable = true;//接收就绪
this.axMSComm1.InputLen = 0;//一次从接收缓冲区中读取字节数,// 使用Input時為讀取全部資料
this.axMSComm1.CommPort = 1;//端口
this.axMSComm1.Settings = "9600,n,8,1";//通信参数
this.axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;//接收数据的类型
this.axMSComm1.RThreshold = 1; //接收缓冲区中接收到一个字符,就产生一次OnComm事件
//this.axMSComm1.OnComm += new EventHandler(axMSComm1_OnComm); //注册一个OnComm事件
this.axMSComm1.SThreshold = 0;//当发送缓冲区中字符数小于该值时,MSComm控件设置Commevent属性为ComEvSend,并产生OnComm事件。若Sthreshold属性设置为0,则不产生OnComm事件
this.axMSComm1.RTSEnable = true;//启用请求发送
this.axMSComm1.DTREnable = true;//接收就绪
this.axMSComm1.InputLen = 0;//一次从接收缓冲区中读取字节数,// 使用Input時為讀取全部資料