示例#1
0
        /// <summary>
        /// event when serial port received data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(5);
            int len = mSerialPort.BytesToRead;

            if (len <= 0)
            {
                return;
            }

            byte[] rcvdata = new byte[len];
            int    rlen    = mSerialPort.Read(rcvdata, 0, len);

            if (rlen != len)
            {
                return;
            }
            mSerialPort.DiscardInBuffer();
            MsgLogger.PushMsg(Name, "Receive", rcvdata);
            //响应
            byte[] rspdata;
            if (ParseMessage(rcvdata, out rspdata))
            {
                Thread.Sleep(mRespDelay);
                mSerialPort.Write(rspdata, 0, rspdata.Length);
                MsgLogger.PushMsg(Name, "Send", rspdata);
            }
        }
示例#2
0
 /// <summary>
 /// 停止设备
 /// </summary>
 /// <returns></returns>
 public bool Stop()
 {
     if (!Running)
     {
         return(true);
     }
     mToRun = false;
     if ((mSerialPort != null) && (mSerialPort.IsOpen))
     {
         try{
             mSerialPort.DataReceived -= new SerialDataReceivedEventHandler(port_DataReceived);
             mSerialPort.Close();
         }
         catch (Exception ex)
         {
             MsgLogger.PushMsg(Name, "Stop", ex.Message);
             return(false);
         }
     }
     foreach (CanbusDevice md in DeviceDict.Values)
     {
         md.Stop();
     }
     MsgLogger.PushMsg(Name, "Stop", "Success");
     Running = false;
     return(true);
 }
示例#3
0
        /// <summary>
        /// event when port received data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int len = port.BytesToRead;

            if (len <= 0)
            {
                return;
            }

            byte[] rcvdata = new byte[len];
            int    rlen    = port.Read(rcvdata, 0, len);

            port.DiscardInBuffer();
            if (rlen == 0)
            {
                return;
            }
            MsgLogger.PushMsg(Name, "Receive", rcvdata);

            int nom;

            byte[][] outdata;
            int[]    delays;
            if (GetMatchedMesage(rcvdata, out nom, out outdata, out delays))
            {
                for (int i = 0; i < nom; i++)
                {
                    System.Threading.Thread.Sleep(delays[i]);
                    port.Write(outdata[i], 0, outdata[i].Length);
                    MsgLogger.PushMsg(Name, "Send", outdata[i]);
                }
            }
        }
示例#4
0
 /// <summary>
 /// 停止设备
 /// </summary>
 /// <returns></returns>
 public bool Stop()
 {
     mToRun  = false;
     Running = false;
     MsgLogger.PushMsg(Name, "Stop", "Success.");
     return(true);
 }
示例#5
0
 /// <summary>
 /// 启动设备
 /// </summary>
 /// <returns></returns>
 public bool Start()
 {
     mToRun    = true;
     Running   = true;
     mLastTime = DateTime.Now.Second;
     MsgLogger.PushMsg(Name, "Start", "Success.");
     return(true);
 }
示例#6
0
 /// <summary>
 /// 停止设备
 /// </summary>
 /// <returns></returns>
 public bool Stop()
 {
     if (Running)
     {
         serverSocket.Close();
         Running = false;
         MsgLogger.PushMsg(Name, "Stop", "Success");
     }
     ReStart();
     return(true);
 }
示例#7
0
        /// <summary>
        /// 启动设备
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            mToRun    = true;
            mLastTime = DateTime.Now.Second;
            Thread thdUpdateRegs = new Thread(new ThreadStart(UpdateRegs));

            thdUpdateRegs.IsBackground = true;
            thdUpdateRegs.Start();
            MsgLogger.PushMsg(Name, "Start", "Success.");
            return(true);
        }
示例#8
0
 /// <summary>
 /// 增加设备
 /// </summary>
 /// <param name="device"></param>
 /// <returns></returns>
 public bool AddDevice(ModbusDevice device)
 {
     if ((DeviceDict.ContainsKey(device.Address)) || (DeviceNameDict.ContainsValue(device.Name)))
     {
         MsgLogger.PushMsg(device.Name, "Add", "Fail, already exist.");
         return(false);
     }
     else
     {
         DeviceDict.Add(device.Address, device);
         DeviceNameDict.Add(device.Address, device.Name);
         MsgLogger.PushMsg(device.Name, "Add", "Success.");
         return(true);
     }
 }
