Encapsulate the communication as a class "
The features of the program have advantages:
Traditional wording when the bar code scanning head stop, abnormal power, network anomalies, communication terminal, the program can not resume the connection, the loss of data caused by failure, I write the program, whether the scan head power off or the production line restart, can be the first time to restart the connection and communication, stability is very good.
This is also in a friend old program constantly out of trouble, find me to help, I write again, after the effect is very good, very easy to use, no longer has been a problem.
If you have hardware communication access difficulties and projects, you can contact me to solve, such as RS232 485 422 serial communication, Ethernet interface Instrumentation communication, OPC Server Automation data plc Access can be, welcome to practice my mailbox: [email protected] add me QQ 458 978 verification Fill, you know my QQ number from the blog park can!
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespacecodereaderclient.connecttype{/// <summary> ///[email protected]///Niewei Suzhou 2016-09-10/// </summary> Public classTcpconnectclass {/// <summary> ///Ethernet Communication Object/// </summary> PublicTcpClient mytcpclient; /// <summary> ///Data Flow Operations/// </summary> PublicNetworkStream Mynetworkstream; /// <summary> ///Barcode Results/// </summary> Public stringBarcodestr =string. Empty; /// <summary> ///IP Address/// </summary> Public string_ipaddressstr =string. Empty; /// <summary> ///Communication Port/// </summary> Public int_port =8080; /// <summary> ///Initialize/// </summary> /// <param name= "Ipaddressstr" ></param> /// <param name= "Port" ></param> PublicTcpconnectclass (stringIPADDRESSSTR,intPort) {_ipaddressstr=Ipaddressstr; _port=Port; Mytcpclient=NewTcpClient (); Thread Td=NewThread (runporcedue); Td.isbackground=true; Td.start (); } /// <summary> ///connection check method, keep wire break reconnect/// </summary> Public voidConnect () {Try { if(!mytcpclient.connected) {IPAddress IPAddress1=Ipaddress.parse (_IPADDRESSSTR); Mytcpclient=NewTcpClient (); Mytcpclient.connect (IPAddress1, _port); Mynetworkstream=Mytcpclient.getstream (); } if(mytcpclient.connected) {Mynetworkstream=Mytcpclient.getstream (); byte[] Buffer =New byte[] {0x41 }; Mynetworkstream.write (Buffer,0,1); } } Catch(Exception) {}}/// <summary> ///Looping worker Threads/// </summary> Private voidrunporcedue () {byte[] Buffer =New byte[ -]; intCount =0; while(true) { //Check the connection//Send Heartbeat PackConnect (); //parse the received data Try { if(Mytcpclient. Available >0) {Buffer=New byte[ -]; Count= Mynetworkstream.read (Buffer,0, buffer.length); if(Count >0) {Barcodestr=Explanationbyte (Buffer); } } } Catch(Exception err) {stringA =Err. Message; } thread.sleep ( -); } } /// <summary> ///parse a string from a barcode scan header/// </summary> /// <param name= "Buffer" ></param> /// <returns></returns> Private stringExplanationbyte (byte[] Buffer) { if(Buffer. Length >0) { stringValue = System.Text.ASCIIEncoding.ASCII.GetString (Buffer,0, buffer.length); Value= Value.replace ("\ r",""); Value= Value.replace ("\ n",""); Value= Value.replace (" ",""); returnValue; } Else { return string. Empty; } } }}
Program written, you can use the Network debugging assistant to debug send and receive situation!
Read and write barcode scanning head with Ethernet communication