Combat of serial scanner communication based on C#

Big things are going on today, the audience is moving, the purpose of making things is

Master serial communication and winform development technology

Hardware equipment: 1. Serial port laser scanner, note that it is a serial port, not a USB port

 

 

                   2. A USB-to-serial connection cable, as shown in the figure

                  3. One USB extender, if you have more USB ports on your computer, you don’t need this one, as shown below

                  4. Cigarettes, betel nuts, mineral water, chicken legs and other physical goods are used for sweeping and likes

Hardware connection topology diagram:

 Software environment: VS2019, C#, WINFORM

1. Open VS2019, create a form project, and name it SerialPortScanner

 2. Add toolbar and text box control, the layout is as follows

 3. Check the serial port in the computer device manager, and add configuration items in the app.config file

 

 4. Add import

 5. Write event code

 

 

 

 The SerialPortHelper helper class is used in the code to initialize the serial port, receive events, set parameters, etc., code:

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerialPortScanner
{
    /// <summary>
    /// 串口帮助类
    /// </summary>
    public class SerialPortHelper
    {
        /// <summary>
        /// 定义委托
        /// </summary>
        /// <param name="info"></param>
        public delegate void ShowMesgDelegate(string info);
        /// <summary>
        /// 委托对象
        /// </summary>
        public ShowMesgDelegate myShowMsg;

        private SerialPort myCom = new SerialPort();//串口对象
        /// <summary>
        /// 串口属性
        /// </summary>
        public SerialPort MyCom
        {
            get
            {
                return myCom;
            }
            set
            {
                myCom = value;
            }
        }

        private byte myReceiveByte = 0;//接收字节
        private byte[] bData = new byte[1024];//接收的字节数组
        private int index = 0;

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="entiry"></param>
        public void OpenMyConn(SerialPortModel entiry)
        {
            if (myCom.IsOpen)
            {
                myCom.Close();//关闭
            }
            myCom.PortName = entiry.PortName;//串口名称
            myCom.BaudRate = entiry.BaudRate;//波特率
            myCom.DataBits = entiry.DataBits;//数据位
            myCom.StopBits = entiry.StopBits;//停止位
            myCom.Parity = entiry.Parity;//校验位 
            myCom.ReceivedBytesThreshold = 1;//设置串口缓冲区的字节数
            myCom.DataReceived += MyCom_DataReceived;//串口接收数据事件 
            myCom.Open();//打开串口
        }
        /// <summary>
        /// 串口接收事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //#region 写法1
            //index = 0;
            //while (MyCom.BytesToRead > 1)//循环读取缓冲区的数据
            //{
            //    myReceiveByte = (byte)MyCom.ReadByte();//从缓冲区读取一个字节数据
            //    bData[index] = myReceiveByte;//将读取到的一个字节数据存放在字节数组中 
            //    //string txt = Encoding.ASCII.GetString(bData, 0, index);//将字节转换成字符串
            //    myShowMsg(txt);
            //    index++;
            //    if (index >= 1024)
            //    {
            //        index = 0;
            //        MyCom.DiscardInBuffer();//丢弃缓冲区的数据
            //        return;
            //    }
            //}
            //if (index > 0)
            //{
            //    //数据解析 
            //    string txt = Encoding.ASCII.GetString(bData, 0, index);//将字节转换成字符串
            //    myShowMsg(txt);
            //}
            //#endregion

            #region 写法2 
            //读取串口缓冲区的字节数据
            byte[] result = new byte[MyCom.BytesToRead];
            MyCom.Read(result, 0, MyCom.BytesToRead);
            string str = Encoding.UTF8.GetString(result);
            myShowMsg(str);
            #endregion

        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void CloseMycom()
        {
            if (MyCom.IsOpen)
            {
                MyCom.Close();//关闭
            }
        }
    }
}

SerialPortModel entity class code:

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerialPortScanner
{
    /// <summary>
    /// 串口实体模型
    /// </summary>
    public class SerialPortModel
    {
        /// <summary>
        /// 端口号
        /// </summary>
        public String PortName { get; set; }
        /// <summary>
        /// 波特率
        /// </summary>
        public int BaudRate { get; set; }
        /// <summary>
        /// 数据位
        /// </summary>
        public int DataBits { get; set; }
        /// <summary>
        /// 停止位
        /// </summary>
        public StopBits StopBits { get; set; }
        /// <summary>
        /// 校验位
        /// </summary>
        public Parity Parity { get; set; }
    }
}

6. Run the program, first establish a connection

 

 Scan cigarettes, soldiers, mineral water, melon seeds, chicken legs, etc., all products with barcodes can be scanned, and the effect is good.

 

 

Guess you like

Origin blog.csdn.net/hqwest/article/details/130941757