我正在尝试在 VB.net 中编写一个 SAG105 METTLER TOLEDO,该 SAG105 METTLER TOLEDO 使用 IBM PCXT 的电缆 LC-RS9 RS232 9 针连接到 PC。我在互联网上进行了研究,但找不到可以帮助我使用 VB.net 将数据发送到称重设备的模板代码。我查看了串行端口的代码,但由于这是 IBM PCXT 的 RS-232,因此这种串行端口方法不起作用。我已经联系了该公司,因为我没有任何关于规模的文件,不幸的是他们说他们没有任何设备的模板代码。
有人可以通过引导我到正确的网站或分享您的一些知识来帮助我开始打开通信渠道并发送基本命令。我已经在这上面花了好几天了,但没有去任何地方。
提前谢谢了
我设法找到了一个不错的 VB 代码,但是我遇到了一些问题。所以我将使用的命令是“Z”——将刻度设置为零,“SIR”连续读取不稳定值,“S”获得稳定值,“SI”——读取一次不稳定值。
但是,我遇到的问题是发送命令的例程运行一次,但接收命令运行两次。当它第一次运行时,我得到正确的值,但第二次得到 ES(意味着语法错误,天平无法识别命令)。我在看到这个时放了一些休息。
我的代码是:
Dim myWeight As String, myWeightValue As Single
Delegate Sub SerialDataIn()
Public myDelegate As SerialDataIn
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
CloseSerialPort() ' Close the port
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This opens the serial Port
OpenSerialPort()
'Assign the delegate for the Serial Communications handling
myDelegate = New SerialDataIn(AddressOf SerialDataInProc)
End Sub
'HANDLE SERIAL PORT COMMUNICATIONS IN TO THE PC FROM THE BALANCE:
Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Get the weight value string from the balance
myWeight = SerialPort1.ReadLine
'Call a new Thread Procedure
Me.Invoke(myDelegate)
End Sub
'The delegate...
Sub SerialDataInProc()
'Display the received string in the text box
Me.TextBox1.Text = myWeight
'If any string is received that is not prefixed with an 'S' then it is not a weight value, so don't try to process the value
If myWeight.Substring(0, 1) <> "S" Then GoTo HopIt
'Convert the string received from the balance into a value
myWeightValue = Convert.ToSingle(myWeight.Substring(4, 10))
'If the weight on the balance exceeds 50g then reset the balance (stop data transmission) and display the message "Stopped"
If myWeightValue > 50 Then
SerialPortOut("@")
MsgBox("Stopped")
End If
HopIt:
End Sub
'Write any command to the balance port
Sub SerialPortOut(ByVal myText)
Try
SerialPort1.WriteLine(myText & vbCrLf)
Catch ex As Exception
MsgBox("Transmission to Serial Port Failed. Error: " & Err.Description, MsgBoxStyle.Critical, "Comms Failed")
End Try
End Sub
'Send the Balance the SIR Command (Send current weight value and repeat) if the [SEND SIR] button has been pressed
'Send the @ Command to the balance (Cancels all previous commands, so resets the balance and stops data transmission) if the [Send @] button is clicked
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click, ButtonStart.Click
Select Case sender.name
Case Is = "ButtonReset"
SerialPortOut("@")
Case Is = "ButtonStart"
SerialPortOut("S")
End Select
End Sub
'Set the serial Port Parameters and open the Port
Private Sub OpenSerialPort()
With SerialPort1
.PortName = "Com1"
.BaudRate = 9600
.DataBits = 8
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.XOnXOff
Try
.Open()
Catch ex As Exception
MsgBox("Could not open the Serial Port! Error: " & Err.Description, MsgBoxStyle.Critical, "Port Error")
End Try
End With
End Sub
'Close the Serial Port
Private Sub CloseSerialPort()
SerialPort1.Close()
End Sub
租约有人可以看看这个并告诉为什么会发生上述问题