I have used serial communications on Mac and Windows targets with no issues. I understand XoJo doesn’t directly support baud rates above 230400 on linux, so I use a shell command to set 1Mbaud (stty -F /dev/ttyUSB0 1000000). But when I send serial data, it doesn’t work. Measuring the bit times on a scope show XOJO sending bits at 57600 baud. What is even more odd is after I quit my app and go to a terminal window and type stty -F /dev/ttyUSB0 it reports 1000000. If I then type
echo UUUU >/dev/ttyUSB0
I measure 1Mbaud.
I also tested
screen /dev/ttyUSB0 1000000
and it works at 1Mbaud.
My application never sets a baud rate. I just clear and set request to send and send a series of characters.
Tested on XOJO 2024 4.2 and 2025 1.1 built on a Mac and run on a RPi 5.
It is strange that before AND after I run my app that stty reports the baud rate at 1000000 but xojo sends data at 57600.
Is this a known bug? On my setup it is 100% reproducible. Anyone else have a similar issue?
1000000 is not a standard baud rate. You mentioned 57,600 the next rate is 2x, that is 115,200, and the next one is 2x, thus 230,400, etc. The command may return the actual baud rate being 1000000, but that does not mean the serial port will work at that speed. You should check the specs for your RaspberryPi.
Sorry I was not clear, but when I said “and it works at 1Mbaud.” that is measured on my oscilloscope and actually communicating with my target device. Under python I regularly use 3Mbaud on a Pi with FTDI adapters.
It seem very odd, and undesirable for an XOJO app to start with the baud rate correctly set to 1Mbaud, set its serial ports to 57600 baud, then exit and see the baud rate restored to 1Mbaud. But in fact that is what happens, measured with a scope.
I have learned a little more. The baud rate that XOJO transmits at is its own default you setup in the Serial Connection Inspector window. The inspector limits you to 16 baud rates from 300 to 230400, all reminiscent of modem speeds from the last century.
I wrote the simplest XOJO serial app I knew how to do. I added a Serial connection named Serial1, set the baud rate to 230400. I added an open event to my window with the code:
Var SelectedSerialPortIndex as Integer
SelectedSerialPortIndex = 2
Serial1.Device = SerialDevice.At(SelectedSerialPortIndex)
Serial1.Connect
Serial1.XmitWait
Serial1.Write(“UUUU”)
Serial1.Flush
Quit(0)
and saved it as SerialBaudTest
I then wrote a simple script:
stty -F /dev/ttyUSB0 1000000
echo UUUU >/dev/ttyUSB0
./SerialBaudTest
echo UUUU >/dev/ttyUSB0
Then captured the three transmissions with my scope (nearly didn’t have the capture depth)
Below are 4 pictures, the first one is the whole capture. The next three are each transmission zoomed in so you can see the bit timing.
Spoiler: 1Mbaud, 230400 baud, 1Mbaud.
I can kinda understand the initial 1Mbaud → 230400 baud if they want to enforce their own default. The last switch baffles me.
I also can’t make a shell call work to change the baud rate even though I have found references on the internet that some people have made that work.
Serial baud rates up to 12Mbaud are not uncommon these days. I have a Raspberry Pi 3B linked to a Windows machine, FTDI adapter to FTDI adapter,running at 3Mbaud that has been running for 3 years (with python).
…for example, replacing the XOJO code above with the below, I am still stuck with 230400 baud.
Var SelectedSerialPortIndex as Integer
Var ShellResult As String
Var sh As New Shell
sh.Backend = “bash”
sh.Execute(“/usr/bin/stty -F /dev/ttyUSB0 1000000”)
ShellResult = sh.Result
SelectedSerialPortIndex = 2
Serial1.Device = SerialDevice.At(SelectedSerialPortIndex)
Serial1.Connect
Serial1.XmitWait
Serial1.Write(“UUUU”)
Serial1.Flush
Quit(0)
May I ask for what process do you need that rate of data? I quit using serial many years ago and now I just use Ethernet and WiFi. That solved all of the speed issues I have.
I may have wrongly thought stty could be used instead of setserial. setserial is kinda primitive and also not installed by default on some Linux distros (for example, the Raspberry Pi).
My last response shows my stty based attempt.
setserial requires you read your UART’s base clock, then you divide that by your desired baud rate, then you write that divisor, and lastly tell it to substitute your new custom baud rate for it 38.4kbaud setting.
stty will just set the baud rate you specify.
I will try installing setserial and testing tonight.
Thank you.
I am a big fan of Ethernet and use it in many application. I am a hardware engineer but often also design test cells on our production floor and favor Ethernet (kinda hate WiFi, especially the old g release, in that environment). We do have some tester doing functional safety testing that require that there is no way to even try to hook them to the internet. For those systems I use serial at 3Mbaud.
The application I am working on now is for family. I designed a tiny model rocket flight computer/logger that “looks” like an Arduino to the Arduino IDE. I have a my host application working on Windows and Mac to set things like sealevel pressure, lat/log location, and so on. I also download up to 256kB of flight log data so I picked 1Mbaud for faster downloads. The size and weight requirements preclude Ethernet and the age of my grandchildren preclude wireless gizmos, all they have to do is read the altitude off the OLED. My board is 12mm x 40mm and weights under 5 grams with battery.
For the benefit of anyone passing by this post. The phrase “standard baud rates” puts you on shaky ground. There are many standards of asynchronous serial communications depending on your application/industry/requirements. Here is a short list of (some maybe not so standard) baud rates.
The automotive SAE J2602 standard rate is 10417 Baud
The music standard MIDI is 31250 baud
The standard DMX lighting is 250000 baud
FTDI USB to serial adapter ‘standard’ Baud Rates (down to 88888888) for their high speed USB products:
12000000
9600000
8000000
6000000
3000000
2000000
1500000
1411764.706
1333333.333
1263157.895
1200000
1142857.143
1090909.091
1043478.261
1000000
960000
923076.9231
888888.8889
…and if you are into Lionel trains, their baud rate over the tracks was 3030 baud.
Skytraq GPS maximum baud rate 921600
If you are a historian, there were even non-integer baud rates in the 1960s, 134.5 baud for IBM mechanical typewriters.
And through the years I have personally developed products with these baud rates:
300
3030
9600
10417
230400
1000000
3000000
Julia, thank you. setserial is required. Required confessions: assume, RTFM-twice!
This is what I have learned (long version) for those who care:
When you drop the serial control into your application in XOJO and launch your application, XOJO will set your serial port to the default baud rate selected in the inspector, even though you may have never set serialcontrol.baud. This seems non-Linux like to me. So this means you can’t do:
stty -F /dev/ttyUSB0 1000000
before your application opens your serial port, because your application will overwrite it.
setserial requires you to ‘replace’ one of the Linux standard baud rates with your custom baud rate. The baud rate setserial sacrifices is 38400.
setserial is not installed on some distros. Ubuntu and Raspberry Pi don’t have it by default.
sudo apt install setserial
will get you going.
setserial can’t set your baud rate directly, you first must find your baud rate divider’s clock input frequency by:
So the baud rate divider input clock is 24000000Hz, and I want 1000000 baud, so 24000000 / 1000000 = 24. Now I do:
setserial /dev/ttyUSB0 divisor 24 spd_cust
In my case, setserial replied with:
stty: /dev/ttyUSB0: unable to perform all requested operations
No details on what failed, but the baud rate was in fact changed.
Now we have replaced 38400 baud with 1000000 baud for that port and all applications on your machine (not just your XOJO application).
So now my XOJO application must:
A) Check if setserial exists, if not, inform user to install and quit.
B) run setserial to find clock rate, check for errors
C) run setserial to set spd_cust, check for errors, or only some errors???
D) and on quit, restore the baud rate to normal or the next application will fail if it needs 38400. This is an open issue for me because
setserial /dev/ttyUSB0 spd_normal
fails for me on my Raspberry Pi. So I guess I will write the divisor required for 38400.