一段时间以来,我一直致力于学习如何编写VB.NET。在这个页面和互联网的帮助下,我设法创建了一个程序,它将一些十六进制值发送到接口盒并从中获得例行响应。
这里的问题是通信是TCP / IP。但是现在我有一个带USB接口的新盒子,现在我对如何使用串口发送和接收相同的十六进制值没有任何想法。
以下是代码。我不知道是否有人可以帮我设置一个名为COM13的串口,或者至少向我解释一下该做什么。
这就是该计划的目的。
基本上就是这样。问题是,到目前为止,我只能发送文本字符串,但是框不能识别它,它会响应垃圾,而不是日期。
以下是代码:
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Public Class Form1
Dim client As New TcpClient
Dim transmit As NetworkStream
Dim protocol As Byte()
Dim data As Byte()
Dim ByteArrayToHexStr As String = String.Empty
Dim message As String
Dim datos_byte As Byte()
Dim respuesta As [String] = [String].Empty
Dim transision As [String] = [String].Empty
Dim Date_full As [String] = [String].Empty
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Clear()
client = New TcpClient("10.2.12.65", 10001)
transmit = client.GetStream()
protocol = {225, 51} ' E1 33 in hex value
transmit.Write(protocol, 0, protocol.Length)
Thread.Sleep(45)
data = {5, 108, 41, 241, 60, 129} ' 05 6c 29 f1 3c 81 in hex value
transmit.Write(data, 0, data.Length)
data = New Byte(66) {}
Thread.Sleep(45)
Dim bytes As Int32 = transmit.Read(data, 0, data.Length)
Dim Date1 As String = String.Empty
Dim Date2 As String = String.Empty
Dim Date_temp As Integer = 0
For i As Integer = 0 To UBound(data)
Date1 = data(i)
If Date1 = "129" Then
Date_temp = i + 1
i = 1000
For k As Integer = 0 To 5
If k = 0 Then
Date1 = data(Date_temp + k)
Date_full = Chr(Date1)
Else
Date1 = data(Date_temp + k)
Date2 = Chr(Date1)
Date_full = Date_full & Date2
End If
Next
Else
End If
Next
Thread.Sleep(45)
transmit.Close()
client.Close()
TextBox1.Text = TextBox1.Text & Date_full
End Sub
End Class
答案 0 :(得分:0)
请查看Microsoft的SerialPort类示例:https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
如果垃圾回收可能表示波特率配置不正确。我不确定你的意思是' box'但是此设备应具有指定的波特率,您应该可以在其数据表或产品信息中找到它。