I'm using a Teensy 3.2 with Teensyduino 1.44.
I have an application where I send data from a PC to the Teensy over the USB serial port, and I was noticing data corruption. I wrote this simple sketch to demonstrate the problem:
On the PC side, I'm running this code (C#):
And, finally, the output (from the PC, which just shows what the Teensy sends to it):
These numbers suggest that I'm loosing entire USB packets, because most of those packets should be carrying 64 bytes of data. Is there just too much overhead in the loop to keep up? It does very little work, unless it finds an error.
I have an application where I send data from a PC to the Teensy over the USB serial port, and I was noticing data corruption. I wrote this simple sketch to demonstrate the problem:
Code:
void setup() {
Serial.begin(1000000);
}
static int seq = 0;
static int received = 0;
static int lasterror = -1;
void loop() {
if (Serial.available()) {
int c = Serial.read();
if (c != seq) {
Serial.print("Received: ");
Serial.println(c);
Serial.print("Expected: ");
Serial.println(seq);
if (lasterror >= 0) {
Serial.print("# bytes since last error: ");
Serial.println(received - lasterror);
}
lasterror = received;
}
seq = c + 1;
if (seq > 255) {
seq = 0;
}
received++;
if (received % 150000 == 0) {
Serial.print("# of bytes received: ");
Serial.println(received);
}
}
}
On the PC side, I'm running this code (C#):
Code:
using System;
using System.IO.Ports;
using System.Threading;
namespace teensy_usbserial
{
class Program
{
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
var serial = (SerialPort)sender;
Console.Write(serial.ReadExisting());
}
static void Main(string[] args)
{
var serial = new SerialPort("COM11", 1000000);
serial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
serial.Open();
var buf = new byte[1572];
byte seq = 0;
while (true) {
for (int i = 0; i < buf.Length; i++) {
buf[i] = seq++;
}
serial.Write(buf, 0, buf.Length);
Thread.Sleep(200);
}
}
}
}
And, finally, the output (from the PC, which just shows what the Teensy sends to it):
Code:
Received: 172
Expected: 44
Received: 80
Expected: 208
# bytes since last error: 36
Received: 236
Expected: 172
# bytes since last error: 92
Received: 208
Expected: 16
# bytes since last error: 36
Received: 52
Expected: 180
# bytes since last error: 78308
Received: 244
Expected: 116
# bytes since last error: 64
Received: 116
Expected: 52
# bytes since last error: 64
Received: 116
Expected: 180
# bytes since last error: 64
Received: 88
Expected: 152
# bytes since last error: 64
Received: 12
Expected: 140
# bytes since last error: 7988
Received: 204
Expected: 76
# bytes since last error: 64
Received: 76
Expected: 12
# bytes since last error: 64
Received: 76
Expected: 140
# bytes since last error: 64
# of bytes received: 300000
These numbers suggest that I'm loosing entire USB packets, because most of those packets should be carrying 64 bytes of data. Is there just too much overhead in the loop to keep up? It does very little work, unless it finds an error.