Detailed explanation of the 0 character receiving routine using the serial port control MSComm in Delphi

Publisher:czm721002Latest update time:2015-01-23 Source: laoguKeywords:Delphi  MSComm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//The following is the MSCOMM parameter setting process when creating a form

//MSComm1.InputMode := comInputModeBinary;

// and MSComm1.InputMode := comInputModeText;

//The experimental results basically have little effect on Delghi


procedure TForm1.FormCreate(Sender: TObject);
var
  str: string;
begin
//MSCOMM parameter settingsMSComm1.CommPort
  := 1;//Use COM1
  MSComm1.Settings := ''9600,N,8,1'';//Set communication port parametersMSComm1.InBufferSize
  := 32;//Set MSComm1 receive buffer to 32 bytesMSComm1.OutBufferSize
  := 2;//Set MSComm1 send buffer to 2
  bytesMSComm1.InputMode := comInputModeBinary;//Set the receive data mode to binary formMSComm1.InputLen
  := 1;//Set the number of bytes read from the receive buffer at one time for Input to be 1
  MSComm1.SThreshold := 1;//Set the number of bytes read from the send buffer at one time for Output to be 1
  MSComm1.InBufferCount := 0;//Clear receive bufferMSComm1.OutBufferCount
  := 0;//Clear send bufferMSComm1.RThreshold
  := 1;//Set to generate OnComm event when receiving a
  byteMSComm1.PortOpen := true;//Open serial port 1
/////////////////////////////////////////////////////////////////////////////
  Buffers := '''';
  CheckSum := 0;
//Send serial port command
  Command := 34;
  str := ''$'' + #2 + #$22 + #1;//Read total MP3 tracksstr
  := str + Char(GetCheckSum(str));//Calculate
  checksumMSComm1.Output := str;//Send serial port
commandend;

//The following is the receiving event processing process, which is equivalent to serial port interrupt in MCU

//Note that there are two statements

//while MSComm1.InBufferCount > 0 do //Input FiFO is not empty
//if str = '''' then str := #0; //0 character processing
//Example received data is #24#02#00#01#03

//At this time, InBufferCount=5. If Input is set, the number of bytes read from the receive buffer at one time is unlimited

//That is: MSComm1.InputLen := 0; then str := MSComm1.Input; str seems to be #24#02#00#01#03

//But the actual value is ''??''#24#02. No result is obtained, at least the #01#03 after the 0 character cannot be read.

//Use MSComm1.InputLen := 1; and cooperate with while MSComm1.InBufferCount > 0 do

//When reading 0 characters, since str = '''' (null), accessing str[1] will cause an exception and cause the program to terminate.

//So use if str = '''' then str := #0; to fill the character #0 into str[1] and the length of str is also 1.

//Therefore, the key point is to use the if str = '''' then str := #0; statement to overcome the difficulty of reading 0 characters~~~

procedure TForm1.MSComm1Comm(Sender: TObject);
var
  str: string;
  i: integer;
begin
  case MSComm1.CommEvent of
    comEvReceive://receive event processing
      begin
        while MSComm1.InBufferCount > 0 do//Input FiFO is not empty
        begin
          str := MSComm1.Input;//Only take 1 character from FIFO, because MSComm1.InputLen := 1
          if str = '''' then str := #0; //0 character processing
          if (Buffers = '''') and (str = ''$'') then//Synchronizer test
          begin
            Buffers := str;//Store synchronization symbol ''$''
            CheckSum := 0;//Initialize checksum
          end
          else if (Buffers <> '''') and (Buffers[1] = ''$'') then begin//Must start with synchronization symbol
            Buffers := Buffers + str;//Add data string
            CheckSum := CheckSum xor Byte(str[1]);//Calculate the checksum (except the synchronization character ''$'')
            if Length(Buffers) = Byte(Buffers[2]) + 3 then//Terminator test
            begin
              if CheckSum = 0 then//At this time, the checksum must be 0 to indicate that the checksum is correct
              begin
                case Command of
                  $22: begin//Get the total number of songs
                        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 := '''';//Clear the buffer after receiving
              CheckSum := 0;//Initialize the checksum
            end;
          end
          else
          begin
            Buffers := ''''; //Receive error, clear the buffer and discard all data
            CheckSum := 0; //Initialize checksum
          end;
        end;
      end;
  end;
end;

 
Keywords:Delphi  MSComm Reference address:Detailed explanation of the 0 character receiving routine using the serial port control MSComm in Delphi

Previous article:Ordinary MCU Teaching——Lecture 10 MCU Instructions (IV)
Next article:From 51 Beginner to Electronic Engineer (7)

Recommended ReadingLatest update time:2024-11-23 03:13

Learning to realize serial communication programming in MSComm in LabVIEW
    Problem description: For general applications, LabVIEW can be implemented directly using the VISA serial port API function, which is convenient and fast. However, for serial communication that requires the use of interrupts, the MSComm control must be used to implement it.     MSComm control: To operate ActiveX co
[Test Measurement]
Using TComm component to realize serial communication in Delphi environment
Abstract: Using Delphi to develop industrial control system software has become the choice of more and more developers, and serial communication is one of the problems that must be solved in this process. Based on the analysis and comparison of several commonly used serial communication methods, this paper focuses o
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号