Serial port baudrate higher than 115200

Just starting out? Need help? Post your questions and find answers here.
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Serial port baudrate higher than 115200

Post by Cezary »

Dear forum users,

is there any way to get higher serial port baud rate than 115200 bps? I did tests with another basic compiler on the same hardware and got communication speed up to 900000 bps. PureBasic limits this to 115200, port opening at other speeds fails. Can this limitation be somehow omitted?

Best regards
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial port baudrate higher than 115200

Post by infratec »

Hm...

this works for me:

Code: Select all

If OpenSerialPort(0, "COM3", 256000, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)
  Debug "Success"
  CloseSerialPort(0)
Else
  Debug "Failed"
EndIf
And I use also other not 'normal' baudrates like 10400 or 7812 and it works.
But I use the driver from FTDI and not the MS driver.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Serial port baudrate higher than 115200

Post by STARGÅTE »

Cezary wrote: Sun Feb 20, 2022 1:22 pm Dear forum users,

is there any way to get higher serial port baud rate than 115200 bps? I did tests with another basic compiler on the same hardware and got communication speed up to 900000 bps. PureBasic limits this to 115200, port opening at other speeds fails. Can this limitation be somehow omitted?

Best regards
900k bps? wtf! on a serial port? Is this communication still "error free"? About what "hardware" we talking about? And what distances?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial port baudrate higher than 115200

Post by infratec »

@STARGATE

serial port means not RS232.
Today you have to use an USB adapter which is represented via a COM port.
Behind is, for example, CAN bus.
So you can also communicate with 2.500.00 bps to the FTDI chip, since it uses USB.
But I think then you need to communicate via the ftdi.dll
Via the COM driver you can reach at least 923,076 baud, but I think it depends on the used chip:

https://ftdichip.com/products/ft2232hq/
TassyJim
Enthusiast
Enthusiast
Posts: 151
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: Serial port baudrate higher than 115200

Post by TassyJim »

It will depend on the serial adapter you are using but I regularly use 460800 baud with PB

Some (but not all) adapters allow for non standard baud rates.
And some such as the Raspbery Pi pico run at maximum speed no matter what baud rate you set when opening the port.

Jim
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Serial port baudrate higher than 115200

Post by Cezary »

infratec,
you are right, on Windows this is possible. I made a mistake because I didn't write that I meant higher speed in Linux. On the same hardware with WIndows and PB6 I get 128000, 230400, 256000, Linux does not allow more than 115200.

STARGÅTE,
I need communication with the ARM Cortex-M0 microcontroller, eventually even at TTL voltage levels.
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Serial port baudrate higher than 115200

Post by Cezary »

TassyJim,
I am currently using Digitus USB - RS232 converter and with Windows it allows for high transmission speeds.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial port baudrate higher than 115200

Post by infratec »

I use this, but only tested for lower custom OBD baudrates:

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  
#TCIFLUSH = 0
#TCOFLUSH = 1
#TCIOFLUSH = 2

#TCSANOW = 0
#TCSADRAIN = 1
#TCSAFLUSH = 2
  
#TIOCGSERIAL = $541E
#TIOCSSERIAL = $541F

#ASYNCB_SPD_HI = 4
#ASYNCB_SPD_VHI = 5

#ASYNC_SPD_HI = (1 << #ASYNCB_SPD_HI)
#ASYNC_SPD_VHI = ( 1 << #ASYNCB_SPD_VHI)
#ASYNC_SPD_CUST = (#ASYNC_SPD_HI | #ASYNC_SPD_VHI)

;#NCCS = 19
#NCCS = 60
Structure termios_struct Align #PB_Structure_AlignC
  c_iflag.l
  c_oflag.l
  c_cflag.l
  c_lflag.l
  c_line.a
  c_cc.a[#NCCS]
EndStructure


Structure serial_struct Align #PB_Structure_AlignC
  type.l
  line.l
  port.l
  irq.l
  flags.l
  xmit_fifo_size.l
  custom_divisor.l
  baud_base.l
  close_delay.u
  io_type.b
  reserved_char.b[1]
  hub6.l
  closing_wait.u
  closing_wait2.u
  *iomem_base
  iomem_reg_shift.u
  port_high.l
  iomap_base.l
EndStructure

;Global OrgTIO.termios_struct
;Global OrgLinuxSerialInfo.serial_struct 

;Debug SizeOf(termios_struct)

Procedure.i OpenSerialPortLinux(SerialPort, SerialPortName$, Baud, Parity, Databits, Stopbits, HandshakeMode, InputBufferSize, OutputBufferSize)
  
  Protected Port.i, Handle.i, Error.i
  Protected LinuxSerialInfo.serial_struct
  
  
  Port = OpenSerialPort(SerialPort, SerialPortName$, 38400, Parity, Databits, Stopbits, HandshakeMode, InputBufferSize, OutputBufferSize)
  If Port
    
    If SerialPort = #PB_Any
      Handle = SerialPortID(Port)
    Else
      Handle = Port
    EndIf
    
    ;tcgetattr_(Handle, @OrgTIO)
    
    Error = ioctl_(Handle , #TIOCGSERIAL, @LinuxSerialInfo)
    If Error = 0
      
      LinuxSerialInfo\flags | #ASYNC_SPD_CUST
      LinuxSerialInfo\custom_divisor = Round(LinuxSerialInfo\baud_base / Baud, #PB_Round_Nearest)
      
      Error = ioctl_(Handle, #TIOCSSERIAL, @LinuxSerialInfo)
      If Error <> 0
        CloseSerialPort(Port)
        Port = 0
      EndIf
      
    Else
      CloseSerialPort(Port)
      Port = 0
    EndIf
    
  EndIf
  
  ProcedureReturn Port
  
EndProcedure




Procedure CloseSerialPortLinux(Port.i)
  
  Protected LinuxSerialInfo.serial_struct
  
  ioctl_(SerialPortID(Port), #TIOCGSERIAL, @LinuxSerialInfo)
  
  LinuxSerialInfo\flags = LinuxSerialInfo\flags & ~#ASYNC_SPD_CUST
  LinuxSerialInfo\custom_divisor = LinuxSerialInfo\baud_base / 38400
  
  ioctl_(SerialPortID(Port), #TIOCSSERIAL, @LinuxSerialInfo)
  
  CloseSerialPort(Port)
  
EndProcedure

; Define Port.i, Byte.a
; 
; Port = OpenSerialPortLinux(#PB_Any, "/dev/ttyUSB0", 10400, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 256, 256)
; If Port
;   Byte = $55
;   WriteSerialPortData(Port, @Byte, 1)
;   CloseSerialPortLinux(Port)
; EndIf

CompilerEndIf
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Serial port baudrate higher than 115200

Post by Cezary »

infratec,
after initial testing, it seems to work at any high speed. Thank you very much.
technopol
User
User
Posts: 20
Joined: Thu Mar 24, 2011 11:00 pm

Re: Serial port baudrate higher than 115200

Post by technopol »

Cezary
I currently run at 921600 baud talking to a FT231XS-R, but before I continue debugging my board at higher speed (I aim at 3Mbaud with the FT231 and hopefully 100Mbaud with my own interface), I just wanted to make sure both Windows and Purebasic support it.

By "it seems to work at any high speed", what's the highest speed you achieved?
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Serial port baudrate higher than 115200

Post by Cezary »

technopol,

460800 bps works fine for me with a typical USB-RS485 dongle, no errors even with very long upload/receive streams (around 200 kbytes).
Post Reply