wpf 在 C# 中使用串行端口从称重桥机获取重量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12278366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Weight From Weigh Bridge Machine Using Serial Port in C#
提问by Dinesh
i am develop application for getting weight from weigh Bridge machine using C#.Net.i am trying lot of ways but,doesn't read correct data format weight from weigh bridge machine.i am getting ouput like ?x???????x?x?x??x???x??x???x???x?continuously get from serial port.i want to get weight from weigh bridge machine my code is listed below:
我正在开发使用 C#.Net 从称重桥机获取重量的应用程序。我尝试了很多方法,但是,没有从称重桥机读取正确的数据格式重量。我得到的输出就像?x???????x?x?x??x???x??x???x???x?从串行端口连续获取一样。我想要为了从地磅机获得重量,我的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace SerialPortTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String a = "";
private void button1_Click(object sender, EventArgs e)
{
serialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
if (serialPort1.IsOpen == false)
{
serialPort1.Open();
}
timer1.Start();
button1.Enabled = false;
button2.Enabled = true;
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
a = a + serialPort1.ReadExisting();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (a.Length != 0)
{
textBox1.AppendText(a);
a = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
serialPort1.Close();
button2.Enabled = false;
button1.Enabled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
button1.Enabled = false;
button2.Enabled = true;
}
else
{
button1.Enabled = true;
button2.Enabled = false;
}
}
}
}
my code is append text from serial port data to textbox but,it's shows only like ?xxx?xxxx?xxxx?
我的代码是将串行端口数据中的文本附加到文本框,但是,它仅显示为 ?xxx?xxxx?xxxx?
can any one help me how to get weight from serial port using c#
任何人都可以帮助我如何使用c#从串口获取重量
Thanks For Reading My Post!
感谢您阅读我的帖子!
回答by Hans Passant
You are using ReadExisting(), that method tries to convert the bytes received by the port into a string. You'll get a question mark if that conversion fails. The default Encoding is ASCII, a byte value between 128 and 255 is not an ASCII character and thus produces a ?
您正在使用 ReadExisting(),该方法尝试将端口接收到的字节转换为字符串。如果转换失败,您会得到一个问号。默认编码为 ASCII,128 到 255 之间的字节值不是 ASCII 字符,因此会产生 ?
Several possible reasons, roughly in order of likelihood:
几种可能的原因,大致按可能性排序:
- Using the wrong baud rate, in particular guessing too high.
- The device might be sending binary data, not strings. Which requires using Read() instead of ReadExisting and decoding the binary data.
- Electrical noise picked up by a long cable that isn't shielded well enough. Easy to eliminate as a possible reason by disconnecting the cable at the bridge end. If that stops the data then it isn't likely to be noise.
- 使用错误的波特率,特别是猜测太高。
- 设备可能正在发送二进制数据,而不是字符串。这需要使用 Read() 而不是 ReadExisting 并解码二进制数据。
- 屏蔽不够好的长电缆接收到的电噪声。通过在桥端断开电缆,很容易消除可能的原因。如果这停止了数据,那么它不太可能是噪音。
Be sure to thoroughly read the manual. Contact the vendor of the device if you don't have one or can't make sense of it.
请务必仔细阅读手册。如果您没有或无法理解该设备,请联系该设备的供应商。

