基于C#实现与新大陆扫码枪通信
程序员文章站
2023-08-20 21:14:15
随着工业互联的发展,扫码枪在很多场合都有所应用,超市、商场以及一些智能工厂。今天主要讲如何通过C#实现与新大陆扫码枪(OY10)进行通信,对于扫码枪的配置,这里就不多说了,结合说明书就可以实现。这里值得注意的是,如果安装驱动后,电脑设备管理器中看不到COM口,可能需要扫一个条形码来设置一下,具体参考 ......
随着工业互联的发展,扫码枪在很多场合都有所应用,超市、商场以及一些智能工厂。今天主要讲如何通过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 }
再者就是界面的调用,直接看代码:
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 }
最后是参数配置,通过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 }