Results 1 to 1 of 1

Thread: [RESOLVED] MSComm madness, need eyes

  1. #1

    Thread Starter
    Member sgarv's Avatar
    Join Date
    Jul 2012
    Posts
    34

    Resolved [RESOLVED] MSComm madness, need eyes

    Hi all,

    I just joined this forum and this is my first post. I consider myself experienced in VB6, but I have a problem which is driving me up the wall and I ask for Other Eyes to have a look and guide me in the right direction.

    My app communicates with an external device through a physical USB port and virtual COM3 port. I am using VB6. The external device just sits there until I send a "1*" command that requests data from it. The data is received and processed by my app.

    When the VB app runs its GUI contains a button. When the user clicks the button it connects to the COM3 port and then sends out the command to the device. If already connected then it just sends the command. My problem is that when the user first clicks the "connect" button the port opens ok and the command is sent, but I don't get anything back.

    The odd part is that if the user clicks the connect button a second time then the 1* command is sent and the data from the device is received correctly by the VB app. So my question is Why (oh why!) doesn't it send the command correctly the first time?

    I set the SThreshold property to 1 and observed the OnComm event. When I send the command the first time the CommEvent property is 1 (DataSend). Nothing else after that. When I click the button a second time the CommEvent property is 1, then it is 2 (various times while it receives the data).

    Below please find the code that I cam currently using for this component of the app. The example correctly duplicates the issue mentioned above.

    My form has one command button and the MSComm control. I also added a multiline textbox for debugging purposes.

    Thank you for your time and assistance. SGarV

    Updates:

    7/4/2012 14:50 -
    I got it working, sort of. I added a msgbox before the MSComm1.Output statement. This made the Output statement react correctly the first time sent. Without the MsgBox statement the code does not work (or works incorrectly as described above). I thought it might have to do with timing, so I added a Timer control, set Interval at 1000 and placed the MSComm Output statement there. When I click the "connect" button it behaves as described, but now it only enables the timer. When the timer fires 1 sec afterwards it sends out the 1* command. This change did not change its behavior. It will not work on the first click, but will work on the second click.

    Ok, tried something else. I moved the statements to configure and open the comm port into the form load event without sending anything. Now when I click the button all it does is send the 1* command to the device. This works perfectly! But I did not want the connection to open when the app loads since the user may want (or need to) change the comm port. I need the code to be able to open the port and send the 1* command under user control.

    http://msdn.microsoft.com/en-us/library/aa259394%28v=vs.60%29.aspx

    The above example clearly shows the code opening the port and immediately thereafter sending a command to the external device. I don't see why it is a problem in my code. As a further test I used a terminal program to manually send the 1* command to the device. Works perfectly the first time and every time.


    7/4/2012 19:10 -
    The issue is resolved. I added a delay of one second after I open the port and before I send out the first command to the external device. Thanks to all who took the time to read this post. Regards. The code that I added follows.

    Code:
        'Port is opened here
    
        dteFut = DateAdd("s", 1, Now)   'dteFut is dimmed as date
        
        Do
          DoEvents           'Just to give pending processes a chance.
        Loop Until Now > dteFut
        
       'Command is sent here.
    Original code follows:

    Code:
    Option Explicit
    
    Private strInComm As String
    
    
    Private Sub Command1_Click()
    
      
      'Open the comm port.
      If Not MSComm1.PortOpen Then
      
        MSComm1.CommPort = 3
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.RThreshold = 1 'musn't be zero for receiving
               
        MSComm1.PortOpen = True
      End If
    
      'Send command to peripheral to get data.
      'Set to 1 for debugging purposes 
      MSComm1.SThreshold = 0 'for sending command request.
      MSComm1.Output = "1*"
    
    End Sub
    
    Private Sub MSComm1_OnComm()
    
      Dim strAr() As String
      Dim i As Integer
      Dim strBuf As String
      
      'Debug 
      'Text1.Text = Text1.Text & MSComm1.CommEvent & vbCrLf
     
      'Get serial data from the device.
      If MSComm1.CommEvent = comEvReceive Then
        
        strBuf = MSComm1.Input
        
        'More debug
        'Text1.Text = Text1.Text & strBuf
        strInComm = strInComm & strBuf
        
        If InStr(strInComm, "*") > 0 Then
          'MsgBox "Rx"
          
          'The end of message char was received. I have
          'all the info.
          'MsgBox strInComm
          'Get the setting information in the array
          'Remove the * end char
          strInComm = Replace(strInComm, "*", "")
              
              
        
          MsgBox strInComm
          
          'Reinit receive buffer for next data that is sent over.
          strInComm = ""
        
        End If
      End If
    
    End Sub
    Last edited by sgarv; Jul 4th, 2012 at 07:11 PM. Reason: Removed superfluous code

Posting Permissions

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



Click Here to Expand Forum to Full Width