Loading...
Pages: 1 [2]   Go Down
Author Topic: Data loss when sending to native USB port (SerialUSB)  (Read 1849 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 2
Posts: 84
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Stimmer,

I found kicking out the circular buffer a great demonstration "out of the box" thinking.

I have tested your implementation with ArduinoISP. I think this is a good real life use scenario of the serial port, with lots of messages of different sizes (though relatively small) in both directions. I powered my leonardo from the due's 3v3 and modified ArduinoISP to use SerialUSB instead of Serial. It worked well: I could upload download the bootloader to/from the leonardo several times.

Nevertheless dropping the serial buffer also has consequences for feeding the due lots of small messages. Each message is received in a separate bank and you only have two of them. Take for example this shell script:
Code:
n=0
while true; do

        echo "$n" > /dev/ttyACM1
        echo $?
        sleep 1
        let "n = $n + 1"
done

I ran this sketch on the due:
Code:
static const int led = 13;
static const int button = 3;

void setup() {
  // initialize serial:
  SerialUSB.begin(9600);
  Serial.begin(115200);
  pinMode(led, OUTPUT);     
  pinMode(button, INPUT);
  digitalWrite(button, HIGH);
}

void loop() {
  // if there's any serial available, read it:
  int i = 0;
  if (digitalRead(button)) {
    while (SerialUSB.available() > 0) {
      char c = SerialUSB.read();
      Serial.write(c);
      i++;
    }
  }
  Serial.print("i=");
  Serial.println(i);
  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led, LOW);
  delay(500);
}

If you press the button for a few seconds, some of the messages are lost.
Odd enough the return code of echo is always 0.

I am not sure where the bytes get lost. And maybe it can be prevented with stty calls, but a circular buffer would prevent this. A circular buffer which you fill up with memcpy would also make sense as an implementation.



Logged

Offline Offline
Sr. Member
****
Karma: 23
Posts: 446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting - I am not losing any data with that script. What happens for me is that echo blocks until ttyACM1 accepts the data. I expect that there is some mysterious stty setting which controls this.

However, it isn't really the right way to use a serial port in a script. The echo command opens the serial port, writes to it, then closes it again each time it executes. That's not what you'd do in, say, a C++ program, where you'd open the serial port once, write to it periodically, and close it at the end of the program. To do this in a shell script you'd use a fifo or a fd, for example:
Code:
exec 3<>/dev/ttyACM1
n=0
while true; do

        echo "$n" >&3
        echo $?
        sleep 1
        let "n = $n + 1"
done
This doesn't lose any data for me either, and it buffers the data on the computer rather than blocking.
Logged


Offline Offline
Jr. Member
**
Karma: 2
Posts: 84
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What happens for me is that echo blocks until ttyACM1 accepts the data.

Well, that is what I expected to happen for me too when I wrote the script. (And on another pc (linux 3.5 vs. 3.0.0)),  I did actually observe echo blocking). The point I wanted to make is that having no circular buffer, one could temporarily block a program on the pc.

But you are right. Closing and reopening the file every time causes the os to flush the file and this makes echo block. If a program would write to ttyACM1 in a normal way, the os would buffer the output... So it looks like we can indeed live without the buffer in the Due.

« Last Edit: February 04, 2013, 04:41:38 pm by PeterVH » Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 84
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I read you have Albert's test running. Is it easy to test with SerialUSB?
Logged

Offline Offline
Sr. Member
****
Karma: 23
Posts: 446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, Albert's program does work with my SerialUSB. In SimpleArduinoSerPro3 change "Serial." to "SerialUSB." in all 5 places. Then in setup()  after SerialUSB.begin(BAUD_RATE); add this line: delay(1000);
I'll have a think about buffers. I'm sure I could reintroduce a buffer with not too much loss of speed. But right now I am sick at the sight of USB code  smiley-mr-green So I'm going to take a break for a week or two and try some other projects which are more fun.
« Last Edit: February 04, 2013, 06:40:37 pm by stimmer » Logged


France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 254
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I read you have Albert's test running. Is it easy to test with SerialUSB?

Please note that the real author of SerPro is Alvaro (on arduino forum http://arduino.cc/forum/index.php?action=profile;u=16163) who build some interesting applications here, one being arduinoscope running on UNO and MEGA, see old posts on arduino forum like http://arduino.cc/forum/index.php/topic,7762.0.html

The topic of secure USB serial communication with arduino's was always tricky so Alvaro came up with SerPro. What happened is that my scientific and extreme electronics project required very safe RX/TX error-free so Alvaro added 16 CRC's along with HDLC protocol and other stuff as we both are telco engineers with critical software.

For some of you interested in non-java SerPro like GTK or C or C++, you'll find a mine of info directly at Alvaro's github https://github.com/alvieboy/arduino-serpro

The IDE version i've published on my website has removed the arduino-oscilloscope hence not using anymore ADC's link.

In fact, Alvaro is building a new arduino tech based on ZPU, namely you create your own softcore uC inside a FPGA with dedicated instructions, like emulate part of the Atmega2560 you want keep and build low level instructions not found in the market
http://www.alvie.com/zpuino/

Regarding ElectroShaman.jar, I've made for my own projects and might publish on github if there is more interest.
« Last Edit: February 04, 2013, 11:45:09 pm by selfonlypath » Logged

Pages: 1 [2]   Go Up
Print
 
Jump to: