Purebasic Serial Port Example

Purebasic Serial Port Example

PureBasic - SerialPort example file;; (c) Fantaisie Software;; -; CompilerIf #PBCompilerOS = #PB. 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. Purebasic - A Beginner's Guide To Computer Programming. Is an essential guide for newcomers to Purebasic or computer programming in general. If you are an experienced programmer looking for a quick reference on Purebasic or somebody wishing to learn how to program computers from scratch, this book is for you. Serial Port Redirector. Serial Port Redirector makes serial data from virtual serial ports available on a TCP/IP network. Main Features: - Transparently network-enables serial applications. True virtual serial port driver. Compatible with wide variety of serial servers. Full support of Telnet RFC2217 protocol.

Page 1 of 1
[ 15 posts ]
Print viewPrevious topic | Next topic
Rs-232
AuthorMessage
Posted: Wed Jan 02, 2008 4:13 pm
Addict

Joined: Sat Apr 10, 2004 1:20 pm
Posts: 1143
Location: Germany
; PureBasic SerialPort Library Demonstration
; Based on the demo Code by Marc Vitry and its MVCOM Library
; To sue this software connect a loop back connector to the tested serial port
; Or connect 2 PC with a serial cable and run this software on the 2 PC
EnableExplicit
Global ComBaud.l ;Index of selected com baudrate
Global ComData.l ;Index of selected data bit
Global ComStopbits.l ;Index of selected stopbits
Global ComParity.l ;Index of selected parity
Global ComHandshake.l ;Index of selected handshake mode
Global comID.l ;Communication Handle
Global ComPort.s ;Communications Port
Global Baud.l ;Baudrate
Global Parity.l ;Parity
Global DataBits.l ;DataBits
Global StopBits.f ;StopBits
Global Handshake.l ;Handshake
;Windows
Enumeration
#Window_0
EndEnumeration
;Gadgets
Enumeration
#Text01
#Label01
#Key_Quit
#Key_SR
#Key_RTSON
#Key_RTSOFF
#Key_DTRON
#Key_DTROFF
#Key06
#Key07
#Key08
#Key09
#ListBaud
#ListData
#ListStopbits
#ListParity
#ListHandshake
#ListPorts
#Gadget_50
#Gadget_51
#Gadget_54
#Gadget_55
#Gadget_56
#Gadget_57
#Gadget_58
#Gadget_59
#Gadget_60
#Gadget_61
#Gadget_62
#Gadget_63
#Gadget_64
#Gadget_65
#Gadget_66
#Gadget_67
#Gadget_68
#Gadget_69
#Gadget_Xon
#Gadget_Xoff
EndEnumeration
;Menu
Enumeration
#Menu_Return
EndEnumeration
Procedure Open_Window_0()
If OpenWindow(#Window_0, 0, 0, 370, 400, 'PureBasic Serial Demonstration', #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
StringGadget(#Text01, 50, 10, 310, 20, ')
ComboBoxGadget(#ListPorts, 10, 215, 70, 0)
ComboBoxGadget(#ListBaud, 130, 215, 100, 0)
ComboBoxGadget(#ListData, 130, 245, 100, 0)
ComboBoxGadget(#ListStopbits, 130, 275, 100, 0)
ComboBoxGadget(#ListParity, 130, 305, 100, 0)
ComboBoxGadget(#ListHandshake, 130, 335, 100, 0)
ButtonGadget(#Key_SR, 240, 145, 120, 30, 'Send / Read Data')
ButtonGadget(#Key_RTSON, 240, 175, 60, 30, 'RTS ON')
ButtonGadget(#Key_RTSOFF, 300, 175, 60, 30, 'RTS OFF')
ButtonGadget(#Key_DTRON, 240, 205, 60, 30, 'DTR ON')
ButtonGadget(#Key_DTROFF, 300, 205, 60, 30, 'DTR OFF')
ButtonGadget(#Key06, 240, 235, 60, 30, 'TXD ON')
ButtonGadget(#Key07, 300, 235, 60, 30, 'TXD OFF')
ButtonGadget(#Key08, 240, 265, 120, 30, 'Open Com Port')
ButtonGadget(#Key09, 240, 295, 120, 30, 'Close Com Port')
ButtonGadget(#Key_Quit, 240, 325, 120, 30, 'Quit')
EditorGadget(#Label01, 50, 30, 310, 100, #PB_Editor_ReadOnly)
TextGadget(#Gadget_50, 10, 10, 40, 20, 'TxD')
TextGadget(#Gadget_51, 10, 30, 30, 20, 'RxD')
TextGadget(#Gadget_54, 120, 135, 70, 20, 'RTS ------>CTS')
TextGadget(#Gadget_55, 120, 155, 70, 20, 'DTR ----->DSR')
TextGadget(#Gadget_56, 160, 175, 30, 20, 'DCD', #PB_Text_Right)
TextGadget(#Gadget_57, 160, 195, 30, 20, 'RI', #PB_Text_Right)
TextGadget(#Gadget_58, 210, 135, 20, 18, ')
TextGadget(#Gadget_59, 210, 155, 20, 18, ')
TextGadget(#Gadget_60, 210, 175, 20, 18, ')
TextGadget(#Gadget_61, 210, 195, 20, 18, ')
TextGadget(#Gadget_62, 85, 215, 40, 20, 'Speed', #PB_Text_Right)
TextGadget(#Gadget_63, 85, 245, 40, 20, 'Data', #PB_Text_Right)
TextGadget(#Gadget_64, 85, 275, 40, 20, 'Stopbits', #PB_Text_Right)
TextGadget(#Gadget_65, 85, 305, 40, 20, 'Parity', #PB_Text_Right)
TextGadget(#Gadget_66, 55, 335, 70, 20, 'Handshaking', #PB_Text_Right)
TextGadget(#Gadget_67, 5, 365, 70, 20, 'Xon character', #PB_Text_Right)
TextGadget(#Gadget_68, 125, 365, 70, 20, 'Xoff character', #PB_Text_Right)
TextGadget(#Gadget_69, 240, 365,120, 20, ',#PB_Text_Border)
StringGadget(#Gadget_Xon, 80, 365, 30, 20, '17')
StringGadget(#Gadget_Xoff, 200, 365, 30, 20, '19')
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, #Menu_Return)
EndIf
EndProcedure
Procedure Set()
SetGadgetState(#ListBaud,ComBaud)
SetGadgetState(#ListData,ComData)
SetGadgetState(#ListStopbits,ComStopbits)
SetGadgetState(#ListParity,ComParity)
SetGadgetState(#ListHandshake,ComHandshake)
If CountGadgetItems(#ListPorts)
SetGadgetState(#ListPorts,0)
EndIf
EndProcedure
Procedure Init()
Protected PortNr,Port.s
ClearGadgetItems(#ListBaud)
AddGadgetItem(#ListBaud,-1,'75 Bauds')
AddGadgetItem(#ListBaud,-1,'110 Bauds')
AddGadgetItem(#ListBaud,-1,'134 Bauds')
AddGadgetItem(#ListBaud,-1,'150 Bauds')
AddGadgetItem(#ListBaud,-1,'300 Bauds')
AddGadgetItem(#ListBaud,-1,'600 Bauds')
AddGadgetItem(#ListBaud,-1,'1200 Bauds')
AddGadgetItem(#ListBaud,-1,'1800 Bauds')
AddGadgetItem(#ListBaud,-1,'2400 Bauds')
AddGadgetItem(#ListBaud,-1,'4800 Bauds')
AddGadgetItem(#ListBaud,-1,'7200 Bauds')
AddGadgetItem(#ListBaud,-1,'9600 Bauds')
AddGadgetItem(#ListBaud,-1,'14400 Bauds')
AddGadgetItem(#ListBaud,-1,'19200 Bauds')
AddGadgetItem(#ListBaud,-1,'38400 Bauds')
AddGadgetItem(#ListBaud,-1,'57600 Bauds')
AddGadgetItem(#ListBaud,-1,'115200 Bauds')
AddGadgetItem(#ListBaud,-1,'128000 Bauds')
ClearGadgetItems(#ListData)
AddGadgetItem(#ListData,-1,'4 Bits')
AddGadgetItem(#ListData,-1,'5 Bits')
AddGadgetItem(#ListData,-1,'6 Bits')
AddGadgetItem(#ListData,-1,'7 Bits')
AddGadgetItem(#ListData,-1,'8 Bits')
ClearGadgetItems(#ListStopbits)
AddGadgetItem(#ListStopbits,-1,'1 Bits')
AddGadgetItem(#ListStopbits,-1,'1.5 Bits')
AddGadgetItem(#ListStopbits,-1,'2 Bits')
ClearGadgetItems(#ListParity)
AddGadgetItem(#ListParity,-1,'None');#PB_SerialPort_NoParity = 0
AddGadgetItem(#ListParity,-1,'Odd');#PB_SerialPort_OddParity = 1
AddGadgetItem(#ListParity,-1,'Even');#PB_SerialPort_EvenParity = 2
AddGadgetItem(#ListParity,-1,'Mark');#PB_SerialPort_MarkParity = 3
AddGadgetItem(#ListParity,-1,'Space');#PB_SerialPort_SpaceParity = 4
ClearGadgetItems(#ListHandshake)
AddGadgetItem(#ListHandshake,-1,'None');#PB_SerialPort_NoHandshake = 0
AddGadgetItem(#ListHandshake,-1,'RTS Enable');#PB_SerialPort_RtsHandshake = 1
AddGadgetItem(#ListHandshake,-1,'RTS / CTS');#PB_SerialPort_RtsCtsHandshake = 2
AddGadgetItem(#ListHandshake,-1,'Xon / Xoff');#PB_SerialPort_XonXoffHandshake = 3
SetGadgetText(#Text01,'The quick brown fox jumps over the lazy dog!!!')
ClearGadgetItems(#ListPorts)
For PortNr = 1 To 256
Port = 'COM' + Str(PortNr)
comID = OpenSerialPort(#PB_Any,Port,1200,#PB_SerialPort_NoParity,8,1,#PB_SerialPort_NoHandshake,0,0)
If comID
AddGadgetItem(#ListPorts,-1,Port)
CloseSerialPort(comID)
EndIf
Next
comID = 0
ComBaud = 11 ;9600 Bauds
ComData = 4 ;8 Bits
ComStopbits = 0 ;1 Stopbit
ComParity = 0 ;No parity
ComHandshake = 0 ;No Handshake
Set()
EndProcedure
Procedure GetComDescription()
ComPort = GetGadgetText(#ListPorts)
Baud = Val(GetGadgetText(#ListBaud))
DataBits = Val(GetGadgetText(#ListData))
StopBits = ValF(GetGadgetText(#ListStopbits))
Handshake = GetGadgetState(#ListHandshake)
Parity = GetGadgetState(#ListParity)
EndProcedure
Procedure DisplayComError()
Protected ComErrorText.s,ComError
If IsSerialPort(comID)
ComError = SerialPortError(comID)
If ComError & #PB_SerialPort_RxOver : ComErrorText + 'An input buffer overflow has occurred'+#CR$ : EndIf
If ComError & #PB_SerialPort_OverRun : ComErrorText + 'A character-buffer overrun has occurred'+#CR$ : EndIf
If ComError & #PB_SerialPort_RxParity : ComErrorText + 'The hardware detected a parity error'+#CR$ : EndIf
If ComError & #PB_SerialPort_Frame : ComErrorText + 'The hardware detected a framing error'+#CR$ : EndIf
If ComError & #PB_SerialPort_Break : ComErrorText + 'The hardware detected a break condition'+#CR$ : EndIf
If ComError & #PB_SerialPort_TxFull : ComErrorText + 'The output buffer was full'+#CR$ : EndIf
If ComError & #PB_SerialPort_IOE : ComErrorText + 'An I/O error occurred during communications with the device'+#CR$ : EndIf
If ComError & #PB_SerialPort_WaitingCTS : ComErrorText + 'Waiting CTS'+#CR$ : EndIf
If ComError & #PB_SerialPort_WaitingDSR : ComErrorText + 'Waiting DSR'+#CR$ : EndIf
If ComError & #PB_SerialPort_WaitingRLSD : ComErrorText + 'Waiting RLSD'+#CR$ : EndIf
If ComError & #PB_SerialPort_XoffReceived : ComErrorText + 'Waiting because the XOFF character was received'+#CR$ : EndIf
If ComError & #PB_SerialPort_XoffSent : ComErrorText + 'Waiting because the XOFF character was transmitted'+#CR$ : EndIf
If ComError & #PB_SerialPort_EOFSent : ComErrorText + 'EOF character has been received'+#CR$ : EndIf
If Right(ComErrorText,1)=#CR$:ComErrorText=Left(ComErrorText,Len(ComErrorText)-1):EndIf
MessageRequester('SerialPortDemo','Communication error occured :'+#CR$+ComErrorText)
EndIf
EndProcedure
; -----------------------------------------------------------------------------------------------
; Main loop
; -----------------------------------------------------------------------------------------------
Define Event,Buffer.b,Receive.b,Text.s,Time,Timediff,GadgetID,EOL.b
Open_Window_0()
Init()
Repeat
Event = WaitWindowEvent(100)
Select Event
Case 0
If comID And IsSerialPort(comID)
If GetActiveGadget()<>#Gadget_Xon
SetGadgetText(#Gadget_Xon,StrU(GetSerialPortStatus(comID,#PB_SerialPort_XonCharacter),#PB_Byte))
EndIf
If GetActiveGadget()<>#Gadget_Xoff
SetGadgetText(#Gadget_Xoff,StrU(GetSerialPortStatus(comID,#PB_SerialPort_XoffCharacter),#PB_Byte))
EndIf
Buffer = 0 : Receive = 0
Text = GetGadgetText(#Label01)
Time=ElapsedMilliseconds()
While AvailableSerialPortInput(comID) > 0
If ReadSerialPortData(comID, @Buffer, 1) ; Read Byte
Text = Text + Chr(Buffer)
SetGadgetText(#Label01, Text)
Receive = 1
EndIf
Wend
Timediff=ElapsedMilliseconds()-Time
If Receive:SetGadgetText(#Gadget_69,'Receive : '+Str(Timediff)+' ms'):EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_CTS)
SetGadgetColor(#Gadget_58,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_58,#PB_Gadget_BackColor,#Red)
EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_DSR)
SetGadgetColor(#Gadget_59,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_59,#PB_Gadget_BackColor,#Red)
EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_DCD)
SetGadgetColor(#Gadget_60,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_60,#PB_Gadget_BackColor,#Red)
EndIf
If GetSerialPortStatus(comID,#PB_SerialPort_RI)
SetGadgetColor(#Gadget_61,#PB_Gadget_BackColor,#Green)
Else
SetGadgetColor(#Gadget_61,#PB_Gadget_BackColor,#Red)
EndIf
Else
SetGadgetColor(#Gadget_58,#PB_Gadget_BackColor,#Red)
SetGadgetColor(#Gadget_59,#PB_Gadget_BackColor,#Red)
SetGadgetColor(#Gadget_60,#PB_Gadget_BackColor,#Red)
SetGadgetColor(#Gadget_61,#PB_Gadget_BackColor,#Red)
EndIf
Case #PB_Event_Menu
Select EventMenu()
Case #Menu_Return
Select GetActiveGadget()
Case #Gadget_Xon
If comID And IsSerialPort(comID)
SetSerialPortStatus(comID,#PB_SerialPort_XonCharacter,Val(GetGadgetText(#Gadget_Xon))&$FF)
EndIf
Case #Gadget_Xoff
If comID And IsSerialPort(comID)
SetSerialPortStatus(comID,#PB_SerialPort_XoffCharacter,Val(GetGadgetText(#Gadget_Xoff))&$FF)
EndIf
EndSelect
EndSelect
Case #PB_Event_Gadget
GadgetID = EventGadget()
Select GadgetID
Case #Key_Quit ;Quit
Break
Case #Key_SR ;Send and Get Data
If comID And IsSerialPort(comID) ;Send the string to Communication Port
EOL=13
Text = GetGadgetText(#Text01)
SetGadgetText(#Label01,')
Select 1
Case 1
WriteSerialPortString(comID,Text) ; forced conversion to ASCII (Standard in PB4.20)
Case 2
WriteSerialPortString(comID,Text,#PB_Unicode) ; convert to UNICODE
Case 3
WriteSerialPortString(comID,Text,#PB_UTF8) ; convert to UTF8
EndSelect
WriteSerialPortData(comID,@EOL,1)
Time=ElapsedMilliseconds()
While AvailableSerialPortOutput(comID):Wend
Timediff=ElapsedMilliseconds()-Time
SetGadgetText(#Gadget_69,'Send : '+Str(Timediff)+' ms')
Else
MessageRequester('SerialPortDemo','Communication Port not open !')
EndIf
Case #Key_RTSON ; Set RTS ON
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_RTS,1):EndIf
Case #Key_RTSOFF ; Set RTS OFF
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_RTS,0):EndIf
Case #Key_DTRON ; Set DTR ON
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_DTR,1):EndIf
Case #Key_DTROFF ; Set DTR OFF
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_DTR,0):EndIf
Case #Key06 ; Set TXD ON
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_TXD,1):EndIf
Case #Key07 ; Set TXD OFF
If comID And IsSerialPort(comID):SetSerialPortStatus(comID,#PB_SerialPort_TXD,0):EndIf
Case #Key08 ;Open Com Port
If comID And IsSerialPort(comID) ;If one port is used then close it
CloseSerialPort(comID)
EndIf
GetComDescription()
comID = OpenSerialPort(#PB_Any,ComPort,Baud,Parity,DataBits,StopBits,Handshake,1024,1024)
If comID = 0
MessageRequester('SerialPortDemo','OpenSerialPort() failed !')
EndIf
Case #Key09 ;Close Com Port
If comID And IsSerialPort(comID)
If CloseSerialPort(comID)
comID = 0
EndIf
EndIf
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
If comID And IsSerialPort(comID)
CloseSerialPort(comID) ;Close the Communication Port
EndIf
CloseWindow(#Window_0) ;Close the window
End

Last edited by ABBKlaus on Mon Feb 07, 2011 7:50 pm, edited 8 times in total.

Top
Posted: Wed Jan 02, 2008 4:57 pm
Addict

Joined: Tue Apr 29, 2003 9:18 pm
Posts: 1488
Location: North by Northwest
Thanks.
BTW:
Change this:
; For OpenSerialPort 'Handshake' parameter
#PB_SerialPort_NoHandshake = #HandShakeModeNone
#PB_SerialPort_RtsEnable = #HandShakeModeRtsEnable
#PB_SerialPort_RtsCts = #HandShakeModeRtsCts
#PB_SerialPort_XonXoff = #HandShakeModeXonXoff

to:
; For OpenSerialPort 'Handshake' parameter
#PB_SerialPort_NoHandshake = 0
#PB_SerialPort_RtsEnable = 1
#PB_SerialPort_RtsCts = 2
#PB_SerialPort_XonXoff = 3

otherwise the MvComRes.res of the MVCOM library is needed in order to compile.


Top
Posted: Wed Jan 02, 2008 5:04 pm
Moderator

Joined: Sat Apr 26, 2003 1:11 am
Posts: 1395
ideal for testing (with less real serial ports then) is the
free Nullmodem-Emulator, found here:
http://com0com.sourceforge.net/

_________________
SPAMINATOR NR.1


Top
Posted: Wed Jan 02, 2008 5:07 pm
Addict

Joined: Sat Apr 10, 2004 1:20 pm
Posts: 1143
Location: Germany


Top
Post subject:
Addict

Joined: Mon May 14, 2007 2:13 am
Posts: 979
Location: Darling River
Nice demo code ABB.
Thanks for sharing.
Rings. That is a handy link matey.

_________________
PureBasic Rocks! Even More! And More!
PureBasic 5, Now We're Really Rockin!


Top
Posted: Sat Jan 05, 2008 3:13 pm
Addict

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4655
Location: Spa, relaxing and thinking, and learning...

_________________
http://www.zeitgeistmovie.com
While world=business:world+mafia:Wend


Top
Post subject:
Addict

Joined: Sat Apr 10, 2004 1:20 pm
Posts: 1143
Location: Germany
Changed my above example to work with PB4.20 Beta 3
- stopbits are now defined as float in Beta3

Last edited by ABBKlaus on Fri Apr 11, 2008 6:10 pm, edited 2 times in total.

Top
Posted: Tue Apr 08, 2008 4:02 pm
Enthusiast

Joined: Sun Jan 11, 2004 11:34 am
Posts: 274
Location: France


Top
Post subject:
Addict

Joined: Sat Dec 31, 2005 5:24 pm
Posts: 2970
Location: Where ya would never look.....
Thank you
P.S. There should be a forum just for code that needs just beta versions of PB.


Top

Ps/2 Port

Posted: Mon Feb 07, 2011 7:51 pm
Addict

Joined: Sat Apr 10, 2004 1:20 pm
Posts: 1143
Location: Germany


Top

Serial Port Rs-232

Post subject: Re: SerialPort Demo (PB4.5)
Enthusiast

Joined: Tue Oct 31, 2006 4:34 am
Posts: 540
Hi ABBKlaus
Nice program. Thank you very much.


Top
Posted: Mon Sep 05, 2011 2:45 pm
User

Joined: Tue Aug 02, 2005 2:23 pm
Posts: 10
Really great work
Is it possible to get the text from the comport
Uploaded with ImageShack.us
In this case 'HUWAI Mobile Connect - 3G PC UI Interface'
Thanks in advance


Top
Posted: Mon Sep 05, 2011 4:25 pm
Addict

Joined: Sat Apr 10, 2004 1:20 pm
Posts: 1143
Location: Germany
Hi r7mk4,
No sorry, this would break the os-compatibility.
But there are some code snippets here on the forum,
You just have to search for enumeration
BR Klaus


Top
Posted: Mon Sep 05, 2011 4:51 pm
Addict

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4813
Location: Lyon - France
A little bit later, but i don't see your great works before
Thanks for sharing

_________________
The happiness is a road...
Not a destination


Top
Purebasic Serial Port Example
Posted: Mon Apr 15, 2013 4:40 pm
New User

Joined: Mon Apr 15, 2013 4:31 pm
Posts: 4
This works well in Windows after I define red and green.
What about Linux?
I know I have to change designation of the serial port but what about the user interface.
The text is all too big and the buttons don't seem to work right.
Thanks!


Top
Purebasic serial port example list
Page 1 of 1
[ 15 posts ]