Announcement

Collapse
No announcement yet.

Using More Than One Serial Port

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

  • Using More Than One Serial Port

    Hi All,

    Does anyone have any prior experience with opening more than one serial port?
    The following code snippet will successfully open Com1, but will give the message
    "Failure to Start Com2".

    I commented out the code that opens Com1 so that Com2 would be opened first and
    then began getting "Failure to Start Com5". From this it would seem that I am
    failing to do some necessary task between the opening of the ports that will allow
    more than one to be opened.

    Incidentally, if I choose to open only one port (Com1, Com2, or Com5) the program
    runs fine and I am able to communicate over that port. I'm just unable to get more
    than one port to open at a time.

    Thank You For Any Help/Advice That You Might Contribute

    Wesley

    Code:
    #Compile Exe
    #Register None
    #Include "WIN32API.INC"
    '===========================================================
    'Define Constants
    $AppTitle   = "Simulation Program"
    $ComPort1   = "COM1"
    $ComPort2   = "COM2"
    $ComPort5   = "COM5"
    %Idt_Timer1 = 500
    %Idt_Timer2 = 501
    '===========================================================
    'Define Globals
    Global hCom1  As Long
    Global hCom2  As Long
    Global hCom5  As Long
    Global hDlg   As Long
    Global Result As Long
    
    '===========================================================
    
    Function StartCom1 As Long
      ErrClear
      Comm Open $ComPort1 As #hCom1
      If ErrClear Then Exit Function          ' Exit If Com1 Cannot Be Opened
      Comm Set #hCom1,  Baud     = 9600       ' 9600 Baud
      Comm Set #hCom1,  Byte     = 8          ' 8 Bits
      Comm Set #hCom1,  Parity   = %False     ' No Parity
      Comm Set #hCom1,  CtsFlow  = 0          ' Disable CTS Flow
      Comm Set #hCom1,  RtsFlow  = 0          ' Disable RTS Flow
      Comm Set #hCom1,  Xinpflow = 0          ' Disable Xon/Xoff on Input
      Comm Set #hCom1,  Stop     = 0          ' 1 Stop Bit
      Comm Set #hCom1,  TxBuffer = 1024       ' 1k Transmit Buffer
      Comm Set #hCom1,  RxBuffer = 1024       ' 1k Receive Buffer
      Function = %True
    End Function
    
    '===========================================================
    
    Function StartCom2 As Long
      ErrClear
      Comm Open $ComPort2 As #hCom2
      If ErrClear Then Exit Function          ' Exit If Com2 Cannot Be Opened
      Comm Set #hCom2,  Baud     = 9600       ' 9600 Baud
      Comm Set #hCom2,  Byte     = 8          ' 8 Bits
      Comm Set #hCom2,  Parity   = %False     ' No Parity
      Comm Set #hCom2,  CtsFlow  = 0          ' Disable CTS Flow
      Comm Set #hCom2,  RtsFlow  = 0          ' Disable RTS Flow
      Comm Set #hCom2,  Xinpflow = 0          ' Disable Xon/Xoff on Input
      Comm Set #hCom2,  Stop     = 0          ' 1 Stop Bit
      Comm Set #hCom2,  TxBuffer = 4096       ' 4k Transmit Buffer
      Comm Set #hCom2,  RxBuffer = 4096       ' 4k Receive Buffer
      Function = %True
    End Function
    
    '===========================================================
    
    Function StartCom5 As Long
      ErrClear
      Comm Open $ComPort5 As #hCom5
      If ErrClear Then Exit Function          ' Exit If Com5 Cannot Be Opened
      Comm Set #hCom5,  Baud     = 9600       ' 9600 Baud
      Comm Set #hCom5,  Byte     = 8          ' 8 Bits
      Comm Set #hCom5,  Parity   = %False     ' No Parity
      Comm Set #hCom5,  CtsFlow  = 0          ' Disable CTS Flow
      Comm Set #hCom5,  RtsFlow  = 0          ' Disable RTS Flow
      Comm Set #hCom5,  Xinpflow = 0          ' Disable Xon/Xoff on Input
      Comm Set #hCom5,  Stop     = 0          ' 1 Stop Bit
      Comm Set #hCom5,  TxBuffer = 4096       ' 4k Transmit Buffer
      Comm Set #hCom5,  RxBuffer = 4096       ' 4k Receive Buffer
      Function = %True
    End Function
    
    '===========================================================
    
    CallBack Function Quit_Callback() As Long
       ' Kill the dialog and let PBMAIN() continue
       Dialog End hDlg, 0
    End Function
    
    '===========================================================
    
    CallBack Function Dialog_Callback()
       Dim BrushLtBr As Static Long, BrushWhite As Static Long
       Select Case CbMsg
              Case %Wm_Timer
                  Select Case CbWparam
                         Case %Idt_Timer1 'Timer 1 Routine Goes Here
                         Case %Idt_Timer2 'Timer 2 Routine Goes Here
                      End Select
              Case %Wm_InitDialog
                  SetTimer  hDlg, %Idt_Timer1, 100, ByVal %Null
                  SetTimer  hDlg, %Idt_Timer2, 500, ByVal %Null
    
              Case %Wm_Destroy
                  KillTimer hDlg, %Idt_Timer1
                  KillTimer hDlg, %Idt_Timer2
              Case %Wm_CtlColorDlg
                  Function = BrushLtBr        'Set Background Color Of Entire Dialog
           End Select
       Function = %False             'Return 0 To Stop The Dialog Box Engine From Setting Focus
                Function = %False
       End Function
    
    '===============================================================
    
    
    Function PbMain
    
    'Initialize Com1
       If IsFalse StartCom1 Then
             MsgBox "Failure to Start Com1",, $AppTitle
             Exit Function
          End If
          
    'Initialize Com2
      If IsFalse StartCom2 Then
            MsgBox "Failure to Start Com2",, $AppTitle
            Exit Function
         End If
    
    'Initialize Com5: 'EZ Pump Interface
      If IsFalse StartCom5 Then
            MsgBox "Failure to Start Com5",, $AppTitle
            Exit Function
         End If
    
    'Create a Modal Dialog Box
      Dialog New 0, $AppTitle,,, 500, 380, %Ws_PopUp   Or %Ws_Visible Or %Ws_ClipChildren _
                                Or %Ws_Caption Or %Ws_SysMenu Or %Ws_MinimizeBox, 0 To hDlg
      Control Add CheckBox, hDlg, 100,        "",  13,  35,   8,   8
      Control Add Button,   hDlg, 200, "Pump 01",  35,  32,  40,  16
      Control Add CheckBox, hDlg, 300,        "",  89,  35,   8,   8
      Control Add Button,   hDlg, 400,    "Exit", 373, 350,  50,  14, Call Quit_Callback
    
    '===========================================================
    
    'Start Dialog Box
      Dialog Show Modal hDlg,  Call Dialog_Callback To Result
    
    'Flush & Close Com1, Com2, & Com5
       Comm Close #hCom1
       Comm Close #hCom2
       Comm Close #hCom5
    
       If Comm(#hCom1, RxQue) Then Comm Recv #hCom1, Comm(#hCom1, RxQue), LBR$
       If Comm(#hCom2, RxQue) Then Comm Recv #hCom2, Comm(#hCom2, RxQue), LBR$
       If Comm(#hCom5, RxQue) Then Comm Recv #hCom5, Comm(#hCom5, RxQue), LBR$
    
    End Function
    ------------------

  • #2
    Wow, I finally figured something out on my own

    The problem was solved by using FreeFile as follows . . .

    Code:
    Function StartCom1 As Long
      hCom1 = FreeFile
      Comm Open $ComPort1 As #hCom1
      If ErrClear Then Exit Function          ' Exit If Com1 Cannot Be Opened
      Comm Set #hCom1,  Baud     = 9600       ' 9600 Baud
      Comm Set #hCom1,  Byte     = 8          ' 8 Bits
      Comm Set #hCom1,  Parity   = %False     ' No Parity
      Comm Set #hCom1,  CtsFlow  = 0          ' Disable CTS Flow
      Comm Set #hCom1,  RtsFlow  = 0          ' Disable RTS Flow
      Comm Set #hCom1,  Xinpflow = 0          ' Disable Xon/Xoff on Input
      Comm Set #hCom1,  Stop     = 0          ' 1 Stop Bit
      Comm Set #hCom1,  TxBuffer = 1024       ' 1k Transmit Buffer
      Comm Set #hCom1,  RxBuffer = 1024       ' 1k Receive Buffer
      Function = %True
    End Function
    
    '===========================================================
    
    Function StartCom2 As Long
      hCom2 = FreeFile
      Comm Open $ComPort2 As #hCom2
      If ErrClear Then Exit Function          ' Exit If Com2 Cannot Be Opened
      Comm Set #hCom2,  Baud     = 9600       ' 9600 Baud
      Comm Set #hCom2,  Byte     = 8          ' 8 Bits
      Comm Set #hCom2,  Parity   = %False     ' No Parity
      Comm Set #hCom2,  CtsFlow  = 0          ' Disable CTS Flow
      Comm Set #hCom2,  RtsFlow  = 0          ' Disable RTS Flow
      Comm Set #hCom2,  Xinpflow = 0          ' Disable Xon/Xoff on Input
      Comm Set #hCom2,  Stop     = 0          ' 1 Stop Bit
      Comm Set #hCom2,  TxBuffer = 4096       ' 4k Transmit Buffer
      Comm Set #hCom2,  RxBuffer = 4096       ' 4k Receive Buffer
      Function = %True
    End Function
    
    '===========================================================
    
    Function StartCom5 As Long
      hCom5 = FreeFile
      Comm Open $ComPort5 As #hCom5
      If ErrClear Then Exit Function          ' Exit If Com5 Cannot Be Opened
      Comm Set #hCom5,  Baud     = 9600       ' 9600 Baud
      Comm Set #hCom5,  Byte     = 8          ' 8 Bits
      Comm Set #hCom5,  Parity   = %False     ' No Parity
      Comm Set #hCom5,  CtsFlow  = 0          ' Disable CTS Flow
      Comm Set #hCom5,  RtsFlow  = 0          ' Disable RTS Flow
      Comm Set #hCom5,  Xinpflow = 0          ' Disable Xon/Xoff on Input
      Comm Set #hCom5,  Stop     = 0          ' 1 Stop Bit
      Comm Set #hCom5,  TxBuffer = 4096       ' 4k Transmit Buffer
      Comm Set #hCom5,  RxBuffer = 4096       ' 4k Receive Buffer
      Function = %True
    End Function
    I am guilty of making the problem much more complicated than it really was by
    looking past the obvious. Sorry for the unnecessary post. Thanks to all
    who are/were looking into the problem.

    Wesley

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

    Comment


    • #3
      Wesley..

      Try adding FREEFILE as below...

      FUNCTION StartCom1 AS LONG
      ERRCLEAR
      hCom1 = FREEFILE '<---- NEW
      COMM OPEN $ComPort1 AS #hCom1

      FUNCTION StartCom2 AS LONG
      ERRCLEAR
      hCom2 = FREEFILE '<---- NEW
      COMM OPEN $ComPort2 AS #hCom2

      FUNCTION StartCom5 AS LONG
      ERRCLEAR
      hCom5 = FREEFILE '<---- NEW
      COMM OPEN $ComPort5 AS #hCom5




      ------------------
      Jim..
      [email protected]
      Jim..

      Comment


      • #4
        Thanks for your time Jim. Your advice was right on the money

        Wesley

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

        Comment

        Working...
        X