Purebasic Serial Port Example

Page 1 of 1
[ 12 posts ]

USB-Serial Software Development Kit. USB-Serial Software Development Kit is a free-to-use, complete library and driver stack for USB-Serial Bridge Controller devices, in order to easily integrate USB interface into any embedded application. This SDK comes with configuration tool and application examples. The example 'ESP32SerialPortExample.ino' is a dual sender and receiver code example, using a single ESP32, you an send and receive data using the same (one) ESP32 relying on the internal ESP32 UART data buffers to make this a simple exercise to conduct, define the pins as required in your test application.

Print viewPrevious topic | Next topic
AuthorMessage
Posted: Thu May 29, 2003 2:53 pm
Enthusiast

Joined: Sun Apr 27, 2003 12:01 am
Posts: 128
Location: Cornwall UK
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.

Last edited by geoff on Thu May 29, 2003 8:23 pm, edited 2 times in total.

Top
Posted: Thu May 29, 2003 8:17 pm
PureBasic Expert

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
There are already 'unofficial' OpenCom() commands see:
http://www.purebasic.com/download/examples/PureFrog.zip

_________________
--Kale


Top
Posted: Thu May 29, 2003 8:41 pm
Enthusiast

Joined: Sun Apr 27, 2003 12:01 am
Posts: 128
Location: Cornwall UK
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?
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
ctReadIntervalTimeout = #MAXDWORD
ctReadTotalTimeoutMultiplier = 0
ctReadTotalTimeoutConstant = 0
ctWriteTotalTimeoutMultiplier = 0
ctWriteTotalTimeoutConstant = 0
HandleError( SetCommTimeouts_(*File, ct), 'SetCommTimeouts()' )
; construction des donnees d'initialisation du port
dcb.DCB2;
HandleError( GetCommState_(*File, @dcb), 'GetCommState()' )
dcbBaudRate = #CBR_300;
dcbParity = #NOPARITY;
dcbStopBits = #TWOSTOPBITS;
dcbByteSize = 8;
dcbFlags = 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


Top
Posted: Thu May 29, 2003 11:06 pm
PureBasic Expert

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
>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

_________________
--Kale


Top
Purebasic serial port example free
Posted: Fri May 30, 2003 10:26 am
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
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...


Top
Purebasic Serial Port ExamplePurebasic Serial Port Example
Posted: Fri May 30, 2003 10:35 am
Enthusiast

Joined: Sun Apr 27, 2003 12:01 am
Posts: 128
Location: Cornwall UK
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.


Top
Posted: Fri May 30, 2003 10:49 am
PureBasic Expert

Joined: Sun Apr 27, 2003 4:41 pm
Posts: 1661
Location: Germany
know the usual meaning of tampon in English.

well, that's a buffer too, isn't it?

_________________
Good programmers don't comment their code. It was hard to write, should be hard to read.


Top
Posted: Fri May 30, 2003 8:31 pm
PureBasic Expert

Joined: Fri Apr 25, 2003 4:51 pm
Posts: 2809
Location: Portugal, Lisbon
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

_________________
Elevator Maintenance: http://central-elevadores.pt
Good web hosting![/size]


Top
Posted: Sat Sep 27, 2003 2:59 pm
User

Joined: Sat Sep 27, 2003 2:53 pm
Posts: 67
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:
Port$ = 'COM1:'
*File = OpenComPort(0, Port$)
If *File
SetupComm_(*File, 4096, 4096) ; Set i/o buffers
; Change timeout settings:
ct.COMMTIMEOUTS
ctReadIntervalTimeout = #MAXDWORD
ctReadTotalTimeoutMultiplier = 0
ctReadTotalTimeoutConstant = 0
ctWriteTotalTimeoutMultiplier = 0
ctWriteTotalTimeoutConstant = 0
SetCommTimeouts_(*File, ct)
; Get protocol settings:
dcb.DCB
GetCommState_(*File, @dcb)
; Change protocol settings:
dcbBaudRate = #CBR_9600
dcbParity = #NOPARITY
dcbStopBits = #ONESTOPBIT
dcbByteSize = 8
dcbFbits = %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


Top
Posted: Wed Oct 15, 2003 10:05 pm
New User

Joined: Wed Oct 15, 2003 9:50 pm
Posts: 1
Location: Nottingham, England
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


Top
Posted: Wed Oct 15, 2003 10:18 pm
Enthusiast

Joined: Fri Apr 25, 2003 7:09 pm
Posts: 251
Location: (Madrid) Spain
You can try with MVCOM library for serial comm, is very usefull:
http://forums.purebasic.com/english/vie ... ight=mvcom

_________________
[:: PB Registered ::]
Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti


Top
Posted: Wed Oct 15, 2003 10:42 pm
PureBasic Expert

Joined: Sat May 17, 2003 11:31 am
Posts: 6074
eventual usb commands in an external library would indeed be nice in a future release...

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
Serial

Purebasic Serial Port Examples

Page 1 of 1
[ 12 posts ]