随着工业互联的发展,扫码枪在很多场合都有所应用,超市、商场以及一些智能工厂。今天主要讲如何通过C#实现与新大陆扫码枪(OY10)进行通信,对于扫码枪的配置,这里就不多说了,结合说明书就可以实现。这里值得注意的是,如果安装驱动后,电脑设备管理器中看不到COM口,可能需要扫一个条形码来设置一下,具体参考说明书通讯配置章节。

   首先贴下界面,基于Winform开发,主要就是正常的串口通信,涉及的技术包括UI界面设计+串口通信知识+参数配置处理+委托更新界面,涵盖了一个小系统必备的一些知识。

    

    再来贴一些源码,首先贴个核心串口类的编写:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO.Ports;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7
  8 namespace NewLand
  9 {
 10     public delegate void ShowMsgDelegate(string info);
 11
 12     public class NewLandSerial
 13     {
 14
 15         //定义串口类对象
 16         private SerialPort MyCom;
 17         //定义接收字节数组
 18         byte[] bData = new byte[1024];
 19         byte mReceiveByte;
 20         int mReceiveByteCount = 0;
 21         public ShowMsgDelegate myShowInfo;
 22
 23         public NewLandSerial()
 24         {
 25             MyCom = new SerialPort();
 26
 27         }
 28
 29         #region 打开关闭串口方法
 30         /// <summary>
 31         /// 打开串口方法【9600 N 8 1】
 32         /// </summary>
 33         /// <param name="iBaudRate">波特率</param>
 34         /// <param name="iPortNo">端口号</param>
 35         /// <param name="iDataBits">数据位</param>
 36         /// <param name="iParity">校验位</param>
 37         /// <param name="iStopBits">停止位</param>
 38         /// <returns></returns>
 39         public bool OpenMyComm(int iBaudRate, string iPortNo, int iDataBits, Parity iParity, StopBits iStopBits)
 40         {
 41             try
 42             {
 43                 //关闭已打开串口
 44                 if (MyCom.IsOpen)
 45                 {
 46                     MyCom.Close();
 47                 }
 48                 //设置串口属性
 49                 MyCom.BaudRate = iBaudRate;
 50                 MyCom.PortName = iPortNo;
 51                 MyCom.DataBits = iDataBits;
 52                 MyCom.Parity = iParity;
 53                 MyCom.StopBits = iStopBits;
 54                 MyCom.ReceivedBytesThreshold = 1;
 55                 MyCom.DataReceived += MyCom_DataReceived;
 56
 57                 MyCom.Open();
 58                 return true;
 59             }
 60             catch
 61             {
 62                 return false;
 63             }
 64         }
 65
 66         private void MyCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
 67         {
 68             mReceiveByteCount = 0;
 69             while (MyCom.BytesToRead > 0)
 70             {
 71                 mReceiveByte = (byte)MyCom.ReadByte();
 72                 bData[mReceiveByteCount] = mReceiveByte;
 73                 mReceiveByteCount += 1;
 74                 if (mReceiveByteCount >= 1024)
 75                 {
 76                     mReceiveByteCount = 0;
 77                     //清除输入缓存区
 78                     MyCom.DiscardInBuffer();
 79                     return;
 80                 }
 81             }
 82             if (mReceiveByteCount > 0)
 83             {
 84                 myShowInfo(Encoding.ASCII.GetString(GetByteArray(bData, 0, mReceiveByteCount)));
 85             }
 86
 87         }
 88
 89         /// <summary>
 90         /// 自定义截取字节数组
 91         /// </summary>
 92         /// <param name="byteArr"></param>
 93         /// <param name="start"></param>
 94         /// <param name="length"></param>
 95         /// <returns></returns>
 96         private byte[] GetByteArray(byte[] byteArr, int start, int length)
 97         {
 98             byte[] Res = new byte[length];
 99             if (byteArr != null && byteArr.Length >= length)
100             {
101                 for (int i = 0; i < length; i++)
102                 {
103                     Res[i] = byteArr[i + start];
104                 }
105
106             }
107             return Res;
108         }
109
110
111         /// <summary>
112         /// 关闭串口方法
113         /// </summary>
114         /// <returns></returns>
115         public bool ClosePort()
116         {
117             if (MyCom.IsOpen)
118             {
119                 MyCom.Close();
120                 return true;
121             }
122             else
123             {
124                 return false;
125             }
126
127         }
128         #endregion
129
130     }
131 }

NewLandSerial

    再者就是界面的调用,直接看代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Configuration;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.IO.Ports;
 8 using System.Linq;
 9 using System.Text;
10 using System.Threading.Tasks;
11 using System.Windows.Forms;
12
13 namespace NewLand
14 {
15     public partial class FrmMain : Form
16     {
17         public FrmMain()
18         {
19             InitializeComponent();
20             this.Load += FrmMain_Load;
21
22         }
23
24         private void FrmMain_Load(object sender, EventArgs e)
25         {
26             this.btn_DisConn.Enabled = false;
27         }
28
29         private void txt_Info_DoubleClick(object sender, EventArgs e)
30         {
31             this.txt_Info.Clear();
32         }
33
34         NewLandSerial myNewLand;
35
36
37         private void btn_Connect_Click(object sender, EventArgs e)
38         {
39            string Port= ConfigurationManager.AppSettings["Port"].ToString();
40
41             myNewLand = new NewLandSerial();
42             myNewLand.myShowInfo += ShowInfo;
43
44             if (myNewLand.OpenMyComm(9600, Port, 8, Parity.None, StopBits.One))
45             {
46                 MessageBox.Show("连接成功!", "建立连接");
47                 this.btn_Connect.Enabled = false;
48                 this.btn_DisConn.Enabled = true;
49             }
50             else
51             {
52                 MessageBox.Show("连接失败!", "建立连接");
53             }
54         }
55
56         private void ShowInfo(string info)
57         {
58             Invoke(new Action(() =>
59             {
60                 this.txt_Info.AppendText(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "   " + info + Environment.NewLine);
61             }));
62         }
63
64         private void btn_DisConn_Click(object sender, EventArgs e)
65         {
66             if (myNewLand.ClosePort())
67             {
68                 MessageBox.Show("断开连接成功!", "断开连接");
69                 this.btn_Connect.Enabled = true;
70                 this.btn_DisConn.Enabled = false;
71             }
72             else
73             {
74                 MessageBox.Show("断开连接失败!", "断开连接");
75             }
76         }
77
78         private void btn_ParaSet_Click(object sender, EventArgs e)
79         {
80             new FrmParaSet().ShowDialog();
81         }
82     }
83 }

FrmMain

    最后是参数配置,通过App.Config实现:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Configuration;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11
12 namespace NewLand
13 {
14     public partial class FrmParaSet : Form
15     {
16         public FrmParaSet()
17         {
18             InitializeComponent();
19             this.Load += FrmParaSet_Load;
20         }
21
22         private void FrmParaSet_Load(object sender, EventArgs e)
23         {
24             for (int i = 0; i < 20; i++)
25             {
26                 this.cmb_Port.Items.Add("COM" + i.ToString());
27             }
28
29             this.cmb_Port.Text = ConfigurationManager.AppSettings["Port"].ToString();
30         }
31
32         private void btn_Set_Click(object sender, EventArgs e)
33         {
34             Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //首先打开配置文件
35             cfa.AppSettings.Settings["Port"].Value = this.cmb_Port.Text;
36             cfa.Save();  //保存配置文件
37             ConfigurationManager.RefreshSection("appSettings");  //刷新配置文件
38             this.Close();
39         }
40     }
41 }

FrmParamSet

转载于:https://www.cnblogs.com/xiketangedu/p/11104770.html

