Broken data from/to USB-serial module 1.3 on 4.2 framework

Hi all.
I have a strange problem with the USB-serial module (USB serial 1.3 on 4.2 framework) and strings transmission betwen the serial and a PC. :wall:
I already read everything I found in the documentations AND in this forum, but nothing suggested me a solution.

1- When i ONLY send data (strings) FROM the USB-SERIAL to the PC, everything SEEMS to be ok (apparently no data is corrupted or missing in my samples)

2- When i ONLY send data (strings) FROM the PC to the USB-SERIAL, everything SEEMS to be ok (apparently no data is corrupted or missing in my samples)

3- When i try sending and receiving data simultanously and bidirectionaly from/to the PC and the USB-SERIAL module (e.g. using a timer on both sides or buttons that raise events and call write/writeline) the received data on both sides is randomly broken (missing bytes, or senseless binary content instead of the expected characters).
About 10% of the data sent seems to be broken :open_mouth:

I tried all these (also mixing various techniques hoping it could work):
A- using usbSerial.SerialLine.LineReceived
B- using usbSerial.SerialLine.DataReceived (manually handling ‘\n’ characters)
C- using usbSerial.SerialLine.Write
D- using usbSerial.SerialLine.WriteLine
E- adding locks (lock(myLock){…}) during reads and writes to guarantee that they are not concurrent (at least inside my code)
F- using a different USB-serial module (I have 2 modules, they have the same strange behaviour)
G- playing with ALL the configuration parameters (like baud rate, parity, flow control, and so on)… so since nothing changes, i think they are not meaningfull for my problem. Obviously the settings of the module always matches the settings of the serial on the PC.

Could please anyone suggest me a way to solve this problem ?
What is the correct way to implement bidirectional concurrent communication using the USB-serial module ?
Some code samples ?

What hardware are you using? I’m having serial communication issues on the G400/Raptor when it talks to the GSM Module over serial. It’s like you describe here… missing bits every now and then… using NETMF 4.2, GHI 2014 SDK and latest firmware/bootloader

I’m using a GHI Electronics USB-serial module, version 1.3 on 4.2 framework https://www.ghielectronics.com/catalog/product/287

What mainboard?

Fez Spider 1.0
https://www.ghielectronics.com/catalog/product/269

Any suggestions ?

The best would be to provide us with a test program, a simple one, that we or the community can try on our end,

Ok, I’ll make a pair of samples and I’ll post them.
Thanks for your feedback.

On this link you can fine a simple pair of programs.
http://dogmasolutions.com/public/Temp/USB_Serial_Test.zip
Here is a screenshot with the results
http://dogmasolutions.com/public/Temp/Results.png

In the zip there is a Visual Studio 2010 solution with 2 programs.

  • NJC_WindowsForms_USB_Serial_Test → The programs that runs on the PC.
    Using a timer, it sends messages prefixed with “<PC_” to the serial port, and receive messages from the gadgeteer mobo.

  • NJC_Gadgeteer_USB_Serial_Test → The program that runs on the gadgeteer mobo.
    Using a timer, it sends to the PC messages prefixed with “<GADGETEER_”.
    When it receive a message from the PC, it sends it back (infact in the “received” box of the screenshot you can find both “<PC_” and “<GADGETEER_” messages)

As you can see from the attached screenshot, some messages from the PC (the ones that starts with “<PC” and red arrows) are broken.
Those are the messages received by the gadgeteer mobo from the PC, and sent back to the PC

Hi,
at the Moment I do not have the time to test your code, but perhaps a look on these threads helps. It concerns the rs232 module, but the problems should be the same.

https://www.ghielectronics.com/community/forum/topic?id=14821&page=2

Post #17

and

https://www.ghielectronics.com/community/forum/topic?id=14927

Kind regards
Roland

Thankyou for you answer.
I’ll take a look at the threads you linked, even if I hope you’ll soon have the time to check my code.
Bye

Hi,
I did not test, but perhaps it works like this
The changes to your code are commented out

 
private void SerialLine_DataReceived(Gadgeteer.Interfaces.Serial sender, SerialData data)
    {
      try
      {
        
            int NumberOfBytesToRead = sender.BytesToRead;
         // int NumberOfBytesToRead = usbSerial.SerialLine.BytesToRead;
          byte[] readInputBuffer = new byte[NumberOfBytesToRead];
          sender.Read((readInputBuffer, 0, NumberOfBytesToRead);
         //usbSerial.SerialLine.Read(readInputBuffer, 0, NumberOfBytesToRead);

          foreach (byte b in readInputBuffer)
          {
            char c = Convert.ToChar(b);
            if (c == '\n')
            {
              String commandData = buffer.ToString();
              buffer.Clear();
              Debug.Print(commandData);
              usbSerial.SerialLine.WriteLine(commandData); // send back to the PC
            }
            else
            {
              buffer.Append(c);
            }
          }
        
      }
      catch (Exception exc)
      {
        Debug.Print(exc.Message);
      }
    

Thankyou for the suggestion.
I tried the modifications you suggested, but even if the errors are a bit less frequent, they still happen too much often (5% of messages is corrupted)
Any other hints ?

Are the messages already corrupted, when they arrive in the Spider (Debug.print) or only when they are back at the PC?

Bye, til tomorrow

Some are already corrupted when they arrive in the Spider (Debug.print), some others are corrupted when they arrive on the PC

Only an idea,
perhaps it’s worth a try to store the messages in a buffer an send them back from another thread, not directly from the Eventhandler.

This would not solve the fact that the Spider receive broken data, this would (maybe) only solve the fact that the PC receive broken data from the Spider.
How do I solve the fact that the Spider receive broken data ?

I did not yer check your code for the PC but it could be the same issue.
Perhaps a Google search: “serial port read and write at the same time” helps.

I followed you suggestions, and I managed to get a stable bidirectonal communication.
Unfortunately I can obtain such stability only at a trasmission rate of about 100 chars/second, and this is unacceptable for the kind of application I’m developing.
If I increase the data exchange rate some messages are lost or arrives broken.
So by now my problem is a duplicity of instability and/or speed.

Maybe I’m approaching the problem in the wrong way, so let’s rollback to the root.

First of all: what is the most reliable way to make robust bidirectional simultaneous serial communication between Gadgeteer and a PC ?

Faster transmission would require use of RTS/CTS. I did not catch it before, but there are 2 jumpers on the module next to the IC. Bridging J1 connects RTS on the IC and bridging J2 connects CTS on the IC.

Could you place a jumper for both of these and retry with handshaking enabled?