Click here to Skip to main content
15,311,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai to all,

Whenever i click on Button3 i get some serialport error.. ie., thread something have been appeared... How to rectify it .... and also itz not write when i click on button ........ How to overcome this error........

Imports System.IO.Ports
Imports System.Windows.Forms.Form
Imports System
Public Class Main
    Dim WithEvents serialport As New SerialPort
    Dim Rxd As String
    Dim Rxd1 As String
    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each sp As String In My.Computer.Ports.SerialPortNames
            ComboBox1.Items.Add(sp)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            With serialport
                .PortName = ComboBox1.Text
                .BaudRate = "9600"
                .Parity = Parity.None
                .StopBits = StopBits.One
                .DataBits = 8
                .Handshake = Handshake.None
                .ReadBufferSize = 500
                .Open()
                AddHandler serialport.DataReceived, AddressOf serialport_DataReceived
                '.ReadBufferSize = 500
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Height = 743
        Me.Width = 1029
        Label8.Height = 30
        Label8.Width = 85
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        serialport.Write("$M2#")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        serialport.Write("$M1#")
    End Sub

    Private Sub serialport_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived
        Dim sp As SerialPort = CType(sender, SerialPort)

        Dim menu_count As Integer
        Dim res As Integer
        Rxd = sp.ReadExisting
        menu_count = InStr(1, Rxd, "$M2", vbTextCompare)
        If menu_count > (500 - 189) Then
            sp.Write("$M1#")
        ElseIf menu_count Then
            menu_count = menu_count + 3
            For res = 0 To 5
                Rxd1 = Mid(Rxd, menu_count, 3)
                menu_count = menu_count + 3
                If res = 0 Then
                    TextBox1.Text = Rxd1
                ElseIf res = 1 Then
                    TextBox2.Text = Rxd1
                ElseIf res = 2 Then
                    TextBox3.Text = Rxd1
                ElseIf res = 3 Then
                    TextBox4.Text = Rxd1
                ElseIf res = 4 Then
                    TextBox5.Text = Rxd1
                ElseIf res = 5 Then
                    TextBox6.Text = Rxd1
                End If
            Next
        End If
    End Sub
End Class
Posted
Comments
Rajesh Anuhya 26-Dec-11 2:56am
   
Your Question is not clear to me.., can you explain more about error which you are getting..
Himachandra 26-Dec-11 3:02am
   
Whenever i click on Button3 ........ An Error has been occured ie., An IOException was unhandled
"The I/O operation has been aborted because of either a thread exit or an application request."

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900