使用C#梅特勒 - 托利多(IND560)规模的设备读取数据 [英] Reading data from Mettler Toledo (IND560) scale device using C#

查看:598
本文介绍了使用C#梅特勒 - 托利多(IND560)规模的设备读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#梅特勒 - 托利多(IND560)规模的设备接收数据时面临的一些问题。



当我送去皮命令(T)的装置,它的正常工作,但没有任何回应。该BytesToRead总是空和,而是无限循环。



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



下面是代码:

 私人小数? BalancaIND560(字符串PORTA,串COMANDO){
的SerialPort SerialObj =新的SerialPort(PORTA);
如果(SerialObj.IsOpen!)
SerialObj.Open();

串RETORNO =;
尝试{
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);

,而((SerialObj.BytesToRead == 0))
Application.DoEvents();

的Thread.Sleep(500);
RETORNO = SerialObj.ReadExisting();

SerialObj.DiscardInBuffer();
SerialObj.DiscardOutBuffer();
} {最后
尝试{SerialObj.Close(); }赶上{}
}

十进制? resultado = NULL;
尝试{
的String [] =辅助retorno.Split(''); //šš100.52公斤

StringBuilder的SB =新的StringBuilder();
的for(int i = 0; I< aux.Length;我++)
sb.Append(的String.Format(AUX [{0}]:{1}+ Environment.NewLine,我,AUX [I]));
MessageBox.Show(sb.ToString());

十进制比索= 0.0M;
如果(!Decimal.TryParse(AUX [6] .Trim(),NumberStyles.AllowDecimalPoint,CultureInfo.InvariantCulture,出比索))
Decimal.TryParse(AUX [7] .Trim()的NumberStyles .AllowDecimalPoint,CultureInfo.InvariantCulture,出比索);
resultado =比索;
}赶上{}

返回resultado;
}

//发送命令
尝试{
十进制?比索= BalancaIND560(COM1,S);
如果(peso.HasValue)
MessageBox.Show(的String.Format(比索:{0},peso.Value));
,否则
MessageBox.Show(比索NAO FOI encontrado,ATENÇÃO,MessageBoxButtons.OK,MessageBoxIcon.Warning);
} {赶上
MessageBox.Show(埃罗AO executar COMANDO,ERRO,MessageBoxButtons.OK,MessageBoxIcon.Error);
}


解决方案

我找到了解决办法!我不得不改变一个规模配置和它的工作!如果有人遇到同样的问题,只是改变 COM 配置(结构>交际> Conections )以 SICS 在设备中,我的代码进行的非常棒! TKS所有!


I'm facing some problems when receiving data from Mettler Toledo (IND560) scale device using C#.

When I send "taring" command (T) to device, it's working properly but there is nothing in response. The BytesToRead is always empty and the "while" is in infinite loop.

When I send "send stable weight value" command (S), I face the same infinite loop issue. I guess the command is running properly but not responding.

Here is the code:

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);
}

解决方案

I found the solution! I just had to change a scale configuration and it's WORKING!!! If someone face the same problem, just change COM configuration (Configuration > Comunication > Conections) to SICS in the device and my code is working wonderfully!! Tks All!

这篇关于使用C#梅特勒 - 托利多(IND560)规模的设备读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