Recent

Author Topic: Fast Serial communication  (Read 1324 times)

emjtech

  • New Member
  • *
  • Posts: 28
    • Global R&D ltd
Fast Serial communication
« on: October 27, 2020, 06:15:23 pm »
Hi
I need to do the fastest serial communication possible using a Virtual Com Port (VCP on USB)
The baud rate is 2000000, 8 data bits, 1 stop bit and no parity
I used a modified TLazSerial (to allow the 2 Mbaud rate) successfully sending packets of 6 bytes
Each string is written in 30 uSec as expected, but between the packets I have a "wait" of 1mS.
I do not know from where that wait comes, so I can't fix it.
Someone can help?
Thanks

This is the code:

procedure TFMain.FillBlockClick(Sender: TObject);
Var
   p,t,j,u,r,g,b:byte;
begin
          r:=RedVal.Position;
          g:=GreenVal.Position;
          b:=BlueVal.Position;
  For p:=PixelAdd.Value to PixelAdd1.Value do begin
    For t:=TileAdd.Value to TileAdd1.Value do begin
      For j:=JBadd.Value to JBadd1.Value do begin
        For u:=PortAdd.Value to PortAdd1.Value do begin
          WPixel(r,g,b, 0, SideSel.ItemIndex,p,t,j,u,false); //<-- This takes 30uS
        end;
      end;
    end;
  end;
end;



Thanks
Eli Jacob

emjtech

  • New Member
  • *
  • Posts: 28
    • Global R&D ltd
Re: Fast Serial communication
« Reply #1 on: October 27, 2020, 08:24:16 pm »
Hi
I want to add that I did a second procedure that send all the data as a single buffer, that works right, the total time is as fast as expected, however I can't use that procedure except for the test.
This is the test procedure:

procedure TFMain.BlockClick(Sender: TObject);
Var
   p,t,j,u,r,g,b:byte;
   Pos,Lng:integer;
begin
  Pos:=0;
          r:=RedVal.Position;
          g:=GreenVal.Position;
          b:=BlueVal.Position;
  Lng:=8*(1+PixelAdd1.Value-PixelAdd.Value)*
         (1+TileAdd1.Value-TileAdd.Value)*
         (1+JBadd1.Value-JBadd.Value)*
         (1+PortAdd1.Value-PortAdd.Value);
  //SetLength(Buf,Lng+1) ;
  For p:=PixelAdd.Value to PixelAdd1.Value do begin
    For t:=TileAdd.Value to TileAdd1.Value do begin
      For j:=JBadd.Value to JBadd1.Value do begin
        For u:=PortAdd.Value to PortAdd1.Value do begin
          PrepPixel(Pos,r,g,b, 0, SideSel.ItemIndex,p,t,j,u);
          inc(Pos,8);
        end;
      end;
    end;
  end;
  Serial.WriteBuffer(Buf,Lng);  //<-- this send the buffer at the correct speed.
end;

Therefore it looks like the delay happens each time the routine WriteBuffer of LazSerial is accessed, still I didn't found the problem on the LazSerial

Thanks
Eli Jacob

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Fast Serial communication
« Reply #2 on: October 28, 2020, 07:22:05 am »
hello,
Therefore it looks like the delay happens each time the routine WriteBuffer of LazSerial is accessed, still I didn't found the problem on the LazSerial
I don't think that the problem is in Lazserial ( Lazserial use sendbuffer of TBlockserial from Synaser to send data).
what is your O.S ? Windows ? Linux ? MacOsx ?  other ? 
Windows, Linux , MacOSx aren't real time O.S , you can have some latencies (ex : serial interrupt latency, Usb polling latency, process priority). Without buffering data, you can't use these O.S for real time communication data at high speed. What is your serial target ?


Friendly, J.P
« Last Edit: October 28, 2020, 07:34:20 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Fast Serial communication
« Reply #3 on: October 28, 2020, 09:53:46 am »
I need to do the fastest serial communication possible using a Virtual Com Port (VCP on USB)
The baud rate is 2000000, 8 data bits, 1 stop bit and no parity
I used a modified TLazSerial (to allow the 2 Mbaud rate) successfully sending packets of 6 bytes
Each string is written in 30 uSec as expected, but between the packets I have a "wait" of 1mS.
Your device is probably 12MB/s full speed USB. Your pc as USB host can send data frames to such devices in chunks of 1ms. Each frame can have up to 1500 bytes. This means that your maximum speed is 1.500.000 and not 2.000.000 as you assume (you can send up to 1500 bytes each ms). Faster devices can have 125us frames, but that does not seam to be your case.