示例#9
0
 /// <summary>
 /// 启动总线
 /// </summary>
 /// <returns></returns>
 public bool Start()
 {
     mToRun = true;
     if (mCommType == 1)
     {
         if ((mSerialPort == null) || (!mSerialPort.IsOpen))
         {
             try{
                 mSerialPort               = new SerialPort(mComPort + "A", 9600, Parity.None, 8, StopBits.One);
                 mSerialPort.ReadTimeout   = mComTimeout;
                 mSerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                 mSerialPort.Open();
                 Running = true;
             }
             catch (Exception ex) {
                 MsgLogger.PushMsg(Name, "Start", ex.Message);
                 return(false);
             }
         }
     }
     else if (mCommType == 2)
     {
         try
         {
             mServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
             IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(mNetIP), mNetPort);
             mServer.Bind(ipe);
             mServer.Listen(10);
             mClients = new Hashtable();
             Thread lj = new Thread(new ThreadStart(WaitForConnect));
             lj.IsBackground = true;
             lj.Start();
             Running = true;
         }
         catch (Exception ex)
         {
             MsgLogger.PushMsg(Name, "Start", ex.Message);
             return(false);
         }
     }
     foreach (ModbusDevice md in DeviceDict.Values)
     {
         md.Start();
     }
     MsgLogger.PushMsg(Name, "Start", "Success");
     return(true);
 }
示例#10
0
 /// <summary>
 /// 添加设备
 /// </summary>
 /// <param name="device"></param>
 public static bool AddDevice(NetDevice device)
 {
     lock (obj)
     {
         if (DeviceList.ContainsKey(device.Name))
         {
             MsgLogger.PushMsg(device.Name, "Add", "Already exist.");
             return(false);
         }
         else
         {
             DeviceList.Add(device.Name, device);
             MsgLogger.PushMsg(device.Name, "Add", "Success.");
             return(true);
         }
     }
 }
示例#11
0
 /// <summary>
 /// 添加
 /// </summary>
 /// <param name="bus"></param>
 public static bool AddModbus(Modbus bus)
 {
     lock (obj)
     {
         if (ModbusDict.ContainsKey(bus.Name))
         {
             MsgLogger.PushMsg(bus.Name, "Add", "Fail, already exist.");
             return(false);
         }
         else
         {
             ModbusDict.Add(bus.Name, bus);
             MsgLogger.PushMsg(bus.Name, "Add", "Success.");
             return(true);
         }
     }
 }
示例#12
0
 /// <summary>
 /// 删除设备
 /// </summary>
 /// <param name="device"></param>
 /// <returns></returns>
 public bool DeleteDevice(string devicename)
 {
     foreach (ModbusDevice device in DeviceDict.Values)
     {
         if (device.Name == devicename)
         {
             //停止设备
             device.Stop();
             //删除
             DeviceDict.Remove(device.Address);
             DeviceNameDict.Remove(device.Address);
             MsgLogger.PushMsg(devicename, "Delete", "Success.");
             return(true);
         }
     }
     MsgLogger.PushMsg(devicename, "Delete", "Fail, not exist.");
     return(false);
 }
示例#13
0
        /// <summary>
        /// net Message Loop
        /// </summary>
        /// <param name="cl"></param>
        private void ReceiveLoop(object cl)
        {
            Socket client = cl as Socket;

            if (client == null)
            {
                return;
            }
            string clientName = client.RemoteEndPoint.ToString();

            mClients.Add(clientName, client);

            byte[] rbytes = new byte[256];
            int    count;

            while (mToRun)
            {
                count = 0;
                try{
                    count = client.Receive(rbytes);
                    if (count > 0)
                    {
                        byte[] rcvdata = new byte[count];
                        for (int i = 0; i < count; i++)
                        {
                            rcvdata[i] = rbytes[i];
                        }
                        MsgLogger.PushMsg(Name, "Receive", rcvdata);
                        byte[] rspdata;
                        if (ParseMessage(rcvdata, out rspdata))
                        {
                            Thread.Sleep(mRespDelay);
                            client.Send(rspdata);
                            MsgLogger.PushMsg(Name, "Send", rspdata);
                        }
                    }
                }
                catch {
                    break;
                }
            }
            mClients.Remove(clientName);
        }
示例#14
0
 /// <summary>
 /// 启动设备
 /// </summary>
 /// <returns></returns>
 public bool Start()
 {
     if ((port == null) || (!port.IsOpen))
     {
         try{
             port               = new SerialPort(mPortName + "A", 9600, Parity.None, 8, StopBits.One);
             port.RtsEnable     = true;
             port.ReadTimeout   = mTimeOut;
             port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
             port.Open();
         }
         catch (Exception ex) {
             MsgLogger.PushMsg(Name, "Start", ex.Message);
             return(false);
         }
     }
     MsgLogger.PushMsg(Name, "Start", "Success");
     Running = true;
     return(true);
 }
示例#15
0
 /// <summary>
 /// 启动设备
 /// </summary>
 /// <returns></returns>
 public bool Start()
 {
     if ((port == null) || (!port.IsOpen))
     {
         try{
             port               = new SerialPort(mPortName, mBaudRate, mParity, mDataBits, mStopBits);
             port.DataBits      = mDataBits;
             port.RtsEnable     = true;
             port.ReadTimeout   = mTimeoutR;
             port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
             port.Open();
         }
         catch (Exception ex) {
             MsgLogger.PushMsg(Name, "Start", ex.Message);
             return(false);
         }
     }
     MsgLogger.PushMsg(Name, "Start", "Success");
     Running = true;
     return(true);
 }
