Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSComm Data Collection Help Please 2

Status
Not open for further replies.

WildGun

Technical User
Jan 13, 2005
15
US
Hi

I am connected to a peice of equipment that sends string of hex data every 500ms. Each string is terminated by a carriage return and a line feed is also inserted, an example would be;

01 54 1F OK<CR>
<LF>
02 D0 1F OK<CR>
<LF>

I have the following code to attempt ot collect the strings;

Private Sub IT_Comm()
Dim MyString As String
Dim MyChar As Variant
Dim Done As Boolean

MSComm1.InputLen = 1 'Get one character at a time
MyString = "" ' Clears variable
MyChar = "" ' Clears variable
Done = False
Do

MyChar = MSComm1.Input 'Get a Character from the comport
If MyChar = Chr(13) Then 'If it is a terminiting character
Exit Do 'Jump out of loop
Else

MyString = MyString & MyChar 'otherwise concatenate it to the string
txtTerm1.Text = MyString 'Displays the collected string
txtTerm2.Text = MyChar 'Displays each character collected
End If
Loop Until Done 'Loop forever, I need to add a time out timer

End Sub

I am using the comEvReceive to call this sub maybe that is my problem? TxtTerm1 and TxtTerm2 are noting but a simple text box just so I can see what I am getting.

Here is the problem, I get 1 complete string and that is it, it is a great snap shot but I need it to clear the old string and constantly reload the most current string. I was also attempting to view each character collected using TxtTerm2 but I see a solid black bar and that is it, maybe it is some Ascii character I am not sure.

As you can see my code is rough but I am trying to learn so please help if you can, and thank you for trying.

Regards

Randy



 
Here's how I get data like this from the com port.

1: Set your COMM control to InputLen of 0 and RThreashold as 1. This causes the Comm input statement to get all of the data at once, as soon as it is received by the COMM port.

2: Make a new class module, name it clsCOMMDATA, and put this code into it.

Code:
Option Explicit

Event DataArrived(DataString As String)

Private m_COMMString As String
Private m_String As String

Property Let NewDataString(ByVal NwInput As String)
Dim I As Integer
Dim S As String
    
    ' add the characters onto the string until you reach the CR
    For I = 1 To Len(NwInput)
        ' get the next character
        S = Mid(NwInput, I, 1)
        
        Select Case S
        Case vbCr
            ' this is the CR character. Set the output string, erase the temp string, and raise the event.
            m_COMMString = m_String
            m_String = ""
            RaiseEvent DataArrived(m_COMMString)
        
        Case vbLf
            ' this is the Line Feed.  Ignore it
            
        Case Else
            ' this is any other character.  Add it onto the string.
            m_String = m_String & S
            
        End Select
    ' continue on if there are any remaining characters in the new input string
    Next I
    
End Property

Property Get COMMString() As String
    ' gets the last commstring that was transmitted
    
    COMMString = m_COMMString

End Property


In your main form, put this in your declarations
Code:
Dim WithEvents COMMData As clsCOMMData

and this is your code in your COMM control

Code:
Private Sub MsComm1_OnComm()
' this sub fires whenever new data has arrived at the Comm port
Dim S As String

S = MsComm1.Input

' send this new string to the COMMData class for assembly into the full string
' and possible activation of the DataArrived event.
COMMData.NewDataString = S

End Sub

And finally, your comdata event
Code:
Private Sub COMMData_DataArrived(DataString As String)

' this is where you handle the incoming string, have it display where you want, etc.

txtTerm1.Text = DataString

end sub

As your data is received, it is sent to the Commdata class for assembly into a full string. Once a full string is assembled, this raises an event back in the main program.

Now, this is a little more than a basic method might give you, but it shows you how to make a reuseable class that can be adapted to fit your needs.

I'll leave it up to you to figure out how to get the individual characters to display in your program.

Hope it helps,

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top