Author Topic: Looped SendString gets slower each iteration  (Read 755 times)

edbored

  • Newbie
  • *
  • Posts: 2
    • View Profile
Looped SendString gets slower each iteration
« on: April 21, 2014, 03:10:30 am »

I have a situation where I am sending a command string to a device that must start from power-off state.

On power up, this device waits 50ms for a special init string. If it doesn't see the init string after 50 ms, the device jumps to main processing, which ignores the init string.

I'm using a USB-to-CANBus dongle that behaves as a virtual serial port.

I have a main project that is failing, so I created a simple project that simply performs the following:

nrComm1.Active:=true;
for k:=0 to 9 do
  nrComm1.SendString('my command string');

I run this with the dongle plugged into my laptop, and the device not powered up.

From the log file, you can see how the first 4 iterations take about 1ms each  to run (051-054).

I gave up waiting for the 5th iteration after about 4 minutes - I  turned on the device, and the SendStrings worked as I'd expect.

[20/04/2014 10:27:23 PM +766] nrComm Lib v9.37 Pro Windows 7 [6.1.7601] Service Pack 1
[20/04/2014 10:27:23 PM +768] nrComm: The port COM3 is opened succesfully
[20/04/2014 10:27:32 PM +051] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:27:32 PM +052] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:27:32 PM +053] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:27:32 PM +054] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:31:13 PM +101] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:31:13 PM +102] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:31:13 PM +104] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:31:13 PM +105] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:31:13 PM +106] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:31:13 PM +107] nrComm: Try to send string :X16040008N0500010000000000; ...
[20/04/2014 10:31:22 PM +916] nrComm: The port is closed succesfully

It doesn't matter if I use spHardware, spNone, spXonXoff - SendString always blocks after 4 calls.

In my main program I expected to be able to do the following:

Code: [Select]
procedure ProcessMyCommand(aCommand: AnsiString; var ResultString: AnsiString; retries: integer;timeOutMS: integer)
var
  ndx  : integer;
  done : boolean;
  cLen : integer; // read string length
begin
  done := false;
  ndx  := 0;

  cLen := length(aCommand); // return result is same length as sent command

  while ( (ndx<retries ) and (not done) ) do begin
     inc(ndx);   
     nrComm1.SendString(aCommand); // send string
     // receive and Done, or timeout and try again
     if  nrComm1.WaitForBytes(cLen, timeOutMs) then begin
       inStr := nrComm1.ReadString;
       //<parse and handle inStr>
       done:=true;
     end;
  end;
end;

That is, I fully expected SendString to just return (given the 8K buffer) - so that I could "timeout" waiting on receiving data.

I don't understand why the 5th call to SendString does not return immediately.

Cheers,
EdB

edbored

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Looped SendString gets slower each iteration
« Reply #1 on: April 22, 2014, 02:10:15 am »
Resolved: this is the third (or fourth) generation of this particular CAN-USB dongle.

The previous adapters defaulted to different setting.

This new one buffers 4 outgoing CAN commands, then 'lock', waiting for the bus to clear in order to ensure that the host program "knows" about the issue.

It doesn't actually time-out - it sits and waits for CAN bus to clear.

So, a blocking SendString will wait forever (or until the CAN bus comes "alive").

Changing the default behaviour of the CAN dongle to time-out after 50ms cleared everything up.


Cheers,
EdB