Beispiel #1
0
        public Result Comm(Commor commor, string input, int timeout)
        {
            var omronFinsNet   = (OmronFinsNet)commor.Connector;
            var ethernetCommor = (EthernetCommor)commor.Communicator;

            return(Result.Success);
        }
Beispiel #2
0
        public Result Connect(Commor commor)
        {
            var omronFinsNet   = new OmronFinsNet();
            var ethernetCommor = (EthernetCommor)commor.Communicator;

            try
            {
                omronFinsNet.ConnectTimeOut           = 2000;
                omronFinsNet.IpAddress                = ethernetCommor.IP;
                omronFinsNet.Port                     = ethernetCommor.Port;
                omronFinsNet.SA1                      = NetHelper.GetIpLastValue(NetHelper.GetLocalIpByRegex("192.168.1.*"));;
                omronFinsNet.DA2                      = 0;
                omronFinsNet.ByteTransform.DataFormat = (HslCommunication.Core.DataFormat)Enum.Parse(typeof(HslCommunication.Core.DataFormat), "CDAB");

                OperateResult connect = omronFinsNet.ConnectServer();
                if (connect.IsSuccess)
                {
                    commor.Connector = omronFinsNet;
                    return(Result.Success);
                }
                else
                {
                    return(new Result(string.Format("连接{0}失败({1} :{2})", ethernetCommor.Name, ethernetCommor.IP, ethernetCommor.Port)));
                }
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
        }
Beispiel #3
0
        public Result Comm(Commor commor, string input, int timeout)
        {
            var serialCommor = (SerialCommor)commor.Communicator;
            var recvData     = string.Empty;
            var serialPort   = (SerialPort)commor.Connector;

            try
            {
                if (!input.Contains(" "))
                {
                    serialPort.Write(input);
                }
                else
                {
                    var bytes = Arthur.Core.Transfer._Convert.StrToToHexBytes(input);
                    serialPort.Write(bytes, 0, bytes.Length);
                }
                Thread.Sleep(timeout);
                Byte[] InputBuf = new Byte[128];
                serialPort.Read(InputBuf, 0, serialPort.BytesToRead);
                recvData = Encoding.ASCII.GetString(InputBuf).Trim(new char[] { '\0', '\r', '\n' });

                if (string.IsNullOrEmpty(recvData))
                {
                    return(new Result("规定时间内串口未返回数据:" + serialPort.PortName));
                }
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
            return(Result.SuccessHasData(recvData));
        }
Beispiel #4
0
        public Result Connect(Commor commor)
        {
            commor.Connector = null;
            var ethernetCommor = (EthernetCommor)commor.Communicator;
            var socket         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                ConnectSocketDelegate connect     = ConnectSocket;
                IAsyncResult          asyncResult = connect.BeginInvoke(socket, ethernetCommor.IP, ethernetCommor.Port, null, null);
                bool success = asyncResult.AsyncWaitHandle.WaitOne(1000, false);
                if (!success)
                {
                    return(new Result(string.Format("连接{0}失败({1} :{2})", ethernetCommor.Name, ethernetCommor.IP, ethernetCommor.Port)));
                }
                //socket.Connect(ethernetCommor.IP, ethernetCommor.Port); 连接失败时卡死 超时(20s)
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
            if (!socket.Connected)
            {
                return(new Result(string.Format("连接失败:{0}:{1}", ethernetCommor.IP, ethernetCommor.Port)));
            }
            commor.Connector = socket;
            return(Result.Success);
        }
Beispiel #5
0
        public Result EndConnect(Commor commor)
        {
            var socket = (Socket)commor.Connector;

            if (socket != null)
            {
                socket.Close();
            }
            return(Result.Success);
        }
Beispiel #6
0
        public Result EndConnect(Commor commor)
        {
            var omronFinsNet = (OmronFinsNet)commor.Connector;

            if (omronFinsNet != null)
            {
                omronFinsNet.ConnectClose();
                commor.Connector = null;
            }
            return(Result.Success);
        }
Beispiel #7
0
        public Result EndConnect(Commor commor)
        {
            var serialPort = (SerialPort)commor.Connector;

            try
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
                serialPort.Dispose();
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
            return(Result.Success);
        }
Beispiel #8
0
        public Result Connect(Commor commor)
        {
            commor.Connector = null;
            var serialCommor = (SerialCommor)commor.Communicator;
            var serialPort   = new SerialPort(serialCommor.PortName, serialCommor.BaudRate, serialCommor.Parity, serialCommor.DataBits);

            try
            {
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                }
            }
            catch (Exception ex)
            {
                return(new Result(string.Format("连接{0}失败", commor.Communicator.Name), ex));
            }
            commor.Connector = serialPort;
            return(Result.Success);
        }
Beispiel #9
0
        public Result ReadInt(Commor commor, string addr)
        {
            try
            {
                var omronFinsNet           = (OmronFinsNet)commor.Connector;
                OperateResult <int> result = omronFinsNet.ReadInt32(addr);

                if (result.IsSuccess)
                {
                    return(Result.SuccessHasData(result.Content));
                }
                else
                {
                    return(new Result(string.Format("{0}:{1}", result.ErrorCode, result.Message)));
                }
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
        }
Beispiel #10
0
        public Result Write(Commor commor, string addr, ushort value)
        {
            try
            {
                var           omronFinsNet = (OmronFinsNet)commor.Connector;
                OperateResult result       = omronFinsNet.Write(addr, value);

                if (result.IsSuccess)
                {
                    return(Result.Success);
                }
                else
                {
                    return(new Result(string.Format("{0}:{1}", result.ErrorCode, result.Message)));
                }
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
        }
Beispiel #11
0
        public Result Comm(Commor commor, string input, int timeout)
        {
            var socket         = (Socket)commor.Connector;
            var ethernetCommor = (EthernetCommor)commor.Communicator;
            var recvData       = string.Empty;

            try
            {
                if (socket == null || !socket.Connected)
                {
                    commor.Connect();
                    socket = (Socket)commor.Connector;
                }

                if (socket.Connected)
                {
                    Byte[] sendBytes = Encoding.UTF8.GetBytes(input + "\r");
                    socket.Send(sendBytes);

                    Thread.Sleep(10);

                    Byte[] Data = new Byte[1024];

                    if (timeout > 0)
                    {
                        Stopwatch sw             = new Stopwatch();
                        var       getStr         = string.Empty;
                        var       connectSuccess = false;
                        // Try to open the connection, if anything goes wrong, make sure we set connectSuccess = false
                        Thread t = new Thread(delegate()
                        {
                            try
                            {
                                sw.Start();
                                socket.Receive(Data);
                                getStr         = Encoding.ASCII.GetString(Data);
                                connectSuccess = true;
                            }
                            catch { }
                        });

                        // Make sure it's marked as a background thread so it'll get cleaned up automatically
                        t.IsBackground = true;
                        t.Start();

                        // Keep trying to join the thread until we either succeed or the timeout value has been exceeded
                        while (timeout > sw.ElapsedMilliseconds)
                        {
                            if (t.Join(1))
                            {
                                break;
                            }
                        }
                        // IsCommunicatting = false;
                        // If we didn't connect successfully, throw an exception
                        if (connectSuccess)
                        {
                            //throw new Exception("Timed out while trying to connect.");
                            recvData = getStr.Trim('\0', ' ', '\r', '\n');
                        }
                        else
                        {
                            return(new Result("读取数据超时:" + ethernetCommor.Name));
                        }
                    }
                    else
                    {
                        socket.Receive(Data);
                        recvData = Encoding.ASCII.GetString(Data).Trim('\0', ' ', '\r', '\n');
                    }
                }
                else
                {
                    throw new Exception("重复连接失败");
                }
            }
            catch (Exception ex)
            {
                var msg = string.Format("和{0}通信出现异常!原因:{1}", ethernetCommor.Name, ex.Message);
                return(new Result(msg));
            }
            return(Result.SuccessHasData(recvData));
        }