Results 1 to 3 of 3

Thread: Analysis of Serial port data using a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    1

    Analysis of Serial port data using a text file

    Hi All,

    I need some real help. My programming experience is about 20 hour’s total. I am using visual basic express 2005. I want to create an application which opens a com port and references the received data to a text file or some other user editable file. I have created the application and the com port opens and I can see the data. I have then used some "if then" statements to check for certain bytes and display a message. This works; however for three days I have tried to understand how to reference a database or text file and out put the results: e.g. I receive T1380000A0005 on my serial line. I want to split the serial data as T138000 and 0005 "removing the 0A" then I want to reference each split in a files similar to the below:-

    #T138000#Button Press 1#
    #T138080#Button Release 1#

    #0005#Bus 0#Unit ID 5#
    #0004#Bus 0#Unit ID 4#

    Then in a text box I want to output the result as:-

    Button 1 Pressed on Unit 5 Bus 0

    I have racked my brains on this with no luck, and remember it’s all Chinese to me, so any real world examples would be much appreciated.

    Thanks in advance

    Mike

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Analysis of Serial port data using a text file

    Since VB 2005 is VB .net, you should be posting in Visual Basic .NET.

    Look at (I believe it's still in VB 2005) the Split function. (It migt be a method of class String in 2005.) Also Mid$, Left$ and Right$ (which might also be methods now).

    If you want to compare each input from the port to the entire user file, I'd suggest a small database (create an access table and use that from VB). Then you can "Select" just the entries with the text you want - T138000 or Button or whatever, without having to check every single entry.

  3. #3
    Junior Member
    Join Date
    Apr 2006
    Posts
    25

    Re: Analysis of Serial port data using a text file

    Look at using a StringTokenizer class. You can do split things according to some delimiter.
    You can copy this code:

    VB Code:
    1. 'StringTokenizer Class
    2. Private s As String, i As Integer
    3. Private sep As String   'token separator
    4. Private stokens() As String 'array of tokens
    5.  
    6.  
    7. Public Sub init(ByVal st As String, ByVal seper As String)
    8.     s = st
    9.     sep = seper
    10.     setSeparator (seper)
    11. End Sub
    12.  
    13. Public Sub setSeparator(ByVal sp As String)
    14.     sep = sp
    15.     stokens = Split(s, sp)
    16.     i = -1
    17. End Sub
    18.  
    19.  
    20. Public Function nextToken() As String
    21.     Dim tok As String
    22.     If i < UBound(stokens) Then
    23.         i = i + 1
    24.         tok = stokens(i)
    25.     Else
    26.         tok = ""
    27.     End If
    28.     nextToken = tok    'return token
    29. End Function

    ie. "T1380000A0005" using "0A" as the delimiter:
    Use this way:
    VB Code:
    1. dim st as StringTokenizer
    2. dim firstToken as String
    3. dim secondToken as String
    4.  
    5. call st.init("T1380000A0005", "0A")
    6. firstToken = st.nextToken                       'Becomes: T1380000
    7. secondToken = st.nextToken                   'Becomes: 0005

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