예제 #1
0
 /// <summary>
 /// 连接已建立事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 static void Server_ClientConnected(object sender, AsyncSocketEventArgs e)
 {
     LogHelpr.Info("客户端连接成功" + e._state.ClientSocket.RemoteEndPoint.ToString());
     socket         = e._state;
     timer          = new System.Timers.Timer();
     timer.Elapsed += Timer_Elapsed;
     timer.Interval = 60000000;
     timer.Enabled  = true;
 }
예제 #2
0
        private void Main_Load(object sender, EventArgs e)
        {
            string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
            string assemblyDirPath  = Path.GetDirectoryName(assemblyFilePath);
            string configFilePath   = assemblyDirPath + "\\log4net.xml";

            XmlConfigurator.Configure(new FileInfo(configFilePath));
            client = new ClientTCP();
            client.Connect("127.0.0.1", 5000);
            LogHelpr.Info("客户端已启动");
        }
예제 #3
0
 public void ServiceStart()
 {
     localPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000);
     server     = new ServerTCP(localPoint);
     server.Server.ClientConnected    += Server_ClientConnected;
     server.Server.ClientDisconnected += Server_ClientDisconnected;
     server.Server.DataReceived       += Server_DataReceived;
     server.Server.CompletedSend      += Server_CompletedSend;
     server.Start();
     LogHelpr.Info("已启动");
 }
예제 #4
0
        /// <summary>
        /// 读取串口数据
        /// </summary>
        public void SerialPortRead(object _serialPort)
        {
            SerialPort serialPort = (SerialPort)_serialPort;

            while (_keepReading)
            {
                //间隔时间
                if (_jcpl > 0)
                {
                    Thread.Sleep(_jcpl);
                }
                //2秒后重新检测  串口对象未实例化
                if (serialPort == null)
                {
                    Thread.Sleep(2000);
                    continue;
                }
                //串口未打开
                if (!serialPort.IsOpen)
                {
                    Thread.Sleep(2000);
                    continue;
                }
                try
                {
                    #region 字节读取
                    byte[] readBuffer = new byte[serialPort.BytesToRead];
                    int    count      = serialPort.Read(readBuffer, 0, readBuffer.Length);
                    if (count != 0)
                    {
                        //转为16进制字符
                        string Code = ConvertHelper.ByteToHexStr(readBuffer);
                        //触发接收数据事件
                        ResolveDevdisplay(serialPort, Code);
                    }
                    #endregion
                }
                catch (TimeoutException)
                {
                    LogHelpr.Info("串口读取超时");
                }
                catch (Exception ex)
                {
                    LogHelpr.Info("串口读取发生错误:" + ex.ToString());
                }
            }
        }
예제 #5
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="Code"></param>
 public void Sent(string Code, int PortIndex)
 {
     try
     {
         byte[] sendData = null;
         sendData = ConvertHelper.StrToHexByte(Code);
         //写入数据
         if (SerialPortModel.SerialPorts[PortIndex].IsOpen)
         {
             SerialPortModel.SerialPorts[PortIndex].Write(sendData, 0, sendData.Length);
         }
     }
     catch (Exception ex)
     {
         LogHelpr.Error("错误:" + ex.ToString());
     }
 }
예제 #6
0
 /// <summary>
 /// 连接已断开事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 static void Server_ClientDisconnected(object sender, AsyncSocketEventArgs e)
 {
     LogHelpr.Info("连接已断开" + DateTime.Now.ToString());
 }
예제 #7
0
 /// <summary>
 /// 接收到数据事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 static void Server_DataReceived(object sender, AsyncSocketEventArgs e)
 {
     byte[] data = sender as byte[];
     LogHelpr.Info(Encoding.Default.GetString(data));
 }
예제 #8
0
 /// <summary>
 /// 数据发送完毕事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 static void Server_CompletedSend(object sender, AsyncSocketEventArgs e)
 {
     LogHelpr.Info("数据发送完毕" + DateTime.Now.ToString());
 }