现在的位置: 首页 > 综合 > 正文

MSCOMM参数设置过程

2013年09月08日 ⁄ 综合 ⁄ 共 2808字 ⁄ 字号 评论关闭

//以下是创建窗体时的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;

抱歉!评论已关闭.