MSComm控件使用详解
Windows编程 2008-04-06 19:15:39 阅读70 评论0字号:大中小
MSComm控件使用详解
MSComm控件的常用属性
1.MSComm1.Settings:=’9600,n,8,1’; //设置波特率 ,校验位,数据位,停止位
Settings属性: 设置串口的波特率 ,校验位,数据位,停止位, ’9600,n,8,1’表示波特率为9600,无奇
偶校验,数据位为8为,1位停止位
MSComm1.InBufferSize:=1024; // 接受缓冲区大小
InBufferSize属性:接收缓冲区的大小,默认值为1024,也可以自己设定,比如,
MSComm1.InBufferSize:=2000,则接收缓冲区的大小为2000字节
MSComm1.OutBufferSize:=1024; // 发送缓冲区大小
OutBufferSize属性:发送缓冲区的大小,默认值为1024,也可以自己设定,比如, MSComm1.OutBufferSize:=2000,则发送缓冲区的大小为2000字节
MSComm1.InBufferCount:=0; //清空接受缓冲区
InBufferCount属性:当前接收缓冲区接收到的数据的长度,count:=MSComm1.InBufferCount, 则count值就是接收缓冲区接收到的数据的长度,对InBufferCount赋值
MSComm1.InBufferCount:=0,可以清空接受缓冲区
MSComm1.OutBufferCount:=0; //清空发送缓冲区
OutBufferCount属性:当前发送缓冲区中数据的长度,对OutBufferCount赋值 MSComm1.
OutBufferCount:=0,可以清空发送缓冲区
MSComm1.InputMode:=comInputModeText;//以文本方式取回数据
MSComm1.InputMode:=comInputModeBinary; //设置接收数据模式为二进制形式
InputMode属性:串口接收数据的模式, comInputModeText(0)表示以文本(ASCII)方式取回数据, comInputModeBinary(1)表示以二进制方式取回数据
comInputModeText, comInputModeBinary为预定义常量,分别表示0,1 RcvByte:=MSComm1.Input
Input属性:通过Input属性可以读取串口中接收到的数据,RcvByte:=MSComm1.Input表示读取串口
接收到的数据 ,其中RcvByte的数据类型为:array of Byte或array of Variant.在读取之前先设置RcvByte的长度:SetLength(RcvByte,len),如果一次读取所有数据,则SetLength(RcvByte, MSComm1.InBufferCount)
MSComm1.Output:=OutputDat
Output属性:通过Output属性可以发送数据,MSComm1.Output:=OutputDat,则将OutputDat中
的数据发送出去,其中OutputDat数据类型为array of Byte,发送前要设置OutputDat的长度, SetLength(OutputDat,len),然后向OutputDat中填入数据,再清空发送缓冲区MSComm1.OutBufferCount:=0,然后再发送MSComm1.Output:=OutputDat
MSComm1.InputLen:=0;////////////// 一次读取所有数据 /////////
InputLen属性:一次从Input属性中读取数据的长度, MSComm1.InputLen:=1,表示一次读取一个字
节,如果MSComm1.InputLen:=0,则表示一次读取全部数据
MSComm1.SThreshold:=0;//一次发送所有数据 ,发送数据时不产生OnComm事件
SThreshold属性:通过该属性设置产生OnComm事件(发送时产生)的阀值,若
MSComm1.SThreshold:=0,则一次发送所有数据 ,发送数据时不产生OnComm事件,若MSComm1.SThreshold:=5,当发送缓冲区的字节数从5字节减少到4字节时,产生OnComm事件
MSComm1.RThreshold:=1;////////////每接收1个字节就产生一个OnComm事件
RThreshold属性: 通过该属性设置产生OnComm事件(接收时产生)的阀值,若
MSComm1.RThreshold:=0,不产生OnComm事件,若MSComm1.RThreshold:=5,接收缓冲区每收到5字节时,则产生OnComm事件
MSComm1.PortOpen:=True; //打开串口
PortOpen属性:设置端口的打开与关闭,打开端口MSComm1.PortOpen:=True,
关闭端口MSComm1.PortOpen:=False
MSComm1.CommPort:=CommPort;
CommPort属性:设置端口号, MSComm1.CommPort:=1,设置端口号为COM1 CommEvent属性:常用的两个comEvReceive, comEvSend
OnComm 常数
常数 值 描述
comEvSend 1 发送事件。 comEvReceive 2 接收事件。
comEvCTS 3 clear-to-send 线变化。 comEvDSR 4 data-set ready 线变化。 comEvCD 5 carrier detect 线变化。 comEvRing 6 振铃检测。 comEvEOF 7 文件结束。 端口初始化设置
procedure TFMonitorCenter.SBtnOpenPortClick(Sender: TObject); var
i,CommPort:integer;
Speed,DataBit,StopBit,CheckBit,SetString:string; begin
CommPort:=1;
Speed:='115200'; DataBit:='8';
CheckBit[1]:='N'; StopBit:='1';
if MSComm1.PortOpen=False then begin
MSComm1.CommPort:=CommPort; //设置端口
SetString:=Speed+','+CheckBit[1]+','+DataBit+','+StopBit; MSComm1.Settings:=SetString; //设置波特率 ,校验位,数据位,停止位 MSComm1.InBufferSize:=1024; // 接受缓冲区大小 MSComm1.OutBufferSize:=1024; // 发送缓冲区大小 MSComm1.InBufferCount:=0; //清空接受缓冲区 MSComm1.OutBufferCount:=0; //清空发送缓冲区
//////// MSComm1.InputMode:=comInputModeText;//以文本方式取回数据 MSComm1.InputMode:=comInputModeBinary; //设置接收数据模式为二进制形式 MSComm1.InputLen:=0;////////////// 一次读取所有数据 /////////
MSComm1.SThreshold:=0;//一次发送所有数据 ,发送数据时不产生OnComm事件 MSComm1.RThreshold:=1;////////////每接收1个字节就产生一个OnComm事件 MSComm1.PortOpen:=True; //打开串口 end; end;
OnComm 事件
procedure TFMonitorCenter.MSComm1Comm(Sender: TObject); var
str:string;
RcvByte: array of Byte; // RcvByte存放缓冲区的数据
i,len,NumOfFlag:integer; //NumOfFlag 为一次OnComm事件中接收到的字符'~'的个数 OldCRCResult,NewCRCResult:WORD; CRCByte:array[0..255] of Byte; begin
if(MSComm1.CommEvent=comEvReceive) then//接收事件
begin //接收缓冲区中是否收到Rthreshold个字符 if(MSComm1.InBufferCount<>0) then //缓冲区有数据 begin
len:=MSComm1.InBufferCount; //缓冲区数据长度 SetLength(RcvByte,len); //设置动态数组的长度 RcvByte:=MSComm1.Input;
......
end;
end;
//*****************************************************// if (MSComm1.CommEvent= comEvSend) then//发送事件 begin ...... end; end;