通过TCP/IP方式不间断的读取条码扫描结果,并高速处理,停机不断线



请勿复制黏贴转载,不尊重原创作者的劳动,做可耻的小偷!


代码方便你们应用到自己的工程里,可自由使用,谢谢!


如有不明白,欢迎咨询: 邮箱 [email protected]  QQ:458978 微信号:nieweiking


承接各种工业仪器仪表,电子装置的通讯程序开发、数据库应用开发,具体面议:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets ;
using System.Threading;

namespace CodeReaderClient.ConnectType
{
 public    class TCPConnectClass
    {
     /// 
     /// 以太网通讯对象
     /// 
     public  TcpClient MyTcpClient;
     /// 
     /// 数据流操作
     /// 
     public NetworkStream MyNetworkStream;
     /// 
     /// 条码结果
     /// 
     public string BarCodeStr = string.Empty;
     /// 
     /// IP地址
     /// 
     public  string _IPAddressStr = string.Empty;

     /// 
     /// 通讯端口
     /// 
     public int _Port = 8080;
     /// 
     /// 初始化
     /// 
     /// 
     /// 
     public TCPConnectClass (string IPAddressStr,int Port)
     {
         _IPAddressStr = IPAddressStr;
         _Port = Port;
         MyTcpClient = new TcpClient();
         Thread Td = new Thread(RunPorcedue);
         Td.IsBackground = true;
         Td.Start();
     }
   

     /// 
     /// 连接检查方法,保持断线重连
     /// 
     public  void Connect()
     {
         try
         {
             if (!MyTcpClient.Connected)
             {
                 IPAddress IPAddress1 = IPAddress.Parse(_IPAddressStr);
                 MyTcpClient = new TcpClient();
                 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)
         {
             
         }
     }

     /// 
     /// 循环工作线程
     /// 
     private void  RunPorcedue()
     {

         byte[] Buffer = new byte[128];
         int Count = 0;
         while (true)
         {

             //检查连接//发送心跳包
                 Connect();
                 //解析接收到的数据
                 try
                 {
                     if (MyTcpClient .Available >0)
                     {
                         Buffer = new byte[128];
                         Count = MyNetworkStream.Read(Buffer, 0, Buffer.Length);
                         if (Count > 0)
                         {
                             BarCodeStr = ExplanationByte(Buffer);
                         }
                     }
                 }
                 catch (Exception err)
                 {
                     string a = err.Message;
                 }

                 Thread.Sleep(100);
             
         }
     }

     /// 
     /// 解析来自条码扫描头的字符串
     /// 
     /// 
     /// 
   private   string ExplanationByte(byte[] Buffer)
     {
         if (Buffer .Length >0)
         {
             string Value = System.Text.ASCIIEncoding.ASCII.GetString(Buffer, 0, Buffer.Length);
             Value = Value.Replace("\r", "");
             Value = Value.Replace("\n", "");
             Value = Value.Replace(" ", "");
             return Value;
         }
         else
         {
             return string.Empty;
         }
         
     }
         
    }
}





你可能感兴趣的:(C#开发技术)