|
-
Feb 18th, 2011, 03:31 PM
#1
Thread Starter
Lively Member
[RESOLVED] serial port program hogging cpu
Hi,
i have the following code which runs ok, but it hogs upto 95% of the CPU. am i doing something really silly here?
program outline: a timer tick event = 1000ms scans through 30 readlines of data on the comport for a specific line of hex, and then extracts information from the next line and converts it to decimal for viewing on the screen.
i've only included the main part of the code to try and keep it simple. thanks in advance,
aaron.
Code:
Public Class Form1
Private WithEvents serial As New IO.Ports.SerialPort
Public grab As String
Public cardIndex As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
openPort()
Timer1.Enabled = True
End Sub
Public Sub openPort() ' Open Serial Port and initialize.
' Timer1.Enabled = False
If serial.IsOpen = False Then
serial.BaudRate = 9600
serial.PortName = "COM4"
serial.StopBits = 1
serial.Parity = IO.Ports.Parity.None
serial.DataBits = 8
serial.WriteTimeout = 2000
serial.ReadTimeout = 2000 'Set timeout to 2 seconds.
Try
serial.Open()
System.Threading.Thread.Sleep(50)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If serial.IsOpen Then
System.Threading.Thread.Sleep((11000 / serial.BaudRate) + 2) ' Min. 11 bit delay (startbit, 8 data bits, parity bit, stopbit)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
searchData()
End Sub
Private Sub searchData()
Timer1.Enabled = False
For runThrough As Integer = 0 To 30
Dim test As String
Dim i As Integer
Dim DataIn As String
DataIn = ""
If serial.IsOpen = True Then
If serial.BytesToRead > 0 Then
DataIn = serial.ReadLine
End If
End If
grab = ""
For i = 1 To DataIn.Length - 1 ' This removes the first null character from the read line.
test = DataIn.Chars(i)
grab = grab + test
Next
If grab = "+010100B3" Then cardIndex = 1 : GetCardValues() ' card 1 info found. go and sort out data.
Timer1.Enabled = True
Next
End Sub
Public Sub GetCardValues() ' Sort data out for current card.
Dim i As Integer
Dim test As String
Dim temp As String
Dim check As String
check = ""
Dim DataIn As String
DataIn = ""
temp = ""
If serial.BytesToRead > 0 Then
grab = serial.ReadLine
End If
For i = 1 To grab.Length - 1 ' This removes the first null character from the read line again.
test = grab.Chars(i)
temp = temp + test
Next
For a As Integer = 0 To 6
check = check + temp.Chars(a) ' look for a values identifier.
Next
If check = "+FD1803" Then
grab = temp
temp = ""
For a As Integer = 7 To 10
temp = temp + grab.Chars(a)
Next
temp = Convert.ToInt32(temp, 16).ToString
If temp <= 100 Then
If cardIndex = 1 Then Box1.Text = " " + temp + " LEL"
End If
temp = ""
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
closePort()
End Sub
Private Sub closePort()
Try
serial.Close()
System.Threading.Thread.Sleep(50)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
________________________________________
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|