用C#从称重机到计算机的读取值 [英] Read value from weighing machine to computer in C#
本文介绍了用C#从称重机到计算机的读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读取从称重机到系统的值,现在我从称重中得到的值但不正确。最后2个数字不会显示在系统中。
编码:
Hi, I want to read values from weighing machine to system, Now i'm getting the value from Weighing but not correctly. Last 2 numbers are not display in the System.
Coding:
private void waitbridge2_Load(object sender, EventArgs e)
{
timer1.Start();
timer1.Interval = 1000;
}
private void timer1_Tick(object sender, EventArgs e)
{
SerialPort sp = new SerialPort();
sp.PortName = "COM1";
sp.BaudRate = 2400;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.RtsEnable = true;
sp.DtrEnable = true;
sp.Open();
txtweight.Text = sp.ReadLine().ToString();
Regex digits = new Regex(@"^\D*?((-?(\d+(\.\d+)?))|(-?\.\d+)).*");
Match mx = digits.Match(txtweight.Text);
decimal strValue1 = mx.Success ? Convert.ToDecimal(mx.Groups[1].Value) : 0;
txtweight.Text = strValue1.ToString();
sp.Close();
}
我的尝试:
从重量机器到计算机获取全部值
What I have tried:
Getting Full Value from weight machine to Computer
推荐答案
decimal strValue1 = mx.Success ? Convert.ToDecimal(mx.Groups[1].Value) : 0;
txtweight.Text = strValue1.ToString();
为什么要将字符串转换为Decimal,只是将其转换回字符串?
您应该使用调试器逐步执行代码并查看从COM端口返回的内容。
Why are you converting a string to Decimal, just to convert it back to a string?
You should use your debugger to step through the code and see what is returned from the COM port.
这篇关于用C#从称重机到计算机的读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文