C# 使用C中的串行端口从称重机获取重量#
我正在开发一个应用程序,使用C#Net从地磅机上获取重量。我尝试了很多方法,但是,未从称重机读取正确的数据格式重量。我从串行端口连续获取像C# 使用C中的串行端口从称重机获取重量#,c#,.net,wpf,serial-port,C#,.net,Wpf,Serial Port,我正在开发一个应用程序,使用C#Net从地磅机上获取重量。我尝试了很多方法,但是,未从称重机读取正确的数据格式重量。我从串行端口连续获取像?x?????x?x?x???x???x???x?一样的输出。我想从称重机获取重量。我的代码如下所示: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syste
?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;
}
}
}
}
我的代码是将串行端口数据中的文本附加到文本框中,但它只显示为?xxx?xxxx?xxxx?
有谁能帮我用c从串口获取重量吗#
谢谢你阅读我的帖子 如果使用ReadExisting(),该方法会尝试将端口接收的字节转换为字符串。如果转换失败,您将得到一个问号。默认编码是ASCII,128和255之间的字节值不是ASCII字符,因此产生
几个可能的原因,大致按可能性顺序排列:
- 使用错误的波特率,尤其是猜测过高
- 设备可能正在发送二进制数据,而不是字符串。这需要使用Read()而不是ReadExisting并解码二进制数据
- 由于长电缆屏蔽不够好而产生的电气噪音。通过断开桥端的电缆,很容易消除可能的原因。如果这停止了数据,那么不太可能是噪声
务必彻底阅读本手册。如果您没有或无法理解该设备,请联系该设备的供应商。此代码将在后台连续读取weightbridge。确保使用串行端口连接电脑。同样,在设计页面Form1.cs[design]中,您需要从工具箱中添加串行端口。这个代码适合我,我希望它也适合你
public partial class Form1 : Form
{
//Initialize the port and background Worker
private SerialPort port;
private BackgroundWorker backgroundWorker_Indicator;
public Form1()
{
backgroundWorker_Indicator = new BackgroundWorker();
backgroundWorker_Indicator.WorkerSupportsCancellation = true;
backgroundWorker_Indicator.DoWork += new DoWorkEventHandler(Indicator_DoWork);
//set the port according to your requirement.
port = new SerialPort("COMM2", 2400, Parity.None, 8, StopBits.One);
port.DataReceived += new SerialDataReceivedEventHandler(this.Indicator_DataReceived);
}
//button which starts the method. You can also put the method in Form1_Load()
private void SerialPortButton(object sender, EventArgs e)
{
StartStopIndicator();
}
private void StartStopIndicator()
{
try
{
port.Open();
backgroundWorker_Indicator.RunWorkerAsync();
}catch (Exception ea)
{
MessageBox.Show("13 "+ea.Message);
}
}
// Not a button. Just a methood.
private void Indicator_DoWork(object sender, DoWorkEventArgs e)
{
}
private void Indicator_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string str = StripNonNumeric(port.ReadLine());
UpdateWeightOnUI(str);
}
catch (Exception eb)
{
MessageBox.Show("12"+eb.Message);
}
}
private void UpdateWeightOnUI(string Weight)
{
try
{
// A label named weightLabel from the toolbox. This will keep updating on weight change automatically
if (weightLabel.InvokeRequired)
{
this.Invoke((Delegate)new Form1.SetTextCallBack(this.UpdateWeightOnUI), (object)Weight);
}
else
{
weightLabel.Text = Weight;
}
}
catch (Exception ec)
{
MessageBox.Show("11"+ec.Message);
}
}
// This method will remove all other things except the integers
private string StripNonNumeric(string original)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (char c in original)
{
if (char.IsDigit(c))
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
private delegate void SetTextCallBack(string text);
你首先需要知道它是怎么写的。显然不是ASCII。@HenkHolterman你说的是将串口数据转换成ASCII值吗?我是说有很多方法可以让网桥对信息进行编码。你需要规格和文件。@Henkholtman你能发布任何链接来解码信息吗?不,我不知道你的设备是什么。