0

我正在通过串行端口通信开发体重秤程序,但我已经这样做了......示例:

当我的应用程序获得重量时使用将项目放在秤上,然后停止与秤进行通信,首先设置重量, 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 + "...\r\n";
        }
        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);
                }
            }
        }
    }
}
4

1 回答 1

0

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

于 2013-11-15T17:17:21.187 回答