Как прочитать более 1 байта из последовательного порта
Dim outData As Byte() = New Byte(0) {} Private Function HextoByte(ByVal msg As String) As Byte() msg = msg.Replace(" ", "") Dim combuffer As Byte() = New Byte(msg.Length \ 2 - 1) {} For i As Integer = 0 To msg.Length - 1 Step 2 combuffer(i \ 2) = CByte(Convert.ToByte(msg.Substring(i, 2), 16)) Next Return combuffer End Function Private Function BytetoHex(ByVal comByte As Byte()) As String Dim builder As New StringBuilder(comByte.Length * 3) For Each data As Byte In comByte builder.Append((Convert.ToString(data, 16).PadLeft(2, "0"))) Next Return builder.ToString().ToUpper() End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblDataValue.Text = ScrollData.Value.ToString() ScrollData.Enabled = False ScrollData.Maximum = 255 ScrollData.LargeChange = 1 btnDisconnect.Enabled = False tbRx.Text = "00" Dim Portnames as String() = SerialPOrt.GetPortNames IF Portnames is nothing then msgbox("No ports detected") me.close() End If cbComPort.Items.AddRange(Portnames) cbComPort.Text = Portnames(0) End Sub Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click outData(0) = Convert.ToByte(lblDataValue.Text) Try SerialPort1.PortName = cboCOMPorts.Items(cboCOMPorts.SelectedIndex).ToString() SerialPort1.BaudRate = 9600 SerialPort1.Open() SerialPort1.Write(outData, 0, 1) btnDisconnect.Enabled = True ScrollData.Enabled = True btnConnect.Enabled = False Catch ex As Exception btnDisconnect.PerformClick() End Try End Sub Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click Try SerialPort1.DiscardInBuffer() SerialPort1.DiscardOutBuffer() SerialPort1.Close() ScrollData.Value = 0 ScrollData.Enabled = False btnConnect.Enabled = True btnDisconnect.Enabled = False Catch ex As Exception End Try End Sub Private Sub ScrollData_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles ScrollData.Scroll lblDataValue.Text = ScrollData.Value.ToString("X") outData(0) = Convert.ToByte(ScrollData.Value) SerialPort1.Write(outData, 0, 1) End Sub Private Delegate Sub DisplayDelegate(ByVal displayChar As String) Private Sub DisplayCharacter(ByVal displayChar As String) tbRx.Text = displayChar End Sub Private Sub serialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim rx As Integer rx = SerialPort1.BytesToRead Dim comBuff As Byte() = New Byte(rx - 1) {} SerialPort1.Read(comBuff, 0, rx) tbRx.Invoke(New DisplayDelegate(AddressOf DisplayCharacter), New Object() {BytetoHex(comBuff)}) End Sub
код взят с этого сайта
[^]
Что я уже пробовал:
http://www.codeproject.com/Tips/341405/Read-n-bytes-from-the-serial-port-in-NET
В приведенной выше ссылке объясняется, как читать n байт, что я и думал сделать, так как ожидаю 2 байта в соответствии с руководством по командам от производителя:
https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&uact=8&ved=0ahUKEwj8xNS2-Z3PAhVDD8AKHQotAIIQFgg0MAI&url=http%3A%2F%2Fwww.optris.co.uk%2Finterfaces%3Ffile%3Dtl_files%2Fdownloads%2FManuals%2Faddendums-de-en%2Fct-ctlaser-commands.pdf&usg=AFQjCNEkEtwUoiM6jsJYOnGD4-8Yo2Cprg&bvm=bv.133387755,bs.2,d.d2s
Member 12292743
но ничего не меняется в tbRx, в котором я хочу видеть временное значение
Member 12292743
если я прав насчет чтения более 1 байта, как мне реализовать это в своем коде