• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 
April 25, 2024, 05:17:59 pm

News:

SMF - Just Installed!


RS232 help needed

Started by kasko, April 03, 2014, 02:58:37 am

Previous topic - Next topic

kasko

Hello out there, I need some help!
I have two problems I hope someone could help me with.
I'm using the nrComm component in a program to control an external unit, connectid via a USB/RS232 converter.
By clicking on some buttons in my program, single commands are sent to the external unit and the unit responds with a string terminating with CR+LF. The length of the return string is variable. This workes ok in Async mode. I'm using code as in the sample program SimplePacket.

Then problem 1:
Let's say I have three buttons, sending three different comamnds. Based on the three different responses from the external unit, the three divverent responses should be "decoded" in three different ways. The calls in the click-routine cab be like this:
    send command
    decode response
    based on the response contents set different variables
    do some calculations
As the system works in Async mode, the program execution will not wait for the response from the external unit, resulting in wrong decode, wrong variables and wrong calculations.
I could have used Synchcronous mode and nrComm.waitforbytes, but as the length of the returnstring is unknown, this is not possible.
Any suggestions?

Then problem 2:
I have tried to solve problem 1 ia a way, but a new problem appears when I send two command just after each other like this:
   send command1
   send command2
   decode response1
   decode response2
Now I get only response2 to decode, response1 is lost.

Any suggestions out there?

Best regards
Karl Jan Skontorp

kasko

I have found a solution to my problem, and for those of you who might be interested, please read on!

The two following snippets are found in one of the demo programs. Every time a packet (terminated with CR+LF) is received, the result is stored in a listbox(lbRespons).

procedure TfrmMain.nrCommAfterReceive(Com: TObject; Buffer: Pointer;
  Received: Cardinal);

var i:integer;
    ch:AnsiChar;
begin
  for i := 0 to Received - 1 do begin
    ch := PAnsiChar(Buffer)[I];
    if ch = EndOfPacket[idxEOP] then begin
      if idxEOP >= Length(EndOfPacket) then begin
        DoSomethingWithPacket(DataPacket);
        DataPacket := '';
        idxEOP := 1;
      end;
      Inc(idxEOP);
    end else begin
      idxEOP := 1;
      DataPacket := DataPacket + ch;
    end;
  end;
end;


procedure TfrmMain.DoSomethingWithPacket(S:string);
begin
  lbRespons.Items.Add(S);
end;



Then I have this routine on the different buttons. The commands to be sent is stored in an array belonging to each button (CommandF1). Then the key to get it working is the use of the Repeat - Until loop! Going through the commands, the loop will wait for a respons from every command before sending the next command. This will continue until all commands are sent and a response is received for each of them. Then the nrComm is closed. There are two listboxes, one for receiving only one response and another one to keep all the responses. After receiving all the responses, they are decoded and action taken is dependent on the content of the response!

  Begin
         if oob0.OnOff then
         Begin
           for i := 0 to High(CommandF1) do
           Begin
             lbRespons.Clear;
             Str:=CommandF1[i];
             SendCommand(Str+EndOfPacket);  //Str:='#'+cbAddress.Text+' Set X:1=1\n'+EndOfPacket;
             Repeat
               Application.ProcessMessages;
             Until lbRespons.Items.Count=1 ;
             Responser.Append(lbRespons.Items.Strings[0]);
           End;
           nrComm.Active:=False;
           for i := 0 to High(CommandF1) do
           Begin
             Do smoething with the responses!
           End;
           oob0.OnOff:=False;
         End;
       End;

     
I'm not sure, but I think I have read somewhere that the use of "Application.ProsessMessages" is not a good practice - anyhow this workes ok in my program.

Best regards
Karl Jan Skontorp