首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用C#串口实现称重仪的称重

利用C#串口实现称重仪的称重
EN

Stack Overflow用户
提问于 2012-09-05 17:30:35
回答 2查看 5.8K关注 0票数 2

我正在使用C#.Net.i开发从称重机获取体重的应用程序我尝试了很多方法,但是,没有从称重机读取正确的数据格式体重。我像?x???????x?x?x??x???x??x???x???x?一样连续从串口获得输出。我想从称重机获取体重我的代码如下所示:

代码语言:javascript
复制
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#从串口获取重量

感谢您阅读我的帖子!

EN

回答 2

Stack Overflow用户

发布于 2012-09-05 20:09:03

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

几种可能的原因,大致按可能性排序:

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

请务必彻底阅读手册。如果您没有该设备或无法理解它,请联系该设备的供应商。

票数 2
EN

Stack Overflow用户

发布于 2021-01-07 14:33:22

这段代码将在后台连续读取weightbridge。请务必使用串口连接pc。另外,在设计页面Form1.csDesign中,您需要从工具箱添加串行端口。这段代码适用于我,我希望它也适用于你…

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12278366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档