How to display the weight of the balance in a text field via the RS-232 serial port or a USB converter?

I was assigned to display the weight of the scales (CAS CI-201A) in a text box using C #. Weight will be sent via RS-232 serial port or USB converter. The scale is with me, but I don’t know where to start. How can I achieve my goal?

+3
source share
7 answers

Have you tried anything else?

If you want to use a serial port, it makes sense to first give the user the opportunity to choose which port to use. This can be done easily by filling out the field 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;
    }

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

using System.IO.Ports;

to the directives used.

(button1) (textbox1) :

        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.

: , click1 - , "SerialPort" . .

:

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 + "...\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
                {
                    textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte());
                        //<-- bytewise adds inbuffer to textbox
                }
            }
        }
    }
}
+8

-, -, , . (HyperTerm, putty) , - .

, , .

( ), . , , (nullmodem aka crossed-over).

, SerialPort # http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

+5

:

COM1... 30 30 33 33 20 49 44 5F 30 30 3A 20 20 20 31 30 2E 36 20 6B 67 20 0D 0A 0D 0A

ASCII :

0033 ID_00: 10,6

, . , byte[] serialReceived:

string reading = System.Text.Encoding.UTF8.GetString(serialReceived);
textBox1.Text = reading.Substring(13);
+3

adam ( ASCII UTF8) i []

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
            {
                int dataLength = _serialPort.BytesToRead;
                byte[] data = new byte[dataLength];
                int nbrDataRead = _serialPort.Read(data, 0, dataLength);
                if (nbrDataRead == 0)
                    return;
                string str = System.Text.Encoding.UTF8.GetString(data);
                textBox1.Text = str.ToString();
            }
        }

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 + "...\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
            {
                int dataLength = _serialPort.BytesToRead;
                byte[] data = new byte[dataLength];
                int nbrDataRead = _serialPort.Read(data, 0, dataLength);
                if (nbrDataRead == 0)
                    return;
                string str = System.Text.Encoding.UTF8.GetString(data);
                textBox1.Text = str.ToString();
            }
        }
}

A & D EK V Calibration Model: AND EK-610V. BaudRate = 2400; DataBits = 7

: : enter image description here

BaudRate, DataBits (. )

+1

Anto sujesh, , , , . .

        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
        {
            int dataLength = _serialPort.BytesToRead;                

            byte[] data = new byte[dataLength];
            int nbrDataRead = _serialPort.Read(data, 0, dataLength);
            if (nbrDataRead == 0)
                return;
            string str = Encoding.UTF8.GetString(data);

            //Buffers values in a file
            File.AppendAllText("buffer1", str);

            //Read from buffer and write into "strnew" String
            string strnew = File.ReadLines("buffer1").Last();

            //Shows actual true value coming from scale
            textBox5.Text = strnew;
+1
source
using System;

using System.IO;

using System.IO.Ports;

namespace comport

{
    public partial class Form1 : Form

    {
        public Form1()

        {

            InitializeComponent();
        }

        private SerialPort _serialPort = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            AppConfiguration.sConfigType = "default";

            _serialPort = new SerialPort("COM1", 9600, Parity.None, 8);

            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);

            _serialPort.Open();

        }

        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string data = _serialPort.ReadExisting();

            textBox2.Text = data;
        }
    }

}
0
source
    using System;
    using System.IO.Ports;         
    using System.Windows.Forms;
    namespace ComPortTests
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
                private SerialPort _serialPort = null;
    private void Form1_Load(object sender, EventArgs e)
    {
     _serialPort = new SerialPort("COM1", 9600, Parity.None, 8);

     _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);

     _serialPort.Open();
    }

    void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)

            {

                string data = _serialPort.ReadExisting();
                textBox2.Text = data;    
            }
    }
}
-1
source

All Articles