使用Cable LC-RS9 RS232 9针与SAG105梅特勒 - 托利多在VB.net中与IBM PCXT进行通讯

时间:2015-10-16 10:03:51

标签: vb.net command serial-communication weighting

我试图在VB.net中编写一个SAG105梅特勒 - 托利多,它使用适用于IBM PCXT的Cable LC-RS9 RS232 9引脚连接到PC。我在互联网上进行了研究,但无法找到可以帮助我使用VB.net将数据发送到加权秤设备的模板代码。我看了一下串口代码,但由于这是IBM PCXT的RS-232,这种串口方法并不起作用。我联系了公司,因为我没有任何规模文档,不幸的是他们说他们没有设备的任何模板代码。

有人可以帮助我开始打开沟通渠道,并通过引导我访问正确的网站或分享您的一些知识来发送基本命令。我已经花了几天时间在这上面并且没有在那里用它。

非常感谢提前

我设法找到适合VB的代码,但是我遇到了一些问题。所以我将使用的命令是" Z" - 将比例设置为零," SIR"不断读取不稳定的值," S"获得稳定的价值," SI" - 一次读取不稳定值。

然而,我遇到的问题是发送命令的例程运行一次,但是receive命令运行两次。当它第一次运行时我得到正确的值,但第二次得到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

租约可以有人看看这个,并告诉为什么上述问题正在发生

1 个答案:

答案 0 :(得分:0)

SAG105的协议似乎是MT-SICS Standard Interface Command Set

使用我们的Docklight software(即使是免费评估版),您也可以轻松地在现代&#34; PC带有标准的USB转RS232插头。

协议似乎包含非常简单的文本命令,因此将以下文本命令发送到SAG105应返回当前余额值:

I2<CR><LF>

&#34; CR&#34; =回车,ASCII十进制代码13.和&#34; LF&#34; =换行,ASCII十进制代码10.

我无法找到SAGE105的标准COM参数(例如9600波特,8个数据位,1个停止位,无奇偶校验),但是您可能已经拥有此信息,无论如何都可以将其设置为应用程序 - 旧应用程序的特定设置。如果你知道RS232设置,我实际上可以给你一个小例子项目,让Docklight手动与SAG105对话。

在.NET中,一旦确认了您正在使用的设置和协议命令是正确的,那么基于文本的简单协议应该很容易实现。