Your best bet would be to use 1.500.000 bps (there is a small USB data buffer but your data is even smaller so it didn't matter) and fill as much as you can of 1500 bytes and send it all at once. You could try doing it as fast as you can, but the system shouldn't let you do that more then 1000 times per second.

At such speeds, the other side also needs to be able to process that amount of data. Arduino will not be able to handle it. Some ARMs might, and Raspberry Pi probably can. So, 1.500.000 bps is the upper theoretical limit.

In reality, I would not expect more then 64 kbps with USB 1.1 CDC devices, and 500 kbps with USB 2.0 CDC devices.

More info: https://www.usb.org/sites/default/files/bwpaper2.pdf
« Last Edit: October 28, 2020, 10:09:04 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

tetrastes

  • Sr. Member
  • ****
  • Posts: 469
Re: Fast Serial communication
« Reply #4 on: October 28, 2020, 12:42:27 pm »
In reality, I would not expect more then 64 kbps with USB 1.1 CDC devices, and 500 kbps with USB 2.0 CDC devices.

Why? What is the difference between Full Speed at USB 1.1 and 2.0?
« Last Edit: October 28, 2020, 01:18:15 pm by tetrastes »

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Fast Serial communication
« Reply #5 on: October 28, 2020, 02:38:06 pm »
In reality, I would not expect more then 64 kbps with USB 1.1 CDC devices, and 500 kbps with USB 2.0 CDC devices.

Why? What is the difference between Full Speed at USB 1.1 and 2.0?
In first case you are able to send max 64 bytes 1000 times per second, in second case max 1500 bytes 1000 times per second. If you want to send more then that then you will have to avoid CDC virtual serial in USB 1.1 and 2.0, and choose some other USB protocol where you would probably need to write your own driver.

Btw. I am not aware if USB 3.x brings any improvement to USB CDC speed.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

emjtech

  • New Member
  • *
  • Posts: 28
    • Global R&D ltd
Re: Fast Serial communication
« Reply #6 on: October 28, 2020, 04:47:44 pm »
Thank you
You have been helpful, my hardware is capable of handle the data even about 15 times faster since I designed it with FPGA's not a micro, certainly not Arduino.
I will work with a buffer so I could send large chunks of data.
I have no problem working at 2,000,000 bps
I didn't check the 1500 bytes limitation but I will keep it in mind
(Do you know ? if I send 3000 bytes will I get 2 chunks of 1500 separated by a mS delay or will I get only the first 1500?
Thanks
Thanks
Eli Jacob

tetrastes

  • Sr. Member
  • ****
  • Posts: 469
Re: Fast Serial communication
« Reply #7 on: October 28, 2020, 05:23:16 pm »
In reality, I would not expect more then 64 kbps with USB 1.1 CDC devices, and 500 kbps with USB 2.0 CDC devices.

Why? What is the difference between Full Speed at USB 1.1 and 2.0?
In first case you are able to send max 64 bytes 1000 times per second,

Are you sure you do not confuse with Low Speed?

Quote
in second case max 1500 bytes 1000 times per second.

Yes, this is for Full Speed, and USB 1.1 supports it. Note that document from your link is dated 1996 year, and USB 2.0 specification appeared in 2000.
For Full Speed USB 1.1 CDC devices see for example https://moxa.com/en/products/industrial-edge-connectivity/usb-to-serial-converters-usb-hubs/usb-to-serial-converters/uport-1000-series#models.

Quote
If you want to send more then that then you will have to avoid CDC virtual serial in USB 1.1 and 2.0, and choose some other USB protocol where you would probably need to write your own driver.

Btw. I am not aware if USB 3.x brings any improvement to USB CDC speed.

I think it depends on CDC device data rate. If it supports only Full Speed, there is no difference which host - USB 1.1, 2.0, 3.x...
Note that High Speed devices from above link support up to 16 ports with the same max baudrate as one-port Full Speed devices.
« Last Edit: October 28, 2020, 09:21:22 pm by tetrastes »

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Fast Serial communication
« Reply #8 on: October 29, 2020, 11:00:01 am »
Are you sure you do not confuse with Low Speed?
Nice catch.

Note that document from your link is dated 1996 year, and USB 2.0 specification appeared in 2000.
As I have said, there are not many programmable high speed USB devices, so I assumed it would be enough. Anyway, USB in it's basics has not changed that much since then (not talking about speed), and unlike original specification that document is briefly explaining some nice facts in just few pages.
« Last Edit: October 29, 2020, 11:06:40 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Fast Serial communication
« Reply #9 on: October 29, 2020, 11:00:16 am »
my hardware is capable of handle the data even about 15 times faster since I designed it with FPGA's not a micro, certainly not Arduino.
Then you might be interested in this VHDL core which claims to achieve 30Mbps with pc using high speed CDC: http://jorisvr.nl/article/usb-serial

We still do not know if your device emulates USB CDC virtual serial port, or it simply has it's RX and TX connected to some USB-Serial converter.

I will work with a buffer so I could send large chunks of data.
I have no problem working at 2,000,000 bps
I didn't check the 1500 bytes limitation but I will keep it in mind
High speed CDC devices are not that common, so I might have assumed wrongly that you use full speed USB CDC device at most (although you say that you have 1ms delays and not 125us, so it looks to me like full speed and not high speed). If that is not the case and you actually use high speed USB CDC then you can probably achieve much higher transfers. You should be able to see best your frame limits with logic analyzer, oscilloscope, or even Wireshark (and check whether bottlenecks are in your code, frame size, or elsewhere). Just be aware that communication errors are common at such fast CDC so you might want to use CRC or something similar for error correction. You get less errors and higher speeds with Linux stock drivers compared to Windows ones (although Win10 is not that bad). Ah yes, and use as short cables as you can afford, and unplug all USB devices you don't need during testing.

(Do you know ? if I send 3000 bytes will I get 2 chunks of 1500 separated by a mS delay or will I get only the first 1500?
Most drivers should do automatic frame splitting for you. Stock ones do that. Again, LA/OSC/Wireshark is your best friend in this matter.

Btw. Here are some benchmarks to get an idea what to expect on some specific systems:
https://e2e.ti.com/support/microcontrollers/other/f/908/t/947608
https://www.pjrc.com/teensy/benchmark_usb_serial_receive.html
« Last Edit: October 29, 2020, 11:15:34 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018