Good Morning all! Sorry its been a few days.
For several reasons I needed to read a serial port yesterday. Here is the code below. Make it a great day!
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
‘This event will Receive the data from the selected COM port..
If e.EventType = SerialData.Chars Then
thRec = New Thread(AddressOf ReceiveData)
thRec.IsBackground = True
thRec.Priority = ThreadPriority.Highest
Private Sub ReceiveData()
‘Sub to Receive Data from the Serial Port, Will Run in a Thread
Dim lstItem As ListViewItem
Dim bRead, nRead As Integer
Dim returnStr As String = ""
Dim ascStr As String = ""
bRead = SerialPort1.BytesToRead ‘Number of Bytes to read
Dim cData(bRead – 1) As Byte
SerialPort1.Encoding = Encoding.GetEncoding(65001)
nRead = SerialPort1.Read(cData, 0, bRead) ‘Reading the Data
For Each b As Byte In cData
ascStr += Chr(b) ‘Ascii String
returnStr += Hex(b).PadLeft(2, "0") ‘Hex String (Modified Padding, to intake compulsory 2 chars, mainly in case of 0)