Lost data during serial communication with Arduino Uno

I have a sketch (I'll paste below) that I install on an ArduinoUno that simply reads 8 bytes over the serial connection, and then echos them back. I communicate to the Arduino via PuTTY. This works fine with a direct connection, but when connected over VirtualHere, I end up losing the first few bytes on the reply. It's not consistently the same number of bytes lost, but it's usually 1-4. After that, communication seems to work OK.

Example:
1. load the sketch to an arduino
2. connect to arduino via PuTTY.
3. Type "12345678" in PuTTY. it echos back "45678" (or similar)
4. Type "12345678" in PuTTY. it echos back "12345678"

I can repeat step 4 over and over and it seems to work, but the loss in step 3 is causing issues in a more complicated example. (This is basically the simplest example we have to reproduce the error).

This happens on Windows client, Linux sever. We haven't seen this on the Linux client.

Any ideas? Things to look into?

Here's the code:

/*
   Read 8 bytes from serial in, and send them back.
*/

#define DATA_SIZE 8
byte data[DATA_SIZE];
int index = 0;

bool writing = false;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void serialEvent() {
  if (!writing && index < DATA_SIZE && Serial.available() > 0) {
    index += Serial.readBytes(&data[index], DATA_SIZE-index);
  }    
}

void loop() { // run over and over
  if (index >= DATA_SIZE-1) {
    writing = true;
    Serial.write(data, DATA_SIZE);
    index = 0;
    Serial.write(" ");
    writing = false;
  }
}
#3

In windows device properties, can you find the com port and select properties and make sure to uncheck allow computer to turn off device to save power.

#4

I don't see that setting (there is no Power Management tab) for the Arduino Uno on the COM port.

I *did* see the setting on the some of the USB controllers, and unchecked them all. Same result.

#5

Can you tell me the Vendor ID and Product Id of the arduino when its shown in virtualhere.

#8

I tend to agree with you. I've even simplified the code, taking the serial read out of the equation:


/*
Read 8 bytes from serail in, add 1 to each, and send them back.
*/

#define DATA_SIZE 8
byte data[DATA_SIZE] = {'h', 'e', 'l', 'l', 'o', '\n' };
int index = 0;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// we don't always get all of this
// Serial.println("All set up!");
Serial.write(data, 6);
int x = Serial.getWriteError();
Serial.print(" error: ");
Serial.println(x);
}

void serialEvent() {
// clear the event
byte b = Serial.read();

Serial.print("hello ");
Serial.println(b);
}

void loop() { // run over and over
}

I don't get missing characters every time, but it is pretty consistent. And it's only ever the first communication from the Arduino to the computer that does it - after that it seems to be fine (or at least so far). I've thrown in every error check, and counted the number of bytes written, and everything, and it appears that the Arduino thinks it is writing all the bytes. I've tried adding Serial.available() on the read (and then even took reading out of the equation) and that doesn't help.

I'll talk with some people in-house, but if you've got ideas and where else to debug this drop, I'm open to suggestions.

Thanks!

#9

So I've asked around. The serial communication between Arduino and PC has been quite solid from our experiences. It might drop data at very rare cases, mostly running at high baud rate like 115200(even this is debatable as I haven’t seen it). Nonetheless, your sketch only uses 9600 so we're not convinced that it is a serial problem on Arduino hardware.

Is there any verbose-level logging to enable to log all data both at the server and the client to help us pinpoint this issue?

#10

Further investigation with WireShark (and I'm no wireshark expert):

By inspecting the data part of the TCP transmission, it looks like all data comes across the network.

However, when inspecting the USB URB , the first one has a packet data length of 0, while the rest have some data. I suspect that the first one should have had some data as well, and that's what we're losing.

Of course, that's my amateur-hour arm-chair investigation. Does it offer any clues? Any thoughts as to what to try next?

#11

Ok, at the moment i cannot look at this because im on holidays and i dont have my test equipment. But i will be back around 25th July then i will definately look at this . I have a mercury T2 i can use to see exactly what the arduino is sending..

#13

I found the bug in the client when running in Windows 10, i'll fix in the next day or two...

#14

This is fixed in 4.5.5 of the client, no server update required.