c# - 使用 C# 从 Mettler Toledo (IND560) 秤设备读取数据

标签 c# scale

我在使用 C# 从 Mettler Toledo (IND560) 秤设备接收数据时遇到了一些问题。

当我向设备发送“去皮重”命令 (T) 时,它工作正常但没有任何响应。 BytesToRead 始终为空,“while”处于无限循环中。

当我发送“发送稳定的重量值”命令 (S) 时,我面临同样的无限循环问题。我猜命令运行正常但没有响应。

代码如下:

private decimal? BalancaIND560(string porta, string comando) {
    SerialPort SerialObj = new SerialPort(porta);
    if (!SerialObj.IsOpen)
        SerialObj.Open();

    string retorno = "";
    try {
        SerialObj.BaudRate = 9600;
        SerialObj.Parity = Parity.Even;
        SerialObj.DataBits = 7;
        SerialObj.StopBits = StopBits.One;
        SerialObj.Handshake = Handshake.XOnXOff;

        SerialObj.DiscardInBuffer();
        SerialObj.DiscardOutBuffer();

        SerialObj.Write(comando);

        while ((SerialObj.BytesToRead == 0))
            Application.DoEvents();

        Thread.Sleep(500);
        retorno = SerialObj.ReadExisting();

        SerialObj.DiscardInBuffer();
        SerialObj.DiscardOutBuffer();
    } finally {
        try { SerialObj.Close(); } catch { }
    }

    decimal? resultado = null;
    try {
        string[] aux = retorno.Split(' '); //"S S     100.52 kg"

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < aux.Length; i++)
            sb.Append(String.Format("aux[{0}]: {1}" + Environment.NewLine, i, aux[i]));
        MessageBox.Show(sb.ToString());

        decimal peso = 0.0M;
        if (!Decimal.TryParse(aux[6].Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out peso))
            Decimal.TryParse(aux[7].Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out peso);
        resultado = peso;
    } catch { }

    return resultado;
}

// Sending command
try {
    decimal? peso = BalancaIND560("COM1", "S");
    if (peso.HasValue)
        MessageBox.Show(String.Format("Peso: {0}", peso.Value));
    else
        MessageBox.Show("Peso não foi encontrado", "ATENÇÃO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
} catch {
    MessageBox.Show("Erro ao executar comando", "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

最佳答案

我找到了解决方案!我只需要更改秤配置,它就可以工作了!!!如果有人遇到同样的问题,只需将设备中的 COM 配置(Configuration > Comunication > Conections)更改为 SICS,我的代码就可以正常工作!!谢谢大家!

关于c# - 使用 C# 从 Mettler Toledo (IND560) 秤设备读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35483240/

相关文章:

c# - 从 base64 解码后的嘈杂音频剪辑

c# - NHibernate - 使用 CreateMultiQuery

c# - 如何在 Visual Studio 2008 中永久禁用区域折叠

ios - 在 ImageView 中缩放图像 (Scale x,y) - IOS

image - 如何更改图像中的像素值范围?

c# - 处理用户控件,真的意味着编辑 .designer.cs 文件吗?

时间:2019-05-17 标签:c#MVCDropdownlistpost选择值

Android:ImageView 放大源图像

java - 计算 ImageView 平移后的新点坐标

c# - 互锁与互斥,放大问题