Get a Jumpstart with our Sample Apps
Results 1 to 10 of 10

Thread: Serial Communications Active X Demo

  1. #1
    Member
    Real Name
    Paul Verboom
    Join Date
    Apr 2006
    Location
    Halifax, Nova Scotia, Canada
    Posts
    128

    Default Serial Communications Active X Demo

    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.

    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

  2. #2
    Member nigeldude's Avatar
    Real Name
    Nigel Freeman
    Join Date
    Jul 2006
    Location
    Belize City, Belize
    Posts
    286

    Default Re: Serial Communications Active X Demo

    Hey Paul, Im drowning here, This topic of serial port communication is killing me, Ive all over this board seeking the needed knowledge to send and receive data between my PC and mobile bar code scanner that scans product and logs the qty of each. I was trying with Eltima Serial Port activeX, but they don't offer the support i need. I was very excited when I came across this post, and I was able to see the dialog ,set the port number and open the port, however nothing happened when i clicked on the "Send" button. I did download and install the suggested ActiveX. Would appreciate any assistance you can offer.

  3. #3
    Member
    Real Name
    Paul Verboom
    Join Date
    Apr 2006
    Location
    Halifax, Nova Scotia, Canada
    Posts
    128

    Default Re: Serial Communications Active X Demo

    I feel your pain. I too struggled with implementation Serial Communication in Alpha5. Their is a whole pile of stuff you need to get just right in order to get it to work. Hardware, communication parameters, events in Alpha5, not to mention implementing a library in an environment it was never tested in, and no example code exist for. Anyone of these things can keep the whole thing from working.

    My advice to anyone trying this it is to keep it as simple as possible at first, and as you get the basic stuff working add more to the whole.

    Start by Download and installing a copy of Real Term http://realterm.sourceforge.net/. This program is your friend when dealing with serial interfaces. You will see lots of examples on the internet showing hyperterm being used to debug serial interfaces. Hyperterm is used in these examples because it came with windows no other reason, certainly not because it works for this purpose. Learn Real Term and use it for testing, I cannot stress this enough.

    Connect a loopback connector to the RS232 port on the PC you are using and then open that port with Real Term. Set real term to not echo characters and anything you type should be echoed back to the terminal screen only when the loopback connector is in place. This verifies the serial port is working and accessible. This may seem like a redundant step put when dealing with serial ports in Windows it can not be assumed that they will work as expected. Just google RS232 loopback connector to find out what they are and how to build one it only takes one wire and and one connector, you can cut up a RS232 cable if need be.

    Next disconnect Real Term from the port and run my code. Once again anything you send should should appear back. If it does not try this trouble shooting step.

    Connect the port used by my code to another port running RealTerm, using a null modem cable. This second port with Real Term can be on same PC or separate PC. If you are using another port be sure to check it with the loopback connector first.Now you can see if my code is sending anything and not receiving or receiving but not sending. Once again use Google to find out what a null modem cable is and how to build one. If My code does nor work at all test your hardware setup by running RealTerm on both ports. Remember the serial port parameters must match Baud rate, Bits, Parity, etc.

    If my code is sending and receiving properly now you need to test the hardware (in this case scanner). Connect the device to a port and run (connect) RealTerm to the port. Make sure you don't need a null modem cable for the device or active DTR CTS type signals. Once again Google this stuff to find out more. Finally you will need to consult the documentation of the hardware (scanner) to see what communication parameters it is expecting, Baudrate, Parity etc. If you don't have that documentation the whole project just got much harder, if not impossible. RealTerm does make it easy try numerous combinations of parameters so it may be possible to puzzle these things out. Use Google to find out what common RS232 communication parameters are as starting point. As a side note most scanners I have seen only put out data when they successfully read a bar code.

    I hope this gives anyone trying to make the Alpha5 serial port thing work some useful pointers.
    Last edited by pboomwork; 01-24-2015 at 12:01 PM.

  4. #4
    Member
    Real Name
    Paul Verboom
    Join Date
    Apr 2006
    Location
    Halifax, Nova Scotia, Canada
    Posts
    128

    Default Re: Serial Communications Active X Demo

    Although it is stated in the Code I should repeat you need to install the StrokeReader ActiveX control before running the code in this post. I can be downloaded from here https://strokescribe.com/en/serial-port-download.html
    The free demo version of StrokeReader puts up occasional nag screen but is perfectly adequate for testing purposes.
    The unlimited distribution version is only $70. This is a real deal compared to the other options out there.

  5. #5
    Member
    Real Name
    Dick Bruno
    Join Date
    Dec 2007
    Posts
    10

    Default Re: Serial Communications Active X Demo

    Paul, Thanks so much for you post .... your demo is great and I "almost" have success. Seems that in my environment the receive data event won’t fire. It sends data but will not receive. Have you tried it with Alpha v12?

    The hardware (using a null modem cable) between 2 PCs is working; have success using the StrokeReader samples in VB and Excel (VBA) . I even added a line to your code to show the data event on the status line:
    Case Evt = 1
    Status = "Data"
    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()

    Any ideas would be most welcome.

    FYI – I have a great tool for serial work “Matko Stream Viewer” http://www.matko.com/downloads.htm

  6. #6
    Moderator Peter.Greulich's Avatar
    Real Name
    Peter Greulich
    Join Date
    Apr 2000
    Location
    Boston, MA
    Posts
    11,486

    Default Re: Serial Communications Active X Demo

    Not sure what you are trying to do. Here is a simple example of intercepting a barcode scanner set to UPC-A:

    Code:
    dim sp as System::IO::Ports::SerialPort
    dim Buffer as C
    dim SizeRead as N
    dim BarCodeSize as N = 12
    dim ReadLog as C
    
    
    ui_dlg_box("Bar Code Scan",<<%dlg%
    {can_exit=close}
    {startup=init}
    {timer=CheckScanner}{interval=.5}
    {region}
    {region}
    [%MRV%.30,20ReadLog]
    {endregion}
    | {region}<Start>;<Stop>;<Close!close>
    {endregion}
    {endregion}
    %dlg%,<<%code%
    
    if a_dlg_button = "init" then
    	a_dlg_button = ""
    	sp.PortName = "COM1"
    	
    else if a_dlg_button = "Start" then
    	a_dlg_button = ""
    	hourglass_cursor(.t.)
    	sp.Open()
    	hourglass_cursor(.f.)
    	
    else if a_dlg_button = "Stop" then
    	a_dlg_button = ""
    	hourglass_cursor(.t.)
    	sp.Close()
    	hourglass_cursor(.f.)
    	
    else if a_dlg_button = "CheckScanner" then
    	a_dlg_button = ""
    	hourglass_cursor(.t.)
    	if sp.IsOpen
    		BytestoRead = sp.BytesToRead
    		if BytesToRead >= BarCodeSize
    			Buffer = ""
    			on error resume next
    			'debug(1)
    			
    			for i = 1 to BytesToRead
    				Buffer = Buffer + chr(sp.ReadChar())
    			next
    			
    			on error goto 0
    			if len(Buffer) >= BarCodeSize
    				StatusBar.Set_Text(Buffer)
    				ReadLog = ReadLog + Buffer + crlf()
    			else
    				StatusBar.Set_Text("Read error: " + Buffer)
    				' .. handle read error
    			end if
    		else
    			StatusBar.Set_Text("Bytes available = " + BytesToRead)
    		end if
    	else
    		StatusBar.Set_Text("Port not open.")
    	end if
    	hourglass_cursor(.f.)
    end if
    
    %code%)
    Works very well. The real code writes data to SQL or dbf depending upon the application (in my situation at least).

  7. #7
    Member
    Real Name
    Dick Bruno
    Join Date
    Dec 2007
    Posts
    10

    Default Re: Serial Communications Active X Demo

    Peter, Thanks for your code sample ... will give it a try later today. My application is reading an industrial truck scale. When you
    send it a 2 character code, it returns the gross weight... pretty simple task.

  8. #8
    Member
    Real Name
    Dick Bruno
    Join Date
    Dec 2007
    Posts
    10

    Default Re: Serial Communications Active X Demo

    Peter, Thank you! Thank you! ... tested great. Since is uses .Net I won't need to purchase ActiveX 3rd party product. You're a life saver ... I've been fooling with this for a couple of days and need to have my app tested and ready for use by the start of the new year.

  9. #9
    Member
    Real Name
    Paul Verboom
    Join Date
    Apr 2006
    Location
    Halifax, Nova Scotia, Canada
    Posts
    128

    Default Re: Serial Communications Active X Demo

    Glad you got your serial scale sorted. To answer your question I've never tried this libray in anything other then Ver 10.5 which is what I own ,runtimes and all. Main reason I went this route no .NET support in Ver 10.5

  10. #10
    Moderator Peter.Greulich's Avatar
    Real Name
    Peter Greulich
    Join Date
    Apr 2000
    Location
    Boston, MA
    Posts
    11,486

    Default Re: Serial Communications Active X Demo

    Main reason I went this route no .NET support in Ver 10.5
    I'm pretty sure .NET runs just fine in v10. It's just not documented until v11. But I think it works.

Similar Threads

  1. dot net serial port communications
    By Peter.Greulich in forum Alpha Five Version 11 - Desktop Applications
    Replies: 7
    Last Post: 04-23-2012, 02:33 PM
  2. Serial Communications
    By Scott Kurimski in forum Alpha Five Version 10 - Desktop Applications
    Replies: 2
    Last Post: 07-27-2011, 09:55 AM
  3. Serial Port Communications
    By Scott Kurimski in forum Alpha Five Version 10 - Desktop Applications
    Replies: 2
    Last Post: 11-15-2010, 03:08 PM
  4. Serial Communications (off)
    By Bill@TrackerSystems.com in forum Alpha Five Version 5
    Replies: 7
    Last Post: 07-29-2004, 04:54 AM
  5. Serial Communications
    By Gregory R. Zilliox in forum Alpha Five Version 4
    Replies: 7
    Last Post: 05-15-2001, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •