The following is a demo program that uses a low cost ActiveX control to add RS232 Serial routines to Alpha5 Version 10 and possibly Version 8 & 9. Should also work in version 11 if you don't want to use it's built in .NET support and serial routines provided by .NET.
This sample script creates a Dialog box that allows the serial port parameters to be set and the port opened. Provision for sending commands and receiving text are provided. With little work this dialog can be made modeless and can send text to forms and global variables etc.
This sample script creates a Dialog box that allows the serial port parameters to be set and the port opened. Provision for sending commands and receiving text are provided. With little work this dialog can be made modeless and can send text to forms and global variables etc.
Code:
'Date Created: 17-Mar-2014 10:56:11 AM 'Last Updated: 20-Mar-2014 08:43:59 AM 'Created By : Paul Verboom 'Updated By : Paul Verboom '---------- 'Ser-Config '---------- 'Configure and test serial communications 'using StrokeReader ActiveX Control 'http://strokescribe.com/en/serial-port-about.html ' 'Well documented, low cost ($70) unlimited ditribution 'serial port interface ActiveX control with event-driven, asynchronous data transfer. 'free demo version of StrokeReader puts up occasional nag screen 'but is perfectly adequate for testing purposes. 'This code devolped in Alpha Ver 10 should work in version 9 as well 'Alpha5 Ver 11 has support for .NET and it's serial routines. 'Name: StrokeReader Control 'Prog Id: strokescribe.strokereaderctrl.1 'UUID: {7E42B8C5-73BE-4806-8904-FF4080A696EC} '----------------------------- '// Set up Serial paramters -- '----------------------------- DIM PortName as C 'Parameters for serial Port DIM BaudRate as C DIM DataBits as C DIM Parity as C DIM StopBits as C 'PortName = "COM4 -" 'Default Parameters for serial port 'BaudRate = "9600" 'DataBits = "8" 'Parity = "None" 'Stopbits = "1" '------------------------------------- '// Retrieve settings from registry -- '------------------------------------- DIM PT as P PT.PortName = "" PT.BaudRate = "9600" PT.DataBits = "8" PT.Parity = "None" PT.Stopbits = "1" :REGISTRY.LOAD_SETTINGS("PineTracker",PT) PortName = PT.PortName 'Can not use dot variables in Dialogs BaudRate = PT.BaudRate 'so copy them to regular strings DataBits = PT.DataBits Parity = PT.Parity StopBits = PT.StopBits '------------------------------------- '// Set up variables used by Dialog -- '------------------------------------- delete TextBld 'Variables used by xBasic code DIM SHARED TextBld as C = "" 'That processes StrokeReader events delete TextIn 'Clear variable before use DIM SHARED TextIn as C = "" 'in case event code somehow Delete Status 'creates a variable with local scope DIM SHARED Status as C = "" DIM SHARED TextOut as C 'The text to be sent to serial port DIM Result as C 'The result after exiting dialog DIM s AS C 'used to build list of available ports DIM Ports as C DIM Port as C DELETE a_Port 'Building list of ports must be done within DIM a_Port[0] as c 'the Dialog as ACtiveX Control is Used DELETE a_BaudRate 'Define available/common Bud rates DIM a_BaudRate[10] as c dim temp_list as c temp_list = <<%list% 115200 57600 38400 19200 9600 1200 300 %list% a_BaudRate.initialize(temp_list) '----------------------------- '// Set some default values -- '----------------------------- DIM DialogTtl as C = "StrokeReader in Alpha5 Demo" 'Title for Dialog dim Sample_Textout as c 'Sample text to send out serial port Sample_Textout = <<%list% This is a test. This is the second test. %list% '----------------------------------- '\\ Complete variable assignments -- '----------------------------------- DELETE a_SendText 'Define pull down for text to send out serial port DIM a_TextOut[line_count(Sample_Textout)] as c a_TextOut.initialize(Sample_Textout) '-------------------------------------------------------------------- '\\ Define ObjectName required to place ActiveX Control in xDialog -- '-------------------------------------------------------------------- dim SR as P dim SR.object as P dim SR.class as C SR.class = "strokescribe.strokereaderctrl.1" SR.Events =<<%code% function commevent as v (Evt as N,data as C) 'Evt - Can be one of following values: 'EVT_DISCONNECT (=0) 'EVT_DATA (=1) 'EVT_SERIALEVENT (=2) 'EVT_CONNECT (=3) 'Data - Can contain an array of received bytes or a text string or ' an integer mask value indicating the type of serial port events that occurred. Select Case Evt = 0 Status = "Disconnected" Case Evt = 3 Status = "Connected" TextTest = "Can set it here" Case Evt = 1 TextBld = TextBld + data while chr(13) $ TextBld S = left(TextBld,AT(CHR(13),TextBld)-1) TextBld = substr(TextBld,AT(CHR(13),TextBld)+1) 'while Left(TextBld,1) $ crlf() ' TextBld = substr(TextBld,2) 'wend TextIn = TextIn + S + crlf() '!ui_msg_box("TextIn",TextIn,UI_OK) '!ui_beep(UI_ASTERISK_BEEP) wend Case Evt = 3 Status = "Control line Change" 'Could be elborated to to include information from Data value Case Else Status = "Unknown serial Event Type" end select end function %code% '--------------------------- '// Define the Dialog Box -- '--------------------------- 'Notes '{ActiveX} - Places ActiveX control dialog, requires dot variable defined prepared previously '{Watch=} - Fires an event when a variable changes such as incoming text '{StartUp} - Defines a event to trigger such as connect when dialog is displayed. ' 'The Textin window must be read only [%r%.40VARIABLENAME] otherwise processing an event 'will cause the current value on screen to be assigned to the Variable overwritting 'the incoming text placed in the variable by the the StrokeReader event ' Dialog =<<%dlg% {region} <%T= Press to reread available Serial Ports%&Read Ports!Ports> | [.80,5PortName^#a_Port]; {activex=1,1SR} {startup=Ports} {Watch=TextIn!Show} Baud Rate:| [%v%.7BaudRate^=a_BaudRate]; {region}Data Bits:{endregion}| {region}(DataBits:5)(DataBits:6)(DataBits:7)(DataBits:8){endregion}; {region}Parity:{endregion}| {region}(Parity:None)(Parity:Odd)(Parity:Even)(Parity:Mark)(Parity:Space){endregion}; ; {region}Stop Bits:{endregion}| {region}(StopBits:1)(StopBits:1.5)(StopBits:2){endregion}; ; <%T= Press to send enterred Text%&Send!Send> | [%v%.77TextOut^+a_TextOut]; TextIn:| [%rmw%.80,10TextIn!show]; Status:| [.80Status]; {endregion}; {line=1,0}; {region} {SP4} <%T=Apply port settings and open connection%20&Connect!Connect> {SP4} <%T=Disconnect from port%20&Disconnect!Disconnect> {SP4} <%T=Save Settings and Exit%15&Exit> {endregion}; %dlg% '----------------------------------- '// Define code for event buttons -- '----------------------------------- Code =<<%code% if a_dlg_button = "Connect" then 'Connect to port set all parameters first a_dlg_button = "" SR.object.datamode = 1 'Data mode is Text Only in this demo S = tagged_pattern("COM1[^0-9 ]","1",PortName) 'Extract Port Number from full name we built SR.object.port = val(S) SR.object.baudrate = val(BaudRate) 'Dialog returns actual value for BaudRate select 'Convert Stop bits values to flags needed case StopBits = "1" ' by Stroke Reader SR.object.stopbits = 0 case StopBits = "1.5" SR.object.stopbits = 1 case StopBits = "2" SR.object.stopbits = 2 end select SR.object.databits = val(DataBits) select 'Convert Parity names to actual to case Parity = "None" 'Values needed by stroke Reader SR.object.parity = 0 case Parity = "Odd" SR.object.parity = 1 case Parity = "Even" SR.object.parity = 2 case Parity = "Mark" SR.object.parity = 3 case Parity = "Space" SR.object.parity = 4 end select SR.object.connected = .T. if SR.object.error <> 0 'Check if any error opening port ui_msg_box(DialogTtl,SR.object.errordescription,UI_ATTENTION_SYMBOL+UI_OK) end if end if if a_dlg_button = "Disconnect" then 'Close the Connection a_dlg_button = "" IF SR.object.connected = .F. Status = "Com Port already closed" else SR.object.connected = .F. end if end if if a_dlg_button = "Show" then 'refresh dialog Forcing display of a_dlg_button = "" 'incoming text in dialog end if 'event triggered by variable watch if a_dlg_button = "Send" 'Send the entered of selected text a_dlg_button = "" DIM S as C IF a_TextOut.Find(TextOut) < 1 'add text to array for future use a_TextOut[] = TextOut end if S = TextOut IF right(S,1) <> CHR(13) 'add carriage return if none S = S + CHR(13) end if SR.object.send(S) end if if a_dlg_button = "Ports" 'refresh the list of available a_dlg_button = "" ' serial ports REDIM a_Port[0] as C Ports = SR.object.PortsAvailable PortList = comma_to_crlf(Ports) AvailablePorts = "" for each Port in PortList s = SR.object.GetPortFriendlyName(Port.value) a_Port[] = "COM" + Port.value + " - " + S next end if if a_dlg_button = "Exit" 'close the port before control gets unloaded SR.object.connected = .F. 'a_dlg_button is no cleared so we will exit after it's press end if %code% '------------------------------- '// Finally put up the Dialog -- '------------------------------- Result = ui_dlg_box(DialogTtl,Dialog,Code) '-------------------------------- '// Write settings to registry -- '-------------------------------- DIM PT as P PT.PortName = PortName PT.BaudRate = BaudRate PT.DataBits = DataBits PT.Parity = Parity PT.Stopbits = Stopbits :REGISTRY.SAVE_SETTINGS("PineTracker",PT) end
Comment