Announcement

Collapse
No announcement yet.

Model Train Control program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Model Train Control program

    I have hardware and software from CTI-Electronics that lets me use my computer to run model trains. They used Visual BASIC to write a program that runs model trains. I can use Visual BASIC to write programs that run model trains. Visual BASIC programs use the MSCOMM control to communicate with the hardware using the serial port. I know that PowerBASIC has functions for using serial ports but I am not sure if I can use PowerBASIC to write programs to run model trains. What will I have to do to use PowerBASIC to write programs to run model trains? Jeffrey.

  • #2
    There are two parts to serial comms - establishing a communication link and establishing a protocol to send data in a meaning ful way to the other device (and to receive data back as necessary).

    1. The actual comms link - this is the "channel" that establishes a way to communicate between the PC and the train controller. All you need to do is open a COMM port with the correct parameters (baud rate, bits, stop bits, parity mode, etc). This is the same as what VB provides, but the implementation will be different. This is the easy part - the train controller should have some spec's on it's serial link requirements (most common links would be 9600 baud, 1 stop bit with no parity). Handshaking may or may not be used - it depends on the device.

    2. The communications protocol - this determines the structure or format of the actual data that is to be sent across the comms link, so that the device can understand the data and react to it accordingly. I would suspect that you have some sort of information on the protocol/data format that it uses? There many "generic" specifications for protocols, mainly to deal with sending large volumes of binary data with error testing, etc.

    If you know the comms parameters and the protocol, then the example COMM's file that comes with PB/DLL will contain all you need to construct an app that will communciate with your controller.


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Email me a sample of your vb code and I'll take a whack at it. I like real time comm programming.
      [email protected]

      ------------------
      Warped by the rain, Driven by the snow...

      jimatluv2rescue.com

      Comment


      • #4
        Lance, Please visit WWW.CTI-Electronics.COM web site, go to "Software Junkies" page, and download "CTI Network Protocol Definition" file. Unzip the file and you will get a sample program, the CTIDRV.BAS file, and a manual saved in PDF format. The CTIDRV.BAS file interfaces Visual BASIC programs with the train control hardware. Jeffrey.


        [This message has been edited by Jeffrey Morris (edited June 27, 2000).]

        Comment


        • #5
          Jim, Please visit WWW.CTI-Electronics.COM web site, go to "Software Junkies" page, and download "CTI Network Protocol Definition" file. Unzip the file and you will get a sample program, the CTIDRV.BAS file, and a manual saved in PDF format. The CTIDRV.BAS file interfaces Visual BASIC programs with the train control hardware. Jeffrey.


          [This message has been edited by Jeffrey Morris (edited June 27, 2000).]

          Comment


          • #6
            Thank you for the link to the the protocol definition file, but I regretfully must decline the offer - unfortunately I just do not have the time to write the code for you.

            Lance
            mailto:[email protected]

            Comment


            • #7
              Lance, I thought that you will look at the Visual BASIC code and tell me how to write PowerBASIC code. I know that Visual BASIC program uses MSCOMM control and that PowerBASIC program uses its own functions related to serial port. Jeffrey.

              Comment


              • #8
                PB/DLL comes with a simple terminal example file which should be more then enough to get you going - you may need to change some of the comms parameters to match the train controller's RS232 port (ie, speed & parity, etc).

                All that remains is for you to implement the protocol (the format of the data you send to the controller).

                In addition, you have the train controller hardware, so you are in the best place to test your code as you debug it - we would only be guessing at the protocol implementation.

                Unfortunately we (PowerBASIC) cannot write your code for you... we'll help you when you get stuck, but you have to at least *try* to learn these things yourself.

                I hope you understand that just do not have the time and resources to convert this for you, and I hope this does not put you off the project.




                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Hi,
                  This is not the Train program, but I strip the unnecessary code
                  on the terminal program and test it with pin 2 and pin 3 at DB9
                  comm 1 connector shorted.
                  Since the train program is using polling mode, so you can use the
                  timer to check the buffer whether any string send in and do the
                  necessary processing.

                  Code:
                  '------------------------------------------------------------------------------
                  '
                  '  Be sure to set the $ComPort constant to the appropriate COM port
                  '  before compiling this example!
                  '  !!! You need to Short Pin 2 and Pin 3 at DB9 to get to work!!!
                  '
                  '------------------------------------------------------------------------------
                  
                  #COMPILE EXE
                  #REGISTER NONE
                  #DIM ALL
                  #INCLUDE "WIN32API.INC"
                  
                  $ComPort            = "COM1"
                  $AppTitle           = "PB/DLL 6.0 Simplest Comm Example"
                  
                  %IDC_EDIT1          = 101
                  %IDC_EDIT2          = 102
                  %IDC_SEND           = 103
                  %IDC_RECEIVETXT     = 104
                  %IDC_QUIT           = 105
                  
                  GLOBAL hComm        AS LONG
                  GLOBAL InboundData AS STRING
                  GLOBAL Stuf        AS STRING
                  
                  DECLARE FUNCTION StartComms                              AS LONG
                  DECLARE FUNCTION SendLine(STRING)                        AS LONG
                  DECLARE FUNCTION ReceiveData(BYVAL LONG)                 AS LONG
                  DECLARE FUNCTION EndComms                                AS LONG
                  
                  '==========================================================================
                  CALLBACK FUNCTION Send_Callback() AS LONG
                      DIM SendText AS STRING
                  
                      ' Obtain the text to send from the edit control
                      CONTROL GET TEXT CBHNDL, %IDC_EDIT1 TO SendText
                  
                      ' Send the line to the comm port
                        SendLine(SendText)
                  
                  END FUNCTION
                  
                  '=======================================================================================
                  
                  CALLBACK FUNCTION ReceiveTxt_Callback() AS LONG
                  DIM dummy AS STRING
                  
                   CALL ReceiveData(CBHNDL)
                  
                   CONTROL SET TEXT CBHNDL,%IDC_EDIT2,Stuf 'InboundData ''See the ReceiveData Function
                  
                  END FUNCTION
                  
                  '===========================================================================
                  
                  CALLBACK FUNCTION Quit_Callback() AS LONG
                      ' Kill the dialog and let PBMAIN() continue to execute Endcomm
                      DIALOG END CBHNDL, 0
                  END FUNCTION
                  
                  '===========================================================================
                  
                  FUNCTION PBMAIN
                  
                    ' Build our GUI interface.
                    DIM hDlg AS LONG, Txt(1:1) AS STRING, Result AS LONG
                  
                    ' Initialize the port ready for the session
                    IF ISFALSE StartComms THEN
                        MSGBOX "Failure to start communications!",, $AppTitle
                        EXIT FUNCTION
                    END IF
                  
                    ' Create a modal dialog box
                    DIALOG NEW 0, $AppTitle,100, 30, 330, 150, %WS_POPUP OR %WS_VISIBLE OR %WS_CLIPCHILDREN OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX, 0 TO hDlg
                    ' Add our application controls
                    CONTROL ADD LABEL,    hDlg, -1, "You need to short Pin2 and Pin3 on " & $ComPort, 9, 20, 200, 10, 0
                    CONTROL ADD LABEL,    hDlg, -1, "Te&xt to send", 9, 40, 100, 10, 0
                    CONTROL ADD TEXTBOX,  hDlg, %IDC_EDIT1, "BASIC", 9, 50, 257, 12
                    CONTROL ADD BUTTON,   hDlg, %IDC_SEND, "Send &Text", 273, 50, 50, 14 ,CALL Send_Callback
                    CONTROL ADD BUTTON,   hDlg, %IDC_RECEIVETXT, "&Text Receive", 273, 75, 50, 14, %WS_GROUP OR %WS_TABSTOP CALL ReceiveTxt_Callback
                    CONTROL ADD BUTTON,   hDlg, %IDC_QUIT, "&Quit", 273, 100, 50, 14, %WS_GROUP OR %WS_TABSTOP CALL Quit_Callback
                    CONTROL ADD TEXTBOX,  hDlg, %IDC_EDIT2, "", 9, 75, 257, 12
                  
                    ' Start the dialog box & run until DIALOG END executed.
                    DIALOG SHOW MODAL hDlg
                  
                    'After Dialog End, Execute here.
                    CALL EndComms
                  
                  END FUNCTION
                  
                  '===========================================================================
                  
                  FUNCTION StartComms AS LONG
                  
                    COMM OPEN $COMPORT AS #hComm
                    IF ERRCLEAR THEN EXIT FUNCTION          ' Exit if port cannot be opened
                  
                    COMM SET #hComm, BAUD     = 9600       ' 9600 baud
                    COMM SET #hComm, BYTE     = 8           ' 8 bits
                    COMM SET #hComm, PARITY   = %False      ' No parity
                    COMM SET #hComm, STOP     = 1           ' 1 stop bit
                    COMM SET #hComm, TXBUFFER = 4096        ' 4k transmit buffer
                    COMM SET #hComm, RXBUFFER = 4096        ' 4k receive buffer
                  
                    FUNCTION = %TRUE
                  
                  END FUNCTION
                  
                  '===========================================================================
                  
                  FUNCTION SendLine(SendText AS STRING) AS LONG
                      COMM PRINT #hComm, SendText
                  END FUNCTION
                  
                  '===========================================================================
                  
                  FUNCTION ReceiveData(BYVAL hWnd AS LONG) AS LONG
                  
                  
                    DIM Stuf AS STRING
                    DIM Qty AS LONG
                  
                  
                        ' Test the RX buffer
                        Qty = COMM(#hComm, RXQUE)
                  
                        COMM RECV #hComm, Qty, Stuf 'read incoming characters
                  
                        'if you using Variable InBoundData to hold the Incomming
                         'Data, you can Strip those unwanted CHRacter, but
                         'it will stay at the text box.
                  
                         'InBoundData = InBoundData & Stuf
                          ' 'strip out LF characters
                         'REPLACE CHR$(10) WITH "" IN InBoundData
                  
                  END FUNCTION
                  
                  '===========================================================================
                  
                  
                  FUNCTION EndComms() AS LONG
                    DIM dummy AS STRING
                  
                    ' Flush the RX buffer & close the port
                    SLEEP 1000
                  
                    IF COMM(#hComm, RXQUE) THEN
                      COMM RECV #hComm, COMM(#hComm, RXQUE), dummy
                    END IF
                  
                    COMM CLOSE #hComm
                  
                  END FUNCTION
                  
                  '===========================================================================
                  '===========================================================================
                  It may not be as easy as using VB, but is worth to rewrite the code
                  in PB, not only for speed and size, but open up the new route to Windows
                  Cheer!




                  ------------------

                  Comment

                  Working...
                  X