regarding serial port

a project that gets data from a mircocontroller thru serial port

Page 1 of 1

0 Replies - 2274 Views - Last Post: 04 January 2010 - 07:55 PM Rate Topic: -----

#1 SkyChild  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-January 10

regarding serial port

Post icon  Posted 04 January 2010 - 07:55 PM

hi, i am a student currently working on a project that gets data from a serial port on the status of 16 sensors on a panel but when i try to debug my program it keeps hang(this data will be constantly sending and cannot be stopped unless i off the sensors).a designer view of the form will also be attached). your help will be much appreciated, thank you so much. the code is shown below.
Public Class Form1

   

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		

		If ComboBox1.SelectedItem = "Com1" Then SerialPort1.PortName = "COM1"
		If ComboBox1.SelectedItem = "Com2" Then SerialPort1.PortName = "COM2"
		If ComboBox1.SelectedItem = "Com3" Then SerialPort1.PortName = "COM3"
		If ComboBox1.SelectedItem = "Com4" Then SerialPort1.PortName = "COM4"
		If ComboBox1.SelectedItem = "Com5" Then SerialPort1.PortName = "COM5"
		If ComboBox1.SelectedItem = "Com6" Then SerialPort1.PortName = "COM6"
		If ComboBox1.SelectedItem = "Com7" Then SerialPort1.PortName = "COM7"
		If ComboBox1.SelectedItem = "Com8" Then SerialPort1.PortName = "COM8"
		If ComboBox1.SelectedItem = "Com9" Then SerialPort1.PortName = "COM9"
		If ComboBox1.SelectedItem = "Com10" Then SerialPort1.PortName = "COM10"

		
		
		'MsgBox("you have selected:" & SerialPort1.PortName)



	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim selitemindex As Integer
		Dim Portstatus As Boolean
		Portstatus = False
		Dim bquit As Boolean
		Dim instring As String

		'comparing the port so that the user does not select a port that is not working
		For Each pn As String In IO.Ports.SerialPort.GetPortNames
			If pn.ToString = SerialPort1.PortName Then
				Portstatus = True
			Else

			End If
		Next

		selitemindex = ComboBox1.SelectedIndex
		If selitemindex = -1 Then
			MsgBox("Please select a port and try again")
			bquit = True

		ElseIf Portstatus = False Then
			MsgBox("The port is not available, Please check your connection")
			bquit = True
		'lock the combo box once user has selected a valid port and start the comparing process
		ElseIf selitemindex <> -1 And ComboBox1.Enabled = False Then  'And SerialPort1.IsOpen = True Then
			ComboBox1.Enabled = True
			Button1.Text = "Start"

			SerialPort1.Close()
			bquit = True
		Else
			ComboBox1.Enabled = False
			Button1.Text = "Stop"

			SerialPort1.Open()
			
			bquit = False

			

			Do While bquit = False And SerialPort1.ReadBufferSize <> 0 'as long as i have data get it and compare
				'as data send from the other side will be in form of "A01\r\n" therefore i decided to use the 
				'readline function as it will be on a new line
				'initially circles should be draw to show the status of sensor but label is use to troubleshoot the serial
				' as it is faster this way for beginner like me 
				
				instring = SerialPort1.ReadLine
				If instring = "A01" Then sensor1.Text = instring '"111"	   'sensor on
				If instring = "01A" Then sensor1.Text = instring '"000"	   'sensor off
				If instring = "A02" Then sensor2.Text = instring '"111"
				If instring = "02A" Then sensor2.Text = instring '"000"
				If instring = "A03" Then sensor3.Text = instring '"111"
				If instring = "03A" Then sensor3.Text = instring '"000"
				If instring = "A04" Then Sensor4.Text = instring '"111"
				If instring = "04A" Then Sensor4.Text = instring '"000"
				If instring = "A05" Then Sensor5.Text = instring '"111"
				If instring = "05A" Then Sensor5.Text = instring '"000"
				If instring = "A06" Then Sensor6.Text = instring '"111"
				If instring = "06A" Then Sensor6.Text = instring '"000"
				If instring = "A07" Then Sensor7.Text = instring '"111"
				If instring = "07A" Then Sensor7.Text = instring '"000"
				If instring = "A08" Then Sensor8.Text = instring '"111"
				If instring = "08A" Then Sensor8.Text = instring '"000"
				If instring = "A09" Then Sensor9.Text = instring '"111"
				If instring = "09A" Then Sensor9.Text = instring '"000"
				If instring = "AZ10" Then Sensor10.Text = instring '"111"
				If instring = "10ZA" Then Sensor10.Text = instring '"000"
				If instring = "AZ11" Then Sensor11.Text = instring '"111"
				If instring = "11ZA" Then Sensor11.Text = instring '"000"
				If instring = "AZ12" Then Sensor12.Text = instring '"111"
				If instring = "12ZA" Then Sensor12.Text = instring '"000"
				If instring = "AZ13" Then Sensor13.Text = instring '"111"
				If instring = "13ZA" Then Sensor13.Text = instring '"000"
				If instring = "AZ14" Then Sensor14.Text = instring '"111"
				If instring = "14ZA" Then Sensor14.Text = instring '"000"
				If instring = "AZ15" Then Sensor15.Text = instring '"111"
				If instring = "15ZA" Then Sensor15.Text = instring '"000"
				If instring = "AZ16" Then Sensor16.Text = instring '"111"
				If instring = "16ZA" Then
					Sensor16.Text = instring '"000"
				Else : Sensor16.Text = "333"
				End If





			Loop





		End If
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Close()
	End Sub
End Class
 
Attached ImageAttached ImageAttached Image

Is This A Good Question/Topic? 0
  • +

Page 1 of 1