5

With an Arduino-like board using a CH340 USB Serial chip, I'd like to send text lines as fast as possible to a Raspberry Pi through USB using the Serial.println() function. The problem is that when I set the serial speed to values above 230 400 with the Serial.begin(speed) function, the Arduino console crashes and the C program on the Rapsberry Pi displays garbage in its console, even though I set the same serial speed.

Short examples :

Serial.begin(230400);
Serial.println("Test !"); // Works fine.

Serial.begin(250000);     // Or any speed above 250 000.
Serial.println("Test !"); // Doesn't work at all.

This answered question states that 2 Mbauds is possible, so I'm a bit lost.

The Arduino IDE console offers various speed from 300 to 2 000 000 bauds, so why don't speeds above 230 400 work ?

Chris Stratton
  • 5,346
  • 17
  • 40
Thesaurus Rex
  • 63
  • 1
  • 2
  • 8
  • What type of Uno? Clone? Check what USB bridge is used on your board. – Mikael Patel Jul 21 '17 at 09:48
  • Cheap Chinese clone. The chip seems to be a CH340G. – Thesaurus Rex Jul 21 '17 at 09:51
  • 2
    CH340G does not support 250 Kbps. See page 5, https://www.insidegadgets.com/wp-content/uploads/2016/12/ch340g-datasheet.pdf – Mikael Patel Jul 21 '17 at 09:53
  • Thank you for the datasheet and this information. But according to the document, speeds like 460 800 bauds and 2 000 000 bauds should work, so why would this not be the case with my board ? – Thesaurus Rex Jul 21 '17 at 10:55
  • Add the line "while (!Serial); " after "Serial.begin()" or simply a delay. The USB handshake might take some time and the first character lost. – Mikael Patel Jul 21 '17 at 12:00
  • 1
    Please remember that the baudrate is not the USB transfer rate. It is between the MCU and the USB bridge. Also the max sustained baudrate is approx. 100-200 Kbps with the Arduino HardwareSerial implementation. – Mikael Patel Jul 21 '17 at 12:07
  • Thank you for your help and reminder. My _print()_ are in a _while_ loop, and it's not a problem if I miss a few characters at the beginning. What is the HarwareSerial implementation ? I see it's a custom version of _Serial_, but what is it for ? – Thesaurus Rex Jul 21 '17 at 12:45
  • The HardwareSerial class is an implementation of the Arduino Serial class that uses the MCU/AVR UART (hardware serial). The class (internals) are interrupt driven and uses ring-buffers to store incoming and outgoing characters. – Mikael Patel Jul 21 '17 at 13:25

2 Answers2

8

CHG340 supports common baud rates: 50, 75, 100, 110, 134.5, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 9600, 14400, 19200, 28800, 33600, 38400, 56000, 57600, 76800, 115200, 128000, 153600, 230400, 460800, 921600, 1500000, 2000000 baud.

250 000 baud isn't supported by that IC.

The Baud steps is either 200% or 150% of the previous value (There is exceptions).

Tested and working Baud rates.

I used putty as serial monitor since the built in monitor didn't have all baud rates preprogrammed.

SpeedTest.ino

// Serial port Baudrates:
// const int Speed = 115200;
// const int Speed = 128000;
// const int Speed = 153600;
// const int Speed = 230400;
// const int Speed = 460800;
// const int Speed = 921600;
// const int Speed = 1500000;
const int Speed = 2000000;

void setup() {
  // Initialize serial:
  Serial.begin(Speed);

  // Initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}


void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  Serial.print("Speed :  ");
  Serial.println(Speed);
}
MatsK
  • 1,336
  • 1
  • 11
  • 24
-1

There may just be a workaround to allow this adapter to operate at 250000 baud (at least in the Raspberry Pi). The author of this article: http://dmxpi.blogspot.com/2014/04/dmx-on-raspberry-pi-start-here.html has modified the linux driver and claims it works. I hope this helps.