SetSerialPortBaudrate(Port.i, Baud.i)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

SetSerialPortBaudrate(Port.i, Baud.i)

Post by infratec »

Hi,

serial comunication is not dead :mrgreen:

I have an application, where I need to switch the baudrate during the comunication.
At the moment I need to use CloseSerialPort() an OpenSerialPort().

But...
that's to slow.
I miss sometimes a few bytes.

Code: Select all

SetSerialPortBaudrate(Port.i, Baud.i)
should be faster :wink:

Bernd
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SetSerialPortBaudrate(Port.i, Baud.i)

Post by infratec »

For Windows I did it already:

Code: Select all

Procedure.i SetSerialPortBaudrate(Port.i, Baud.i)
  
  Protected Result.i, PortDCB.DCB
  
  
  If IsSerialPort(Port)
    If GetCommState_(SerialPortID(Port), @PortDCB)
      PortDCB\BaudRate = Baud
      Result = SetCommState_(SerialPortID(Port), @PortDCB)
    EndIf
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
But I would be nice if it is a native PB command.
And I think it will be faster, because the state is already known, so GetCommState_() would not be needed.

Bernd
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: SetSerialPortBaudrate(Port.i, Baud.i)

Post by User_Russian »

http://www.purebasic.fr/english/viewtop ... 90#p479490

Code: Select all

Procedure SetBaudRate(Port, Rate)
  Protected Result=#False, PortID

  If IsSerialPort(Port)
    
    PortID = SerialPortID(Port)
    
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      
      Protected dcb.DCB
      If PortID
        If GetCommState_(PortID, @dcb)  
          dcb\BaudRate  = Rate ; Требуемая скорость.
          If gModuleMode & #OW_Mode_NoControlPin = 0
            dcb\fbits & ~%0010000000100000
            If gModuleMode & #OW_Mode_Invert = 0
              dcb\fbits |  %0001000000010000
            Else
              dcb\fbits & ~%0001000000010000
            EndIf
          EndIf
          If SetCommState_(PortID, @dcb)
            Result = #True
          EndIf
        EndIf
      EndIf
      
    CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
      
      Protected options.termios, Sp
      
      If Rate=9600 Or Rate=115200
        
        CompilerIf #PB_Compiler_OS = #PB_OS_Linux
          Select Rate
            Case 9600   : Sp = 13
            Case 115200 : Sp = 4098
          EndSelect
        CompilerElse
          Sp = Rate
        CompilerEndIf
        
        If tcgetattr_(PortID, @options) = 0   ; читает параметры порта.
          If cfsetispeed_(@options, Sp) = 0   ; установка скорости порта.
            If cfsetospeed_(@options, Sp) = 0 ; установка скорости порта.
              If tcsetattr_(PortID, #TCSANOW, @options) = 0
                Result=#True
              EndIf
            EndIf
          EndIf
        EndIf
        
      EndIf
      
      If gModuleMode & #OW_Mode_NoControlPin = 0
        If gModuleMode&#OW_Mode_Invert <> gModuleModeLin&#OW_Mode_Invert
          If gModuleMode & #OW_Mode_Invert=0
            SetSerialPortStatus(Port, #PB_SerialPort_DTR, 1)
            SetSerialPortStatus(Port, #PB_SerialPort_RTS, 1)
          Else
            SetSerialPortStatus(Port, #PB_SerialPort_DTR, 0)
            SetSerialPortStatus(Port, #PB_SerialPort_RTS, 0)
          EndIf
        EndIf
        gModuleModeLin=gModuleMode
      EndIf
      
    CompilerEndIf
    
    If Result=#False : tLastErr=#OW_Err_BaudRate : EndIf
  EndIf
    
  ProcedureReturn Result
EndProcedure
Post Reply