[Implemented] Easy serial comms
[Implemented] Easy serial comms
I would like to see simple PureBasic commands to allow reading and
writing of serial ports just as easily as files (like in old Basics).
OpenCom(2,"COM1",baudrate)
WriteByte(chr(13))
CloseCom(2)
Simple as that.
DCB structures, buffers etc would be handled by the language.
Similar commands for USB ports would also be useful.
writing of serial ports just as easily as files (like in old Basics).
OpenCom(2,"COM1",baudrate)
WriteByte(chr(13))
CloseCom(2)
Simple as that.
DCB structures, buffers etc would be handled by the language.
Similar commands for USB ports would also be useful.
Last edited by geoff on Thu May 29, 2003 8:23 pm, edited 2 times in total.
There are already 'unofficial' OpenCom() commands
see:
http://www.purebasic.com/download/examples/PureFrog.zip

http://www.purebasic.com/download/examples/PureFrog.zip
Thanks for your help Kale.
Actually, I already have some experience of writing code to use the serial
Comms using the Windows API, although in another language.
That's why I'd like to see simple PureBasic commands.
IMO the forté of a high level language should be to separate the programmer
from the complexities of the OS. Thus programs are easier to write and test,
less dependant on OS foibles and hence more portable. If I wanted to do
everything using the API I might as well use a low level language like C.
Compare my example above with this code extracted from the PureFrog
example.
I hate to ask what des tampons d'E/S are 8O
In any case, the commands used are unofficial so will they
be supported in future versions of PureBasic?
Actually, I already have some experience of writing code to use the serial
Comms using the Windows API, although in another language.
That's why I'd like to see simple PureBasic commands.

IMO the forté of a high level language should be to separate the programmer
from the complexities of the OS. Thus programs are easier to write and test,
less dependant on OS foibles and hence more portable. If I wanted to do
everything using the API I might as well use a low level language like C.
Compare my example above with this code extracted from the PureFrog
example.

In any case, the commands used are unofficial so will they
be supported in future versions of PureBasic?
Code: Select all
Structure DCB2
DCBlength.l ; sizeof(DCB)
BaudRate.l ; current baud rate
Flags.l
wReserved.w ; not currently used
XonLim.w ; transmit XON threshold
XoffLim.w ; transmit XOFF threshold
ByteSize.b ; number of bits/byte, 4-8
Parity.b ; 0-4=no,odd,even,mark,space
StopBits.b ; 0,1,2 = 1, 1.5, 2
XonChar.b ; Tx and Rx XON character
XoffChar.b ; Tx and Rx XOFF character
ErrorChar.b ; error replacement character
EofChar.b ; end of input character
EvtChar.b ; received event character
wReserved1.w ; reserved; do not use
EndStructure
StartFrog:
*File = OpenComPort(0, SelectedPort$+":")
If *File
If EnableLog
If OpenFile(1, "PureFrog.log")
FileSeek(Lof())
EndIf
EndIf
If GetCommState_(*File, @PortConfig.DCB2)
; Allocation des tampons d'E/S
;
HandleError( SetupComm_(*File, 4096, 4096), "SetupComm()" )
ct.COMMTIMEOUTS
ct\ReadIntervalTimeout = #MAXDWORD
ct\ReadTotalTimeoutMultiplier = 0
ct\ReadTotalTimeoutConstant = 0
ct\WriteTotalTimeoutMultiplier = 0
ct\WriteTotalTimeoutConstant = 0
HandleError( SetCommTimeouts_(*File, ct), "SetCommTimeouts()" )
; construction des donnees d'initialisation du port
dcb.DCB2;
HandleError( GetCommState_(*File, @dcb), "GetCommState()" )
dcb\BaudRate = #CBR_300;
dcb\Parity = #NOPARITY;
dcb\StopBits = #TWOSTOPBITS;
dcb\ByteSize = 8;
dcb\Flags = 4227 ; Combined flags values got from the C.. PureBasic doesn't support flags in structures
HandleError( SetCommState_(*File, @dcb), "SetCommState()" )
CreateThread(@FrogThread(), 0)
EndIf
Else
MessageRequester("PureFrog","Can't open the following port: "+SelectedPort$+Chr(10)+"This port is may be in use", #MB_ICONERROR)
EndIf
Return
Hi,
I'm pretty new to PB coding, but I'm interested in getting serial comms up and running. I've managed to strip down the PureFrog code to something a bit more easy to understand... The following program sends a quick message to the COM1 port, and waits 5 secs for a response. I've used it to communicate with a Palm IIIe PDA running 'ptelnet' - a terminal program. The code won't work with the demo version of PureBasic, as it uses windows functions. Here goes, I hope this is of use to people:
I'm pretty new to PB coding, but I'm interested in getting serial comms up and running. I've managed to strip down the PureFrog code to something a bit more easy to understand... The following program sends a quick message to the COM1 port, and waits 5 secs for a response. I've used it to communicate with a Palm IIIe PDA running 'ptelnet' - a terminal program. The code won't work with the demo version of PureBasic, as it uses windows functions. Here goes, I hope this is of use to people:
Code: Select all
Port$ = "COM1:"
*File = OpenComPort(0, Port$)
If *File
SetupComm_(*File, 4096, 4096) ; Set i/o buffers
; Change timeout settings:
ct.COMMTIMEOUTS
ct\ReadIntervalTimeout = #MAXDWORD
ct\ReadTotalTimeoutMultiplier = 0
ct\ReadTotalTimeoutConstant = 0
ct\WriteTotalTimeoutMultiplier = 0
ct\WriteTotalTimeoutConstant = 0
SetCommTimeouts_(*File, ct)
; Get protocol settings:
dcb.DCB
GetCommState_(*File, @dcb)
; Change protocol settings:
dcb\BaudRate = #CBR_9600
dcb\Parity = #NOPARITY
dcb\StopBits = #ONESTOPBIT
dcb\ByteSize = 8
dcb\Fbits = %1000010000011 ; Flags copied from PureFrog (see Microsoft dev site for details)
SetCommState_(*File, @dcb)
; Send message to serial port:
UseFile(0)
WriteString("Serial output")
a$ = " "
String$ = ""
Delay(5000)
UseFile(0)
While ReadData(@a$, 1)
String$ + a$
UseFile(0)
Wend
Debug "String is: "+String$
Else
Debug "Can't open COM port."
EndIf
Hi I echo the request for easier handeling of RS232, USB and the LPT port. Ive been out of programming for many years and things just seem to have got more complicated. It would certainly make PureBasic attractive to the increasing number of people out there wanting to hook things up to the PC. Although I guess Microsoft took most of the fun out of this via the PCI bus.
Barry

You can try with MVCOM library for serial comm, is very usefull:
viewtopic.php?t=7427&highlight=mvcom
viewtopic.php?t=7427&highlight=mvcom
[:: PB Registered ::]
Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
eventual usb commands in an external library would indeed be nice in a future release...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )