• 大小: 0.12M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-06-18
  • 语言: C#
  • 标签: C#  串口通信  读二维码  

资源简介

1、C# 串口通信 读二维码 基恩士N-R2控制器 SR-710读码器 功能Demo;

2、依据实际选择COM端口号, 依实际修改读码指令和停止读码指令;

3、读码指令和停止读码指令可在 基恩士的软件AutoID Network Navigator中设置和测试。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SerialPortDemo
{
    public partial class FrmSerialPortMain : Form
    {
        public FrmSerialPortMain()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        System.IO.Ports.SerialPort  myCOM;
        bool comHadOpened = false;
        string LasterCode = ““;

        private void btnOpenPort_Click(object sender EventArgs e)
        {
            if (comHadOpened == false)
            {
                try
                {
                    myCOM = new System.IO.Ports.SerialPort();
                    myCOM.PortName = “COM3“;
                    myCOM.BaudRate = 115200;  // 波特率
                    myCOM.DataBits = 8;   // 数据位
                    myCOM.Parity = System.IO.Ports.Parity.None;  // 校验位
                    myCOM.StopBits = System.IO.Ports.StopBits.One;  //停止位
                    myCOM.NewLine = “\r\n“;
                    myCOM.RtsEnable = true; //启用请求发送信号
                    myCOM.DtrEnable = true; //启用控制终端就续信号
                    myCOM.ReadTimeout = 3000;
                    myCOM.ReceivedBytesThreshold = 1;
                    myCOM.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.myCom_DataReceived);

                    myCOM.Open();
                    comHadOpened = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnTriggerOn_Click(object sender EventArgs e)
        {
            TriggerOn();
        }
      
        private void TriggerOn()
        {
            if (comHadOpened == false)
                return;
            try
            {
                myCOM.WriteLine(this.txtONCommand.Text);
            }
            catch (Exception ex)
            {
               listBox1.Items.Add(“[“ + DateTime.Now.ToString(“MM-dd HH:mm:ss“) + “]  “ + ex.Message);
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
            }
        }

        private void myCom_DataReceived(object sender System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(100);
            byte[] ReDatas = new byte[myCOM.BytesToRead];
            myCOM.Read(ReDatas 0 ReDatas.Length);//读取数据
            string recdata = new ASCIIEncoding().GetString(ReDatas);
            if (LasterCode!=recdata)
            {
                listBox1.Items.Add(“[“ + DateTime.Now.ToString(“MM-dd HH:mm:ss“) + “]  “ + recdata);
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
                LasterCode = recdata;
            }
        }

        private void FrmSerialPortMain_F

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3645  2021-07-07 17:01  SerialPortDemo.csproj

     文件     201216  2021-07-09 08:52  bin\Debug\SerialPortDemo.exe

     文件      40448  2021-07-09 08:52  bin\Debug\SerialPortDemo.pdb

     文件        208  2021-07-05 15:43  obj\Debug\.NETframeworkVersion=v4.0.AssemblyAttributes.cs

     文件       2477  2021-07-22 11:11  obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7069  2021-07-07 17:01  obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件         11  2021-07-09 08:52  obj\Debug\SerialPortDemo.csproj.AssemblyReference.cache

     文件         42  2021-07-07 17:13  obj\Debug\SerialPortDemo.csproj.CoreCompileInputs.cache

     文件       2104  2021-07-07 17:13  obj\Debug\SerialPortDemo.csproj.FileListAbsolute.txt

     文件       1082  2021-07-08 09:42  obj\Debug\SerialPortDemo.csproj.GenerateResource.cache

     文件     201216  2021-07-09 08:52  obj\Debug\SerialPortDemo.exe

     文件      93420  2021-07-06 12:56  obj\Debug\SerialPortDemo.Form1.resources

     文件      93420  2021-07-08 09:42  obj\Debug\SerialPortDemo.FrmSerialPortMain.resources

     文件      40448  2021-07-09 08:52  obj\Debug\SerialPortDemo.pdb

     文件        180  2021-07-07 17:13  obj\Debug\SerialPortDemo.Properties.Resources.resources

     文件       1320  2021-07-05 15:43  Properties\AssemblyInfo.cs

     文件       2860  2021-07-05 15:43  Properties\Resources.Designer.cs

     文件       5612  2021-07-05 15:43  Properties\Resources.resx

     文件       1099  2021-07-05 15:43  Properties\Settings.Designer.cs

     文件        249  2021-07-05 15:43  Properties\Settings.settings

     文件       5025  2021-07-09 08:52  FrmSerialPortMain.cs

     文件      12754  2021-07-08 09:42  FrmSerialPortMain.Designer.cs

     文件     145595  2021-07-08 09:42  FrmSerialPortMain.resx

     文件      92854  2021-07-06 12:52  o11.ico

     文件        507  2021-07-07 17:01  Program.cs

     目录          0  2021-06-29 17:15  obj\Debug\TempPE

     目录          0  2021-06-29 17:23  bin\Debug

     目录          0  2021-07-07 17:01  bin\Release

     目录          0  2021-07-22 11:11  obj\Debug

     目录          0  2021-07-07 17:01  bin

............此处省略5个文件信息

评论

共有 条评论