Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

serial port - VB.Net Serialport datarecieved not firing VS2019

I'm writing an Arduino data logging/processing program for a test rig at work and coding in VB.net as i have a few years experience in that language and figured it would be quicker than relearning C#. The issue i am currently having is sometimes during debugging the Serialport.Datarecieved event wont fire and wont fire until i delete the event handler code block and recreate it. When doing this i copy the contents of the sub, delete the block and recreate it then paste the code back into the block and away i got again. I have tried instead to call the handler via addhandler at the start of the program to set it programmatically on form1.load but that hasn't helped.

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports

Public Class Form1
Public WithEvents com1 As New IO.Ports.SerialPort

'serial port recreated and connection details set.
com1 = New IO.Ports.SerialPort(cb_Comms.Text, cb_Baud.Text, Parity.None, 8, StopBits.One)
com1.Handshake = Handshake.None
com1.WriteTimeout = 10
com1.ReadTimeout = 10




Private Sub com1_DataReceived() (sender As Object, e As SerialDataReceivedEventArgs)
     Me.BeginInvoke(Sub() PortRead()) 
End Sub

Any help would be appreciated. Thanks

question from:https://stackoverflow.com/questions/65835453/vb-net-serialport-datarecieved-not-firing-vs2019

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Messages recieved from arduino finish in vbcrlf   

Private Function PortRead() As String

        Dim msg As String = ""
        If Not IsNothing(com1) AndAlso com1.IsOpen Then

            tick = True


            'string format Capacity tester *C5;V3.22;C102.30;E
            'string format 150A tester *!5;V3.22;C102.30;E
            Dim MsgIn As String = ""
            Try

                Try
                    MsgIn = com1.ReadTo(vbcrlf)
                Catch ex As Exception
                    'Stop
                End Try

                If MsgIn.Length >= 0 Then
                    MsgIn = MsgIn.Replace(vbCrLf, "").Trim()
                    If cb_Date.Checked Then rtb_Serial.Text += DateTime.Now.ToString()
                    rtb_Serial.Text = MsgIn + vbCrLf + rtb_Serial.Text

                    If MsgIn.StartsWith("*C") Or MsgIn.StartsWith("*!") Then
                        Try

                            Process_Data(MsgIn)
                        Catch ex As Exception
                            MessageBox.Show("Message Processing Fail" + vbCrLf + MsgIn, "A15")
                           
                        End Try

                    End If
                    Return MsgIn
                Else
                    Return ""
                End If

            Catch e As Exception
                If Not e.Message = "The operation has timed out." Then
                    MessageBox.Show("Error Reading Port: " + com1.PortName + vbCrLf + e.Message + vbCrLf + "Port " + com1.PortName + " Closed", "A13")
                    com1.Close()
                End If

            End Try
        End If

        rtb_Serial.Update()
        Return msg
    End Function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...