示例#16
0
 /// <summary>
 /// 停止设备
 /// </summary>
 /// <returns></returns>
 public bool Stop()
 {
     if (port != null)
     {
         if (port.IsOpen)
         {
             try{
                 port.Close();
             }
             catch (Exception ex)
             {
                 MsgLogger.PushMsg(Name, "Stop", ex.Message);
                 return(false);
             }
         }
         MsgLogger.PushMsg(Name, "Stop", "Success");
     }
     ReStart();
     Running = false;
     return(true);
 }
示例#17
0
 /// <summary>
 /// 删除
 /// </summary>
 /// <param name="busname"></param>
 public static bool DeleteModbus(string busname)
 {
     lock (obj)
     {
         if (ModbusDict.ContainsKey(busname))
         {
             //停止
             Modbus bus;
             ModbusDict.TryGetValue(busname, out bus);
             bus.Stop();
             //删除
             ModbusDict.Remove(busname);
             MsgLogger.PushMsg(busname, "Delete", "Success.");
             return(true);
         }
         else
         {
             MsgLogger.PushMsg(busname, "Delete", "Fail, not exist.");
             return(false);
         }
     }
 }
示例#18
0
 /// <summary>
 /// 等待客户端连接
 /// </summary>
 private void WaitForConnect()
 {
     while (Running)
     {
         Thread.Sleep(10);
         try{
             Socket client = serverSocket.Accept();
             object oj     = client;
             Thread ml     = new Thread(new ParameterizedThreadStart(ReceiveLoop));
             ml.IsBackground = true;
             ml.Start(oj);
         }
         catch (Exception ex) {
             if (serverSocket != null)
             {
                 serverSocket.Close();
             }
             Running = false;
             MsgLogger.PushMsg(Name, "Stop", ex.Message);
         }
     }
 }
示例#19
0
 /// <summary>
 /// 启动设备
 /// </summary>
 /// <returns></returns>
 public bool Start()
 {
     try
     {
         serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         IPAddress  ipa = IPAddress.Parse(mDeviceIP);
         IPEndPoint ipe = new IPEndPoint(ipa, mDevicePort);
         serverSocket.Bind(ipe);
         serverSocket.Listen(MAXCLIENTS);
         Thread lj = new Thread(new ThreadStart(WaitForConnect));
         lj.IsBackground = true;
         lj.Start();
     }
     catch (Exception ex)
     {
         MsgLogger.PushMsg(Name, "Start", ex.Message);
         return(false);
     }
     MsgLogger.PushMsg(Name, "Start", "Success");
     Running = true;
     return(true);
 }
示例#20
0
 /// <summary>
 /// 删除设备
 /// </summary>
 /// <param name="devicename"></param>
 public static bool DeleteDevice(string devicename)
 {
     lock (obj)
     {
         if (DeviceList.ContainsKey(devicename))
         {
             //停止设备
             NetDevice device;
             DeviceList.TryGetValue(devicename, out device);
             device.Stop();
             //删除
             DeviceList.Remove(devicename);
             MsgLogger.PushMsg(devicename, "Delete", "Success.");
             return(true);
         }
         else
         {
             MsgLogger.PushMsg(devicename, "Delete", "Not exist.");
             return(false);
         }
     }
 }
示例#21
0
        /// <summary>
        /// Message Loop
        /// </summary>
        /// <param name="cl"></param>
        private void ReceiveLoop(object cl)
        {
            Socket client = cl as Socket;

            if (client == null)
            {
                return;
            }
            string clientName = client.RemoteEndPoint.ToString();

            AllClients.Add(clientName, client);

            byte[] rbytes = new byte[RECEIVEBUFFERSIZE];
            int    count;

            while (true)
            {
                count = 0;
                try{
                    count = client.Receive(rbytes);
                    if (count > 0)
                    {
                        string restr = Encoding.Default.GetString(rbytes);
                        MsgLogger.PushMsg(clientName, "Receive", restr);

                        byte[] sbytes = rbytes;
                        client.Send(sbytes, sbytes.Length, SocketFlags.None);
                        string sestr = Encoding.Default.GetString(sbytes);
                        MsgLogger.PushMsg(clientName, "Send", sestr);
                    }
                }
                catch {
                    break;
                }
            }
            AllClients.Remove(clientName);
        }
示例#22
0
 /// <summary>
 /// 启动总线
 /// </summary>
 /// <returns></returns>
 public bool Start()
 {
     mToRun = true;
     if ((mSerialPort == null) || (!mSerialPort.IsOpen))
     {
         try{
             mSerialPort               = new SerialPort(mComPort + "A", 9600, Parity.None, 8, StopBits.One);
             mSerialPort.ReadTimeout   = mComTimeout;
             mSerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
             mSerialPort.Open();
             Running = true;
         }
         catch (Exception ex) {
             MsgLogger.PushMsg(Name, "Start", ex.Message);
             return(false);
         }
     }
     foreach (CanbusDevice md in DeviceDict.Values)
     {
         md.Start();
     }
     MsgLogger.PushMsg(Name, "Start", "Success");
     return(true);
 }