使用C#从Mettler Toledo(IND560)秤设备接收数据时,我遇到了一些问题。
当我向设备发送“taring”命令(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);
}
答案 0 :(得分:1)
我找到了解决方案!我只需改变一个比例配置,它正在工作!如果有人遇到同样的问题,只需将COM
配置(Configuration > Comunication > Conections
)更改为设备中的SICS
即可,我的代码工作正常! Tks All!