基于C#实现与新大陆扫码枪通信相关推荐

  1. zynq tcp如何从网口发数据_基于TCP/IP协议的电口通信

    之前有介绍过TCP/IP协议的实现是通过轻量级LWIP协议实现的,具体在FPGA中实现又可以分为多种方式,具体如下: 图8‑98 LWIP协议在FPGA中的实现方式 LWIP可以通过硬核实现或者软核实 ...

  2. 差分跳频MATLAB,基于Matlab的短波差分跳频通信仿真设计与实现

    第29卷第5期沈阳理工大学学报V01.29No.52010年10月JOURNALOFSHENYANGLIGONGUNIVERSIrⅣOct.2010文章编号:1003-1251(2010)05-001 ...

  3. 从入门到入土:基于Python采用TCP协议实现通信功能的程序

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  4. STM32CubeMX基于HAL库实现简单串口通信

    STM32CubeMX基于HAL库实现简单串口通信 板子:STM32F103C8 编译工具:Keil+STM32CubeMX 烧录工具:FlyMcu 串口调试程序:XCOM 实验要求: 1)设置波特率 ...

  5. STM32应用开发实践教程:基于 CAN 总线的多机通信应用开发

    5.2.1 任务分析 本任务要求设计一个基于 CAN 总线的多机通信系统,该系统须具备交互多种类型数据(如 环境温度.环境湿度和光照强度等)的能力. 发送单元对待发数据进行编码,组建 CAN 数据帧, ...

  6. 一种基于中继模式的跨链通信方案

    一.X链 Cannal起源 开源生态模式.去中心化的服务.以及各种加密货币,这一系列技术的出现,使人们得到启发和思考,去中心的的区块链是网络是能变我们生活的很多方面.比如比特币.以太坊.Zcash.超 ...

  7. APP Inventor 基于网络微服务器的即时通信APP

    APP Inventor 基于网络微服务器的即时通信APP  最近,老师要求我们用APP Inventor做一个APP,简单地做了一个即时通信的APP(超低配版的QQ,逃~),连肝了几个晚 上,目前已 ...

  8. 【水声自适应通信】基于OFDM的水声自适应调制通信系统性能matlab仿真

    1.软件版本 matlab2013b 2.本算法理论知识 (1)  调研收集水声通信信道模型和正交频分复用(OFDM)技术的有关资料,熟悉OFDM 原理和水声通信信道模型, 熟悉MATLAB工具. ( ...

  9. 反射内存 延时_一种基于反射内存卡的OpenDDS分布式通信方法与流程

    本发明属于分布式通信技术领域,尤其涉及一种基于反射内存卡的OpenDDS分布式通信方法. 背景技术: 随着分布式系统复杂程度的不断增加和研发规模的迅速扩大,系统集成的难度和风险都在大幅提高.提供实时系 ...

  10. linux moxa 多串口卡_基于MOXA多串口卡的多串口通信的实现

    基于 MOXA 多串口卡的多串口通信的实现 Implementation of M ultiple Serial Port Communication B ased on M OXA Multipor ...

最新文章

  1. ASP.NET MVC自定义ActionResult实现文件压缩
  2. 干货!整理了50个 Pandas 高频使用技巧,强烈建议收藏!
  3. 【系列】EOS开发1 开发环境搭建
  4. 蚂蚁金服面试3+2次,最终有惊无险通过!
  5. shell脚本中的命令替换
  6. 使用plsql连接远程oracle数据库配置
  7. facade 提供一个接口,通过这个接口,可以使一个子系统更容易使用。
  8. C++继承时的对象内存位置(一)有成员变量遮蔽时的内存分布
  9. 导师实验室对学生影响有多大?
  10. Android中Service的启动方式及Activity与Service的通信方式
  11. Java学习笔记——反射
  12. Oracle client 安装、配置
  13. VOC数据集将XML转为对应的train.txt,trainval.txt,val.txt,test.txt
  14. 关于前端页面的meta标签的属性及其用法
  15. 极通EWEBS远程接入v4.2六步实施法
  16. 清华大学保研计算机推荐信模板,清华保研推荐信模板
  17. Termux使用教程
  18. C#实现发送短信到手机
  19. 阿里云的域名和ip绑定
  20. 常见未授权访问漏洞详解

热门文章

  1. 【书评】《IPD 华为研发之道》(著者:刘选鹏)
  2. Twaver-HTML5基础学习(32)Network样式andTree样式
  3. spider.php使用方法,phpspider爬虫框架怎么用
  4. C语言快速解决反转链表
  5. 机器学习笔试面试整理
  6. Python爬取网上文章并发表到微信公众号
  7. android 4g wifi热点,全网通吃!中兴MF910S 4G无线热点上手体验
  8. MAC系统级鼠标手势功能软件BetterAndBetter
  9. 自定义微信小程序TabBar
  10. 笔记本电脑摄像头频闪问题,打开摄像头出现错误代码0xA00F4244(0xC00D36D5)的问题解决方法