Page 1 of 1

Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 1:22 pm
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

Re: Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 1:45 pm
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.

Re: Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 1:52 pm
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?

Re: Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 2:04 pm
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/

Re: Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 9:14 pm
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

Re: Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 9:30 pm
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.

Re: Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 9:35 pm
by Cezary
TassyJim,
I am currently using Digitus USB - RS232 converter and with Windows it allows for high transmission speeds.

Re: Serial port baudrate higher than 115200

Posted: Sun Feb 20, 2022 10:14 pm
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

Re: Serial port baudrate higher than 115200

Posted: Tue Feb 22, 2022 12:26 am
by Cezary
infratec,
after initial testing, it seems to work at any high speed. Thank you very much.

Re: Serial port baudrate higher than 115200

Posted: Mon Feb 06, 2023 11:32 pm
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?

Re: Serial port baudrate higher than 115200

Posted: Sat Jul 08, 2023 4:36 pm
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).