Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VB6, help with serial port.

Options
  • 21-02-2007 2:08pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭


    Hi, completely new to VB. My first program.
    I need to enter commands with my voice, no problem.
    I am having trouble setting up MSComm at the top of the code.

    I get ''Error 424 Object required' when running it and typing in left for example.
    Something to do with defining it?
    Here is the code, Bold is where the problem seems to be at.
    Public Sub Form_Open()
        MsgBox "This is SubA"
    
        [b]MSComm1.Settings = "9600,N,8,1" ' The TX-4-PC operates at 9600 baud
        MSComm1.CommPort = 4                 ' Change to the available comm port on your PC
        MSComm1.PortOpen = True           ' Opens comm port
        MSComm1.Output = "j"
    End Sub[/b]
    
    
    Private Sub CallSubroutine(ByVal sub_name As String)
        CallByName Me, sub_name, VbMethod
    End Sub
    
     
    
     
    
    Private Sub Form_Load()
    
    
        Dim Direction As String
        Dim variable1 As String
        Dim text As String
           
        For Counter = 1 To 10 Step 1                ' This part loops through the commands so that we can enter a number of different commands based on the size of the loop
       
            Open "u:\sample.txt" For Output As #1   'This opens the file for writing
       
            Direction = InputBox("Enter Direction") 'Dialogue box to enter direction
            Print #1, Direction         'Prints the inputted word to file
           
            Close #1                'Closes file
             
            Open "u:\sample.txt" For Input As #1    'Re-opens file to get value
           
            Input #1, variable1
            variable1 = MsgBox(variable1)       'Tells us what the input was, will not be in final code only for testing purposes
           
            Close #1                'Closes file
           
            Open "u:\sample.txt" For Input As #1    'Re-opens file to get value
           
            Input #1, text              'Assigns value in file to text variable
           
            If text = "left" Then           'Checks if left is the word in the file
            text = MsgBox("Right")          'If left is the word then for testing purposes again, it prints out right just so it's different to the value in the file
            CallSubroutine "Form_Open"
           
            End If
           
            If text = "right" Then          'Checks if right is the word in the file
            text = MsgBox("Left")           'Again only for testing
           
            End If
           
            If text = "back" Then           'Checks if back is the word in the file
            text = MsgBox("straight")       'Again only for testing
           
            End If
           
            If text = "straight" Then       'Checks if straight is the word in the file
            text = MsgBox("back")           'Again only for testing
           
            End If
           
            If text = "stop" Then           'Checks if stop is the word in the file
            text = MsgBox("start")          'Again only for testing
           
            End If
                   
            Close #1
           
        Next                    'To go around to the next value to be entered
          
           
    End Sub                     'End of function
    
     
    


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    Have you added a reference to the Microsoft Comm Control ?
    Is there an instance of the control on your form?

    Error 424 means that you are trying to access an object that has not had an instance created.

    Can you post your entire project? If you step through it in debug where exactly does it stop working?

    I just created a new vb project and i don't get any 424 errors


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    What do you mean by adding a reference, or an instance of the control form?
    I'm completely new to VB sorry. It could be the way I have set things up? I just opened a standard project, ticked a box in components for MSComm aswel.


    That is the entire part of the VB code for the project, the rest is in assembly code, so this is all that needs to work.

    Where i get the error is, when you run it, type in left and hit enter.
    When you do that you get a box saying left, then one saying right, then one saying this is subA, then the error message, do you not get that?

    Thanks for the help.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Here is another post I made about it with replies, if this helps.
    http://www.vbforums.com/showthread.php?p=2788723#post2788723


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Tar, if you can wait till this evening, i have code that does something similar at home that might help you out...

    The first thing, is to make sure that on Form_Load you instiate your MSComm1 control

    Then check for errors

    so
    MSComm1.Settings = "9600,N,8,1" '
    MSComm1.CommPort = 4              
    MSComm1.PortOpen = True           
    
    If err.number <> 0 then
       MsgBox "Error: Code: " & Err.Number & " " & Err.Description
       Err.Clear
       Exit Sub
    End If
    
    Then do your continue stuff
    
    If you need to use the MSComm in a function you can use a function such as
    
    function UseMSComm(ByRef MSComm As Object,ByVal strCommand As String) As Boolean
    
    Might help
    
    
    


  • Registered Users Posts: 1,366 ✭✭✭king_of_inismac


    I think this is your problem:

    Go to Projects->Components->Tick the box for mscomm control

    look to the left of your screen, where all the controls are. There should now be a little picture of a telephone, Click on it.

    Go you your form and create a box. The box should now be a picture of a phone. this is your mscomm object. Right click on the picture of the phone and select properties. Make sure its called mscomm1, so it matches your code.

    The code should work now.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Thanks very much for the replies, I have tried the ticking the box(Project-->components) and it is actually denied to me in college because of access priviledges. :/

    I would love a look a that code ginger, thanks.

    Did you test that king_of_inismac?
    If the code above works that would be great, or was it the code I have up on vbforums you looked at?


    Thanks a lot guys.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    So close but yet so far!


    Here is the Code I have:
    Option Explicit 
    
    Public Sub Port_Open() '
        MsgBox "This is SubA"
        MSComm4.Output = "j"
    End Sub
    
    Private Sub Form_Activate()
    
       
        MSComm4.Settings = "9600,N,8,1"
        MSComm4.CommPort = 4                 
        MSComm4.PortOpen = True
    
    End Sub
    
    Private Sub form_Unload(Cancel As Integer)
    MSComm4.PortOpen = False
    End Sub
    
    Private Sub Command1_Click()
    Dim Direction As String
    Dim variable1 As String
    Dim text As String
    Dim Counter As Integer ' You forgot to declare this
          
        For Counter = 1 To 10 'the step is 1 by default
            'Open "u:\sample.txt" For Output As #1   'This opens the file for writing
            Direction = InputBox("Enter Direction") 'Dialogue box to enter direction
            'Print #1, Direction         'Prints the inputted word to file
            'Close #1                'Closes file
            
            'Open "u:\sample.txt" For Input As #1    'Re-opens file to get value
            'no need for the file - you're only using it to hold the value of Direction
          
            'Input #1, variable1
            variable1 = Direction
            MsgBox variable1       'Tells us what the input was, will not be in final code only for testing purposes
            'since you're not using any return value use the sub, not the function
          
            'Close #1                'Closes file
          
            'Open "u:\sample.txt" For Input As #1    'Re-opens file to get value
          
            'Input #1, text              'Assigns value in file to text variable
            text = Direction
          
            If text = "left" Then           'Checks if left is the word in the file
              MsgBox "Right"
              Port_Open
            End If
          
            If text = "right" Then          'Checks if right is the word in the file
            MsgBox "Left"            'Again only for testing
            Port_Open
            End If
          
            If text = "back" Then           'Checks if back is the word in the file
            MsgBox "straight"       'Again only for testing
            Port_Open
            End If
          
            If text = "straight" Then       'Checks if straight is the word in the file
            MsgBox "back"           'Again only for testing
            Port_Open
            End If
          
            If text = "stop" Then           'Checks if stop is the word in the file
            MsgBox "start"           'Again only for testing
            Port_Open
            End If
                  
            'Close #1
    
        Next Counter            'To go around to the next value to be entered
    
    End Sub                     'End of function
    

    When I run it it says port is already open, when I enter left and hit enter.
    When I comment out 'MSComm4.PortOpen = True' it says it is closed, can anybody solve this catch 22?

    I have fixed the telephone thing.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Tar here you go mate
    Public Function SendSMS(strRecipient, strSMS) As Boolean
    
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,N,8,1"
       
    MSComm1.PortOpen = True
        
    If Err Then
           SendSMS = False
           Exit Function
    End If
        
    Dim strCommandSet As String
      
    strCommandSet = "AT+CMGF=1;" + vbCr
        
    If (RunATCommand(MSComm1, strCommandSet, "OK")) Then
        strCommandSet = "AT+CSCA=" & Chr(34) & "+353857000000" & Chr(34) & ";" & vbCr
        If (RunATCommand(MSComm1, strCommandSet, "OK")) Then
            strCommandSet = "AT+CMGS=" & Chr(34) & strRecipient & Chr(34) & ";" & vbCr
            If (RunATCommand(MSComm1, strCommandSet, ">")) Then
                strCommandSet = strSMS & Chr(26)
                If (RunATCommand(MSComm1, strCommandSet, "OK")) Then
                    SendSMS = True
                Else
                    SendSMS = False
                End If
            Else
                SendSMS = False
            End If
        Else
            SendSMS = False
        End If
    Else
        SendSMS = False
    End If
    
    MSComm1.PortOpen = False
    
    End Function
    
    Function RunATCommand(objModem As MSComm, strCommand As String, strCompare As String) As Boolean
        
    Dim blExitLoop          As Boolean
    Dim varEvents           As Variant
    Dim strModemString      As String
    
    blExitLoop = False
        
    If Len(strCommand) <= 0 Or Len(strCompare) <= 0 Then
        RunATCommand = False
        Exit Function
    End If
        
    objModem.InBufferCount = 0
    objModem.Output = strCommand
        
    Do While Not (blExitLoop)
        'varEvents = DoEvents
        
        If objModem.InBufferCount Then
            strModemString = strModemString + objModem.Input
                     
            If InStr(1, strModemString, "ERROR", vbTextCompare) Then
                blExitLoop = True
                RunATCommand = False
            End If
            
            If InStr(1, strModemString, strCompare, vbTextCompare) Then
                blExitLoop = True
                RunATCommand = True
            End If
        
        End If
    
    Loop
        
    End Function
    

    This code works on a GSM Modem connected on a COM port 1


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Also i checked out a second version i did of it with an IR modem (ie my Nokia phone)

    And I used the following initialisation code
    Private Sub Form_Load()
    
    On Error Resume Next
    
        With MSComm1
                .CommPort = 1
                .RThreshold = 1
                .RTSEnable = True
                .Settings = "9600,n,8,1"
                .SThreshold = 1
                .PortOpen = True
       End With
    
    If Err Then
        MsgBox "Unable to open connection to your modem. Please make sure it is plugged in and switched on", vbCritical, "Desktop SMS"
        MsgBox "Desktop SMS will now close"
        Err.Clear
        Unload Me
        Exit Sub
    End If
    Exit Sub
    

    Apologies for the code as well, it was 2001 when i wrote it :P


  • Registered Users Posts: 1,456 ✭✭✭FSL


    Move the code to open the serial port out of the form_activate into form_Load or a separate sub and call it from Form_Load or where you know it will only be called the once.. You will probably find that your form is being activated after your message box has displayed.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    I got it working in the end, did the presentation, got drunk, woke up, and here I am.

    Thanks you. :)


Advertisement