如何在 c# 中从权重秤在网格视图上显示权重



我正在通过串口通信开发体重秤程序但我有这样做的...例:

使用将项目放在比例上,当我 应用程序获取权重然后停止与秤通信,首先设置重量 gridviewrowcolumn然后行自动更改,当用户放置时 第二项规模通信开始和相同的程序做。.

这是我的代码:

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.Threading;

namespace serial
{
    public partial class Form1 : Form
    {
        private SerialPort _serialPort;         //<-- declares a SerialPort Variable to be used throughout the form
        private const int BaudRate = 9600;   
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            #region Form Load
            try
            {
                //getRawWeight();
                string[] portNames = SerialPort.GetPortNames();     //<-- Reads all available comPorts
                foreach (var portName in portNames)
                {
                    comboBox1.Items.Add(portName);                  //<-- Adds Ports to combobox
                }
                comboBox1.SelectedIndex = 0;      
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (_serialPort != null && _serialPort.IsOpen)
                _serialPort.Close();
            if (_serialPort != null)
                _serialPort.Dispose();
            //<-- End of Block
            _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One);       //<-- Creates new SerialPort using the name selected in the combobox
            _serialPort.DataReceived += SerialPortOnDataReceived;       //<-- this event happens everytime when new data is received by the ComPort
            _serialPort.Open();     //<-- make the comport listen
            textBox1.Text = "Listening on " + _serialPort.PortName + "...rn";
        }
        private delegate void Closure();
        private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
        {
            if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
                BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     //<-- Function invokes itself
            else
            {
                while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
                {
                    string reading = "";
                    reading += string.Format("{0:X2} ", _serialPort.ReadByte());
                    //<-- bytewise adds inbuffer to textbox
                    //string reading = System.Text.Encoding.UTF8.GetString(_serialPort);
                    textBox1.Text = reading.Substring(13);
                }
            }
        }
    }
}

我会打开串行端口一次,然后将其保持打开状态,直到您关闭程序。 您成功与秤沟通了吗? 此外,秤等一些设备有自己的按钮,每次按下串口时都会通过串行端口发送数据。 你的代码有什么问题? 它写的是什么?

最新更新