Page 1 of 1
[Implemented] Easy serial comms
Posted: Thu May 29, 2003 2:53 pm
by geoff
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.
Posted: Thu May 29, 2003 8:17 pm
by Kale
There are already 'unofficial'
OpenCom() commands

see:
http://www.purebasic.com/download/examples/PureFrog.zip
Posted: Thu May 29, 2003 8:41 pm
by geoff
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?
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
Posted: Thu May 29, 2003 11:06 pm
by Kale
>Compare my example above with this code extracted from the PureFrog
example.
Yes, i see what you mean.
P.S. I hate to ask what
des tampons d'E/S are too 8O
Posted: Fri May 30, 2003 10:26 am
by Fred
It means 'IO/Buffers'. I agree fully about the com part and that's why I doesn't put the OpenCom() as an official command. It was too hacky for a proper release. I will probably add USB and COM support at the same time as separate libraries. Ok I say that since a while...
Posted: Fri May 30, 2003 10:35 am
by geoff
Thanks Fred. I'm in no hurry on this one, but simple I/O would be a great
addition to the language at some stage.
I expect you know the usual meaning of tampon in English.
Posted: Fri May 30, 2003 10:49 am
by traumatic
geoff wrote:know the usual meaning of tampon in English.
well, that's a buffer too, isn't it?

Posted: Fri May 30, 2003 8:31 pm
by Num3
traumatic wrote:geoff wrote:know the usual meaning of tampon in English.
well, that's a buffer too, isn't it?

It's gotta be

, cause sometimes it needs to be flushed or it will generate a buffer underrun

Posted: Sat Sep 27, 2003 2:59 pm
by paulr
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:
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
Posted: Wed Oct 15, 2003 10:05 pm
by Barry
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
Posted: Wed Oct 15, 2003 10:18 pm
by Saboteur
You can try with MVCOM library for serial comm, is very usefull:
viewtopic.php?t=7427&highlight=mvcom
Posted: Wed Oct 15, 2003 10:42 pm
by blueznl
eventual usb commands in an external library would indeed be nice in a future release...