Got an idea for enhancing PureBasic? New command(s) you'd like to see?
infratec
Always Here
Posts: 6874 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Sat Feb 18, 2017 2:16 pm
Hi,
serial comunication is not dead
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
Bernd
infratec
Always Here
Posts: 6874 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Sat Feb 18, 2017 2:34 pm
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
Posts: 1443 Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia
Post
by User_Russian » Sat Feb 18, 2017 4:29 pm
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