<legend id='SQFyx'><style id='SQFyx'><dir id='SQFyx'><q id='SQFyx'></q></dir></style></legend>
<tfoot id='SQFyx'></tfoot>

      <bdo id='SQFyx'></bdo><ul id='SQFyx'></ul>

    <small id='SQFyx'></small><noframes id='SQFyx'>

    1. <i id='SQFyx'><tr id='SQFyx'><dt id='SQFyx'><q id='SQFyx'><span id='SQFyx'><b id='SQFyx'><form id='SQFyx'><ins id='SQFyx'></ins><ul id='SQFyx'></ul><sub id='SQFyx'></sub></form><legend id='SQFyx'></legend><bdo id='SQFyx'><pre id='SQFyx'><center id='SQFyx'></center></pre></bdo></b><th id='SQFyx'></th></span></q></dt></tr></i><div id='SQFyx'><tfoot id='SQFyx'></tfoot><dl id='SQFyx'><fieldset id='SQFyx'></fieldset></dl></div>

      如何通过串口 RS-232 或 USB 转换器将体重秤的重量显示到文本框中?

      How to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?(如何通过串口 RS-232 或 USB 转换器将体重秤的重量显示到文本框中?)
    2. <legend id='HuhR5'><style id='HuhR5'><dir id='HuhR5'><q id='HuhR5'></q></dir></style></legend>
        • <i id='HuhR5'><tr id='HuhR5'><dt id='HuhR5'><q id='HuhR5'><span id='HuhR5'><b id='HuhR5'><form id='HuhR5'><ins id='HuhR5'></ins><ul id='HuhR5'></ul><sub id='HuhR5'></sub></form><legend id='HuhR5'></legend><bdo id='HuhR5'><pre id='HuhR5'><center id='HuhR5'></center></pre></bdo></b><th id='HuhR5'></th></span></q></dt></tr></i><div id='HuhR5'><tfoot id='HuhR5'></tfoot><dl id='HuhR5'><fieldset id='HuhR5'></fieldset></dl></div>

            <bdo id='HuhR5'></bdo><ul id='HuhR5'></ul>
            <tfoot id='HuhR5'></tfoot>

              <tbody id='HuhR5'></tbody>

            <small id='HuhR5'></small><noframes id='HuhR5'>

                本文介绍了如何通过串口 RS-232 或 USB 转换器将体重秤的重量显示到文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我被分配使用 C# 将体重从称重秤 (CAS CI-201A) 显示到文本框中.重量将通过串口 RS-232 或 USB 转换器发送.秤在我身边,但我不知道从哪里开始.我怎样才能实现我的目标?

                I've been assigned to display weight from weighing scale (CAS CI-201A) into a textbox using C#. The weight will be sent via serial port RS-232 or USB converter. The scale is with me but I don't know where to start. How can I achieve my goal?

                推荐答案

                你试过了吗?

                如果您想使用串行端口,首先让用户选择使用哪个端口是有意义的.这可以通过使用所有可用端口填充组合框来轻松完成.

                If you want to use the serial port it makes sense to first give the user a way to select which port to use. This can be done easily, by filling a combobox with all available ports.

                        private void Form1_Load(object sender, EventArgs e)
                    {
                        string[] portNames = SerialPort.GetPortNames();
                        foreach (var portName in portNames)
                        {
                            comboBox1.Items.Add(portName);
                        }
                        comboBox1.SelectedIndex = 0;
                    }
                

                此代码使用一个带有组合框的表单,称为comboBox1"(默认).您需要添加:

                This code uses a form with a comboBox on it, called "comboBox1" (Default). You will need to add:

                using System.IO.Ports;
                

                到 using 指令.

                to the using directives.

                然后在表单中添加一个按钮(button1)和一个多行文本框(textbox1)并添加以下代码:

                Then add a button (button1) and a multiline textbox (textbox1) to the form and add this code:

                        private void button1_Click(object sender, EventArgs e)
                    {
                        _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One);
                        _serialPort.DataReceived += SerialPortOnDataReceived;
                        _serialPort.Open();
                        textBox1.Text = "Listening on " + comboBox1.Text + "...";
                    }
                
                    private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
                    {
                        while(_serialPort.BytesToRead >0)
                        {
                            textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte());
                        }
                    }
                

                这也需要你添加:

                    private SerialPort _serialPort;
                    private const int BaudRate = 9600;
                

                就在

                public partial class Form1 : Form
                

                点击按钮后,从选定的comPort接收到的所有数据都将在TextBox中显示为十六进制值.

                After clicking the button, all received data from the selected comPort will be displayed as hex values in the TextBox.

                免责声明:上面的代码不包含错误处理,如果多次单击 button1 会产生错误,因为之前的SerialPort"实例没有正确关闭.使用此示例时请记住这一点.

                DISCLAIMER: The above code contains NO error-handling and will produce errors if button1 is clicked multiple times, due to the fact that the previous instance of "SerialPort" is not closed properly. Please remember this when using this example.

                问候尼科

                完整代码:

                using System;
                using System.IO.Ports;          //<-- necessary to use "SerialPort"
                using System.Windows.Forms;
                
                namespace ComPortTests
                {
                    public partial class Form1 : Form
                    {
                        private SerialPort _serialPort;         //<-- declares a SerialPort Variable to be used throughout the form
                        private const int BaudRate = 9600;      //<-- BaudRate Constant. 9600 seems to be the scale-units default value
                        public Form1()
                        {
                            InitializeComponent();
                        }
                
                        private void Form1_Load(object sender, EventArgs e)
                        {
                            string[] portNames = SerialPort.GetPortNames();     //<-- Reads all available comPorts
                            foreach (var portName in portNames)
                            {
                                comboBox1.Items.Add(portName);                  //<-- Adds Ports to combobox
                            }
                            comboBox1.SelectedIndex = 0;                        //<-- Selects first entry (convenience purposes)
                        }
                
                        private void button1_Click(object sender, EventArgs e)
                        {
                            //<-- This block ensures that no exceptions happen
                            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 + "...
                ";
                        }
                
                        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
                                {
                                    textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte());
                                        //<-- bytewise adds inbuffer to textbox
                                }
                            }
                        }
                    }
                }
                

                这篇关于如何通过串口 RS-232 或 USB 转换器将体重秤的重量显示到文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  <bdo id='xrFIy'></bdo><ul id='xrFIy'></ul>
                  <tfoot id='xrFIy'></tfoot>
                • <small id='xrFIy'></small><noframes id='xrFIy'>

                    • <i id='xrFIy'><tr id='xrFIy'><dt id='xrFIy'><q id='xrFIy'><span id='xrFIy'><b id='xrFIy'><form id='xrFIy'><ins id='xrFIy'></ins><ul id='xrFIy'></ul><sub id='xrFIy'></sub></form><legend id='xrFIy'></legend><bdo id='xrFIy'><pre id='xrFIy'><center id='xrFIy'></center></pre></bdo></b><th id='xrFIy'></th></span></q></dt></tr></i><div id='xrFIy'><tfoot id='xrFIy'></tfoot><dl id='xrFIy'><fieldset id='xrFIy'></fieldset></dl></div>
                    • <legend id='xrFIy'><style id='xrFIy'><dir id='xrFIy'><q id='xrFIy'></q></dir></style></legend>
                          <tbody id='xrFIy'></tbody>