处理从 rs232 端口获取的缓冲区数据

发布于 2024-11-26 18:33:32 字数 1560 浏览 0 评论 0原文

我有一个项目,需要使用 rs232 从秤读取数据。秤的输出格式为“L00000”。我的问题是秤在称重时连续发送数据,如“L00000,L00001,L00002,L00003,L00004”。我对这种情况的兴趣是获取要存储在数据库中的最后一部分“L00004”。 这是我的代码:请帮助我改进它。谢谢

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;

namespace WindowsFormsApplication5
 {
public partial class Form1 : Form
{
    private List<byte> PortBuffer = new List<byte>();
    SerialPort comPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {


        if (comPort.IsOpen)
        {
            comPort.DiscardOutBuffer();
            MessageBox.Show("Data cleared");
            comPort.Close();
        }

        if(!comPort.IsOpen)
        {
        comPort.Open();
        //sample of data as written by the scale. How can I get the last chunk only so that I format it for my needs
        comPort.Write("L00002 L00002 L00002");

        int bytes = comPort.BytesToRead;
        byte[] buffer = new byte[bytes];
        comPort.Read(buffer, 0, buffer.Length);

        comPort.DataReceived += new SerialDataReceivedEventHandler(data_received);

        comPort.Close();
       }
       }
    public void data_received(object sender,SerialDataReceivedEventArgs e)
    {
        MessageBox.Show("test" + comPort.ReadExisting().ToString());

    }

   }
 }

I have a project in which I am expected to read data from a scale using rs232. The output from the scale is in the format "L00000".My problem is that the scale sends data continuously as it weighs like "L00000,L00001,L00002,L00003,L00004". My interest in this case is to obtain the last part "L00004" to be stored in the db.
Here is my code: Kindly help me improve it. Thanks

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;

namespace WindowsFormsApplication5
 {
public partial class Form1 : Form
{
    private List<byte> PortBuffer = new List<byte>();
    SerialPort comPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {


        if (comPort.IsOpen)
        {
            comPort.DiscardOutBuffer();
            MessageBox.Show("Data cleared");
            comPort.Close();
        }

        if(!comPort.IsOpen)
        {
        comPort.Open();
        //sample of data as written by the scale. How can I get the last chunk only so that I format it for my needs
        comPort.Write("L00002 L00002 L00002");

        int bytes = comPort.BytesToRead;
        byte[] buffer = new byte[bytes];
        comPort.Read(buffer, 0, buffer.Length);

        comPort.DataReceived += new SerialDataReceivedEventHandler(data_received);

        comPort.Close();
       }
       }
    public void data_received(object sender,SerialDataReceivedEventArgs e)
    {
        MessageBox.Show("test" + comPort.ReadExisting().ToString());

    }

   }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2024-12-03 18:33:32

数据如何分隔,是空格 a 还是不分隔?

如果它是 a ,就像问题中那样

var data=comPort.ReadExisting().ToString();
var result=data.Split(',').Last();

How is the data seperated, is the a space a , or no seperation?

If its a , like in the question then

var data=comPort.ReadExisting().ToString();
var result=data.Split(',').Last();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文