[Implemented] Easy serial comms

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

[Implemented] Easy serial comms

Post 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.
Last edited by geoff on Thu May 29, 2003 8:23 pm, edited 2 times in total.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

There are already 'unofficial' OpenCom() commands :) see:

http://www.purebasic.com/download/examples/PureFrog.zip
--Kale

Image
User avatar
geoff
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Apr 27, 2003 12:01 am
Location: Cornwall UK
Contact:

Post 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 

Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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
--Kale

Image
Fred
Administrator
Administrator
Posts: 18274
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

geoff wrote:know the usual meaning of tampon in English.
well, that's a buffer too, isn't it? 8)
Good programmers don't comment their code. It was hard to write, should be hard to read.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

traumatic wrote:
geoff wrote:know the usual meaning of tampon in English.
well, that's a buffer too, isn't it? 8)
It's gotta be :twisted: , cause sometimes it needs to be flushed or it will generate a buffer underrun :lol:
paulr
User
User
Posts: 70
Joined: Sat Sep 27, 2003 2:53 pm

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

Post 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.

:D Barry
Saboteur
Enthusiast
Enthusiast
Posts: 273
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Post by Saboteur »

You can try with MVCOM library for serial comm, is very usefull:

viewtopic.php?t=7427&highlight=mvcom
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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... )
Post Reply