Exemplo n.º 1
0
        private void SendSock_OnReceived(CSerialDriver driver, string strMessage)
        {
            if (!Monitor.TryEnter(_keylock2, TimeSpan.FromSeconds(1)))
            {
                SystemLogger.Log(Level.Exception, "Monitor.TryEnter Timeout 1 second.", SendSock.Name + "_Driver");
            }
            try
            {
                for (int i = 0; i < strMessage.Length; i++)
                {
                    switch (strMessage[i])
                    {
                    case (char)0x02:
                        _sbPacket2 = new StringBuilder();
                        break;

                    case (char)0x03:
                        OnRecieveMessage2.BeginInvoke(_sbPacket2.ToString(), null, null);
                        break;

                    default:
                        _sbPacket2.Append(strMessage[i]);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                SystemLogger.Log(Level.Exception, ex.Message, SendSock.Name + "Driver");
            }
            finally
            {
                Monitor.Exit(_keylock2);
            }
        }
Exemplo n.º 2
0
        public SocketDriverASCII(String module, String IP, int Port)
        {
            _mouldeID = module;

            receiveSock = new CSerialDriver();
            SendSock    = new CSerialDriver();
            _ip         = IP;
            receivePort = Port;
            sendPort    = Port + 1;

            receiveSock.Name                    = module + "_" + receivePort.ToString();
            receiveSock.OnReceived             += RecvreceiveSock_OnReceived;
            receiveSock.ConnectionStateChanged += Sock_ConnectionStateChanged;

            SendSock.Name                    = module + "_" + sendPort.ToString();
            SendSock.OnReceived             += SendSock_OnReceived;
            SendSock.ConnectionStateChanged += Sock_ConnectionStateChanged;

            receiveSock.EnableLog(false, false);
        }
Exemplo n.º 3
0
        public void SocketConnected(string module, int socketCount, string IP, int Port, bool isActive)
        {
            if (String.IsNullOrEmpty(IP))
            {
                return;
            }

            int iPort = Port;

            sock_dic = new Dictionary <string, CSerialDriver>();

            EqpId = module;

            for (int i = 1; i <= socketCount; i++)
            {
                CSerialDriver serialDrv = new CSerialDriver();
                serialDrv.Name = module + "_" + iPort.ToString();
                serialDrv.ConnectionStateChanged += Sock_ConnectionStateChanged;
                serialDrv.OnReceivedBytes        += Recv_sock_OnReceivedBytes;
                if (isActive)
                {
                    serialDrv.ConnectionInfoString = string.Format("MODE=TCP_ACTIVE, IP={0}, PORT={1}, KEEP_ALIVE=1", IP, iPort);
                    serialDrv.ActiveOpen();
                }
                else
                {
                    serialDrv.ConnectionInfoString = string.Format("MODE=TCP_PASSIVE, PORT={0}, KEEP_ALIVE=1", iPort);
                    serialDrv.Open();
                }
                serialDrv.EnableLog(false, false);
                sock_dic.Add(serialDrv.Name, serialDrv);
                iPort++;
            }

            _byteModule = new byte[EqpId.Length];

            for (int i = 0; i < EqpId.Length; i++)
            {
                _byteModule[i] = Convert.ToByte(EqpId[i]);
            }
        }
Exemplo n.º 4
0
        public SocketDriverIPS(String module, String IP, int Port)
        {
            _sock = new CSerialDriver();
            _ip   = IP;
            _port = Port;

            sock_dic = new Dictionary <string, CSerialDriver>();

            _sock.Name                    = module;
            _sock.OnReceived             += Recv_sock_OnReceived;
            _sock.ConnectionStateChanged += Sock_ConnectionStateChanged;

            _mouldeID = module;

            heartbeatTimer          = new System.Timers.Timer();
            heartbeatTimer.Interval = IpsTelegram.HEARTBEAT_INTERVAL;
            heartbeatTimer.Elapsed += new System.Timers.ElapsedEventHandler(HeartbeatTimer_Elapsed);

            _sock.EnableLog(false, false);

            sock_dic.Add(_sock.Name, _sock);
        }
Exemplo n.º 5
0
        public void Recv_sock_OnReceived(CSerialDriver driver, string strMessage)
        {
            heartbeatCounter = 0;
            if (!Monitor.TryEnter(_keylock, TimeSpan.FromSeconds(1)))
            {
                SystemLogger.Log(Level.Exception, "Monitor.TryEnter Timeout 1 second.", _mouldeID + "_Driver");
            }
            try
            {
                {
                    for (int i = 0; i < strMessage.Length; i++)
                    {
                        switch (strMessage[i])
                        {
                        case (char)0x02:
                            _sbPacket = new StringBuilder();
                            break;

                        case (char)0x03:
                            OnRecieveMessage.BeginInvoke(_sbPacket.ToString(), null, null);
                            break;

                        default:
                            _sbPacket.Append(strMessage[i]);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SystemLogger.Log(Level.Exception, ex.Message, _sock.Name + "_Driver");
            }
            finally
            {
                Monitor.Exit(_keylock);
            }
        }
Exemplo n.º 6
0
        void Recv_sock_OnReceivedBytes(CSerialDriver driver, byte[] bytes)
        {
            // 드라이버 이름이 필요하면  driver.Name 확인

            lock (_keylock)
            {
                if (EqpId.Length != 4)
                {
                    return;
                }

                try
                {
                    if (_bytelist == null)
                    {
                        _bytelist = new List <byte>();
                    }

                    _bytelist.AddRange(bytes);

                    bool retry = false;

                    do
                    {
                        retry = false;

                        int startPos = CUtil.PatternAt(_bytelist.ToArray(), _byteModule) - 1;

                        if (startPos > 0)
                        {
                            _sbPacket = new List <byte>();

                            for (int i = startPos; i < _bytelist.Count; i++)
                            {
                                _sbPacket.Add(_bytelist[i]);

                                if (_sbPacket.Count == CommonHeader.GetHeaderSize())
                                {
                                    // parsing header. & 앞으로 받을 남은 길이 확인.
                                    commonHeader = new CommonHeader();
                                    commonHeader.SetHeader(_sbPacket.ToArray());

                                    if (commonHeader.DataLength > 1000)
                                    {
                                        OnUpdateText?.Invoke("Driver Error : Packet Length Error");

                                        _bytelist.RemoveRange(0, i);
                                        retry = true;
                                        break;
                                    }
                                }
                                else if (_sbPacket.Count > CommonHeader.GetHeaderSize())
                                {
                                    //_sbPacket 의 카운터가 header 보다 크면 header 생성이 되었다고 보고.
                                    //총 길이가 header size + Data 길이 etx 만큼 계속 받음.
                                    if (_sbPacket.Count == commonHeader.DataLength + CommonHeader.GetHeaderSize() + 1)
                                    {
                                        int  index = _sbPacket.Count;
                                        byte check = _sbPacket[index - 1];
                                        if (check != (byte)CConstant._etx)
                                        {
                                            OnUpdateText?.Invoke("Error Packet: not exist ETX");
                                            OnUpdateMakeLogText?.Invoke(2, bytes);
                                        }
                                        else
                                        {
                                            byte[] dataPacket = new byte[commonHeader.DataLength];
                                            Array.Copy(_sbPacket.ToArray(), CommonHeader.GetHeaderSize(), dataPacket, 0, commonHeader.DataLength);
                                            List <short> listdata = CUtil.ByteToShortList(dataPacket);

                                            OnUpdateMakeLogText?.Invoke(1, _sbPacket.ToArray());

                                            if (OnRecievePacket != null)
                                            {
                                                try
                                                {
                                                    //OnRecievePacket(commonHeader, listdata);
                                                    OnRecievePacket.BeginInvoke(commonHeader, listdata, null, null);
                                                }
                                                catch (Exception ex)
                                                {
                                                    SystemLogger.Log(Level.Exception, ex.Message, "Driver");
                                                }
                                            }
                                        }
                                        _bytelist.RemoveRange(0, i);
                                        retry = true;
                                        break;
                                    }
                                }
                            }
                        }
                    } while (retry);
                }
                catch (Exception ex)
                {
                    SystemLogger.Log(Level.Exception, ex.Message, "Driver");
                }
            }
            //driver.Send("R");
        }