2

我正在开发使用 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;
            }
        }
    }
}

我的代码是将串行端口数据中的文本附加到文本框,但它只显示?xxx?xxxx?xxxx?

任何人都可以帮助我如何使用 c# 从串口获取重量

感谢您阅读我的帖子!

4

2 回答 2

2

您正在使用 ReadExisting(),该方法尝试将端口接收到的字节转换为字符串。如果转换失败,您将得到一个问号。默认编码是 ASCII,128 到 255 之间的字节值不是 ASCII 字符,因此会产生 ?

几个可能的原因,大致按可能性顺序排列:

  • 使用错误的波特率,尤其是猜测太高。
  • 设备可能正在发送二进制数据,而不是字符串。这需要使用 Read() 而不是 ReadExisting 并解码二进制数据。
  • 屏蔽不够好的长电缆拾取的电气噪声。通过断开桥端的电缆很容易消除可能的原因。如果这停止了数据,那么它不太可能是噪音。

请务必仔细阅读手册。如果您没有设备或无法理解,请联系设备供应商。

于 2012-09-05T12:09:02.950 回答
0

此代码将在后台连续读取 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);
于 2021-01-07T06:33:22.173 回答