Xojo Version 2025 R2.1
Developing on a Mac for a Linux production target.
According to the documentation the Baud rate of a SerialConnection is set by assigning one of the Baud class constants to the .Baud property.
SerialConnection1.Baud = SerialConnection.Baud4800
MessageBox(SerialConnection1.Baud.ToString)
> 6
So far so good. However when I open the port the .Baud property changes to the actual board rate.
SerialConnection1.Connect
MessageBox(SerialConnection1.Baud.ToString)
> 4800
And stays that way after the port is closed
SerialConnection1.Close
MessageBox(SerialConnection1.Baud.ToString)
> 4800
Is this the expected behaviour?
Do I have to reassign a class constant if I want to reopen the port?
I guess docs need a more clear choice of words.
Seems that what you put into Serial.Baud is just a “Xojo index” of a table, and what you read back from Serial.Baud is the real chosen Baud Rate (set and get differs)
It’s not even that consistent.
If you set .Baud and immediately read it back the class constant value is returned. It only changes to the absolute value after the port has been opened.
Clumsy work around for a clumsy behaviour
Function BaudConstant(baud)
//return baud as a class constant
Var BaudConstants() As Integer = Array(300, 600, 1200, 1800, 2400, _
3600, 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400)
If Baud < 300 Then
Return baud
Else
Return BaudConstants.IndexOf(baud)
End If
End
I agree. Bad consistency.
To make things worse, it does not complain about any invalid values and “connects”
What it says is “The “Baud” class constants can be used to get or set the Baud rate”, italics mine, and “can” being the operative word.
What it doesn’t explicitly state is that you can just specify the actual integer baud rate, which imo results in more readable code.