查看: 5570|回复: 0

在Delphi中使用串口控件MSComm的0字符接收例程祥解

[复制链接]
发表于 2009-4-2 23:50:54 | 显示全部楼层 |阅读模式
关键词: Delphi , MSComm , 串口 , 控件 , 例程
菜农HotPower@126.com 发表于 2006/12/28 22:55:20
//以下是创建窗体时的MSCOMM参数设置过程
//MSComm1.InputMode := comInputModeBinary;
//和MSComm1.InputMode := comInputModeText;
//实验结果基本对Delghi不太起作用

procedure TForm1.FormCreate(Sender: TObject);
var
  str: string;
begin
//MSCOMM参数设置
  MSComm1.CommPort := 1;//使用COM1
  MSComm1.Settings := '9600,N,8,1';//设置通信口参数
  MSComm1.InBufferSize := 32;//设置MSComm1接收缓冲区为32字节
  MSComm1.OutBufferSize := 2;//设置MSComm1发送缓冲区为2字节
  MSComm1.InputMode := comInputModeBinary;//设置接收数据模式为二进制形式
  MSComm1.InputLen := 1;//设置Input 一次从接收缓冲读取字节数为1
  MSComm1.SThreshold := 1;//设置Output 一次从发送缓冲读取字节数为1
  MSComm1.InBufferCount := 0;//清除接收缓冲区
  MSComm1.OutBufferCount := 0;//清除发送缓冲区
  MSComm1.RThreshold := 1;//设置接收一个字节产生OnComm事件
  MSComm1.PortOpen := true;//打开串口1
/////////////////////////////////////////////////////////////
  Buffers := '';
  CheckSum := 0;
//发送串口命令
  Command := 34;
  str := '$' + #2 + #$22 + #1;//读MP3总曲目
  str := str + Char(GetCheckSum(str));//计算校验和
  MSComm1.Output := str;//发送串口命令
end;

//以下是接收事件处理过程,在MCU中相当于串口中断
//注意其中2个语句
//while MSComm1.InBufferCount > 0 do//输入FiFO不为空
//if str = '' then str := #0; //0字符处理
//例接收的数据为#24#02#00#01#03
//此时InBufferCount=5.若设置Input 一次从接收缓冲读取字节数不限
//即:MSComm1.InputLen := 0;则str := MSComm1.Input;后str好象为#24#02#00#01#03
//但实际为'??'#24#02.总得不到结果,至少0字符后的#01#03无法读出.
//采用MSComm1.InputLen := 1;后,并配合while MSComm1.InBufferCount > 0 do
//当读到0字符时,由于str=''(空),故访问str[1]将会引发异常的发生而导致程序的终止.
//故用if str = '' then str := #0; 来向str[1]内认为地填入字符#0且str的长度也为1了.
//故此要点是用if str = '' then str := #0;语句渡过读0字符的难关~~~
procedure TForm1.MSComm1Comm(Sender: TObject);
var
  str: string;
  i: integer;
begin
  case MSComm1.CommEvent of
    comEvReceive://接收事件处理
      begin
        while MSComm1.InBufferCount > 0 do//输入FiFO不为空
        begin
          str := MSComm1.Input;//从FIFO中只取1个字符,因为MSComm1.InputLen := 1
          if str = '' then str := #0; //0字符处理
          if (Buffers = '') and (str = '$') then//同步符测试
          begin
            Buffers := str;//存入同步符'$'
            CheckSum := 0;//初始化校验和
          end
          else if (Buffers <> '') and (Buffers[1] = '$') then begin//必须用同步符起始
            Buffers := Buffers + str;//加入数据串
            CheckSum := CheckSum xor Byte(str[1]);//求校验和(除同步符'$'外)
            if Length(Buffers) = Byte(Buffers[2]) + 3 then//结束符测试
            begin
              if CheckSum = 0 then//此时校验和必须为0表示校验和正确
              begin
                case Command of
                  $22: begin//取歌曲总数
                        ComboBox1.Items.Clear;
                        for i := 1 to Byte(Buffers[4]) do
                        begin
                          str := '第' + inttostr(i) + '首歌曲';
                          ComboBox1.Items.Add(str);//
                        end;
                        Command := 0;
                      end;
                  1: ;
                else ;
                end;
              end;
              Buffers := '';//接收完毕清空缓冲区
              CheckSum := 0;//初始化校验和
            end;
          end
          else
          begin
            Buffers := '';//接收错误清空缓冲区,放弃所有数据
            CheckSum := 0;//初始化校验和
          end;
        end;
      end;
  end;
end;
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表