Noob needing expert help Mscomm


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Noob needing expert help Mscomm

  1. #1
    Join Date
    Apr 2005
    Posts
    1

    Noob needing expert help Mscomm

    cuting a long sort short, i decied to learn how to program 2 weeks ago so i could create a little app to monitor sensor data. so go easy on me im realy green when i comes to programming.

    my problem.

    i have successfully created an app to monitor sensor data thats transmitted in hex data, but while monitoring and processing the info it maxes out my CPU load and eventualy locks the app up.

    the incoming data is a constant sream of hex info after the intitial request is sent. the format its sending in is

    0xff (start flag) 0x** (number of data bytes to follow) and no endflag
    eg
    ff 06 a1 bf 04 6c d3 88

    transmission settings on the port are 9600, 8,N,1


    mscomm's inputlen = 8
    rthreshold = 64
    * i have tryed just having the input len the same as the rtheshold
    snipit of my receiving routine called from the on_mscomm sub

    Sub streamhandler()
    Dim intMSB As Integer
    Dim intoutput As Byte
    Dim tempin As String
    Dim dumpstring As String
    Dim arydatain() As Byte
    intoutput = 2 ' start position of data in array
    ReDim arydatain(framelen + 1)
    For g = 1 To 4 ' 4 passes over the meters processing 4 and dumping 4
    tempin = MSComm1.Input
    For i = 1 To framelen
    arydatain(i - 1) = Asc(Mid(tempin, i, 1)) 'build array of input values in Dec
    Next i
    If arydatain(0) = 255 Then
    If MSComm1.InputLen <> framelen Then 'return the length to normal
    MSComm1.InputLen = framelen
    End If
    ' RPM meter data and calculation
    intMSB = (arydatain(intoutput) * 16)
    intoutput = intoutput + 1
    mtrRPM.HandValue = 12.5 * (intMSB + arydatain(intoutput))
    mtrRPM.RefreshDynamic
    intoutput = intoutput + 1
    'speed meter data and calculation
    mtrSpeed.HandValue = arydatain(intoutput) * 2
    mtrSpeed.RefreshDynamic
    intoutput = intoutput + 1
    ' Timing meter data and calculation
    mtrTiming.HandValue = 110 - arydatain(intoutput)
    mtrTiming.RefreshDynamic
    intoutput = intoutput + 1
    dumpstring = MSComm1.Input 'dump every second input,
    'Throtle possition sensor
    mtrTPS.HandValue = arydatain(intoutput) * 0.02
    mtrTPS.RefreshDynamic
    intoutput = intoutput + 1
    ' coolant temp
    mtrTemp.HandValue = arydatain(intoutput) - 50
    mtrTemp.RefreshDynamic
    intoutput = intoutput + 1
    ' fuel temp
    mtrFtemp.HandValue = arydatain(intoutput) * 0.02
    mtrFtemp.RefreshDynamic
    intoutput = intoutput + 1
    ' batt volts
    mtrBatt.HandValue = arydatain(intoutput) * 0.08
    mtrBatt.RefreshDynamic

    intoutput = 2 'reset output counter/deligator for next pass
    intMSB = 0 'clear msb of data for next pass
    ElseIf arydatain(0) <> 255 And MSComm1.InputLen = framelen Then 'lame atempt to resync
    tbDiag.Text = tbDiag.Text & "SYNC LOST HEADER WRONG" & vbNewLine
    MSComm1.InputLen = framelen + 1
    End If
    Next g

    End Sub

    Anyhelp on how to display this incoming data with a S%$T load lower CPU usage would awsome! thanks in advance

  2. #2
    Join Date
    Feb 2004
    Location
    Colton, CA
    Posts
    550
    You need to insert DoEvents() after the loops (or wherever you feel necessary) to process any outstanding process instructions.

    Code:
    Sub streamhandler()
    Dim intMSB As Integer
    Dim intoutput As Byte
    Dim tempin As String
    Dim dumpstring As String
    Dim arydatain() As Byte
    intoutput = 2 ' start position of data in array
    ReDim arydatain(framelen + 1)
    For g = 1 To 4 ' 4 passes over the meters processing 4 and dumping 4
    tempin = MSComm1.Input
    For i = 1 To framelen
    arydatain(i - 1) = Asc(Mid(tempin, i, 1)) 'build array of input values in Dec
    Next i
    DoEvents()
    If arydatain(0) = 255 Then
    If MSComm1.InputLen <> framelen Then 'return the length to normal 
    MSComm1.InputLen = framelen
    End If
    ' RPM meter data and calculation
    intMSB = (arydatain(intoutput) * 16)
    intoutput = intoutput + 1
    mtrRPM.HandValue = 12.5 * (intMSB + arydatain(intoutput))
    mtrRPM.RefreshDynamic
    intoutput = intoutput + 1
    'speed meter data and calculation
    mtrSpeed.HandValue = arydatain(intoutput) * 2
    mtrSpeed.RefreshDynamic
    intoutput = intoutput + 1
    ' Timing meter data and calculation
    mtrTiming.HandValue = 110 - arydatain(intoutput)
    mtrTiming.RefreshDynamic
    intoutput = intoutput + 1
    dumpstring = MSComm1.Input 'dump every second input, 
    'Throtle possition sensor
    mtrTPS.HandValue = arydatain(intoutput) * 0.02
    mtrTPS.RefreshDynamic
    intoutput = intoutput + 1
    ' coolant temp
    mtrTemp.HandValue = arydatain(intoutput) - 50
    mtrTemp.RefreshDynamic
    intoutput = intoutput + 1
    ' fuel temp
    mtrFtemp.HandValue = arydatain(intoutput) * 0.02
    mtrFtemp.RefreshDynamic
    intoutput = intoutput + 1
    ' batt volts
    mtrBatt.HandValue = arydatain(intoutput) * 0.08
    mtrBatt.RefreshDynamic
    
    intoutput = 2 'reset output counter/deligator for next pass
    intMSB = 0 'clear msb of data for next pass
    ElseIf arydatain(0) <> 255 And MSComm1.InputLen = framelen Then 'lame atempt to resync
    tbDiag.Text = tbDiag.Text & "SYNC LOST HEADER WRONG" & vbNewLine
    MSComm1.InputLen = framelen + 1
    End If
    Next g
    DoEvents()
    
    End Sub

  3. #3
    Join Date
    Feb 2004
    Location
    Colton, CA
    Posts
    550
    Perhaps also some sleep command in there to slow things down a little? To use these:

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    in General declarations

    then

    Code:
    sleep(1000) '1000 = 1 second

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center