在Delphi中使用串口控件MSComm的0字符接收例程祥解 //以下是创建窗体时的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字符的难关~~~...