Search code examples
c#.netserial-port.net-3.5virtual-serial-port

virtual comport is very slow compared to terminal


I'm writing an application where I need to send a file (~600kB) to another unit via a virtual serialport.

When I send it using a terminal application (TeraTerm) it takes less than 10 seconds, but using my program it takes 1-2 minutes.

My code is very simple:

port.WriteTimeout = 30000;
port.ReadTimeout = 5000;
port.WriteBufferSize = 1024 * 1024; // Buffer size larger than file size
...
fs = File.OpenRead(filename);
byte[] filedata = new byte[fs.Length];
fs.Read(filedata, 0, Convert.ToInt32(fs.Length));
...
for (int iter = 0; iter < filedata.Length; iter++) {
    port.Write(filedata, iter, 1);
}

Calling port.Write with the entire file length seems to always cause a write timeout for unknown reason, so I'm writing 1 byte at a time.


Solution

  • Solved it, here's the details in case someone else finds this it might give some hints on what's wrong.

    I was reading the file wrong, somehow the application used \r\n as newlines when transferring. The file itself is a Intel .hex file which contains checksums which were calculated using \r as newlines.

    Checksum errors caused the other device to ACK very slowly, thus making the transfer super slow combined with the PC application now handling checking for checksum errors.

    If you have similar errors I recommend using a software snoop to monitor what's actually being sent