Exemplo n.º 1
0
        /// <summary>
        /// 第一个参数指定服务类型
        /// 第二个参数指定串口名称
        /// 第三个参数指定波特率
        /// 第四个参数指定数据位
        /// 第五个参数指定校验位
        /// 第六个参数指定停止位
        /// 第七个参数指定要发送命令,
        /// 如果第七个参数为-1,就不发送
        /// </summary>
        public HardWareModule()
        {
            Get["/HardWare/m_portName/m_boundRate/m_dataBits/m_parity/m_stopBits/m_command"] =
                parameter =>
            {
                String m_portName = parameter.m_portName;
                String m_command  = parameter.m_command;
                ///如果此串口没有注册,就注册
                if (!SerialPortFactory.SerialPortPool.ContainsKey(m_portName))
                {
                    int              m_boundRate = parameter.m_boundRate;
                    int              m_dataBits  = parameter.m_dataBits;
                    Parity           m_parity    = parameter.m_parity;
                    StopBits         m_stopBits  = parameter.m_stopBits;
                    SerialPortEntity entity      = new SerialPortEntity(m_portName,
                                                                        m_boundRate, m_dataBits, m_parity, m_stopBits, m_command);
                    SerialPortFactory.SerialPortPool.Add(m_portName, entity);
                }

                ///否则取得注册过的串口的实体
                SerialPortEntity port = SerialPortFactory.SerialPortPool[m_portName];
                if (!port.Port.IsOpen)
                {
                    port.Port.Open();
                }
                port.SendCommand(m_command);
                Thread.Sleep(500);

                ///等待500毫秒,从dataTeceived事件取回结果
                return(port.ReceiveContent);
            };
        }
Exemplo n.º 2
0
        private string ProcSerialPort(WebSocketSession session, string e)
        {
            HashMap parameter  = JsonConvert.DeserializeObject <HashMap>(e);
            String  m_portName = parameter.GetValue <String>("m_portName");

            if (String.IsNullOrEmpty(m_portName))
            {
                m_portName = Tools.GetAppSetting("m_portName");
            }


            String m_command = parameter.GetValue <String>("m_command");

            if (String.IsNullOrEmpty(m_command))
            {
                m_command = Tools.GetAppSetting("m_command");
            }


            ///如果此串口没有注册,就注册
            if (!SerialPortFactory.SerialPortPool.ContainsKey(m_portName))
            {
                int m_boundRate = parameter.GetValue <int>("m_boundRate");


                if (m_boundRate < 1)
                {
                    m_boundRate = Int32.Parse(Tools.GetAppSetting("m_boundRate"));
                }
                int m_dataBits = 8;



                Parity           m_parity   = Parity.None;
                StopBits         m_stopBits = StopBits.One;
                SerialPortEntity entity     = new SerialPortEntity(m_portName,
                                                                   m_boundRate, m_dataBits, m_parity, m_stopBits, m_command);
                SerialPortFactory.SerialPortPool.Add(m_portName, entity);
            }

            ///否则取得注册过的串口的实体
            SerialPortEntity port = SerialPortFactory.SerialPortPool[m_portName];

            if (!port.Port.IsOpen)
            {
                port.Port.Open();
            }

            ///不再等待,将sessionID推送到当前串口,从dataTeceived事件取回结果,推送回sessionID
            ///实际多少时间就是多少时间

            SerialPortEntity.currentSession = session;

            port.SendCommand(m_command);

            return("{}");
        }
Exemplo n.º 3
0
        public override String sendComCommand()
        {
            if (String.IsNullOrEmpty(PortName))
            {
                PortName = Tools.GetAppSetting("m_portName");
            }



            if (String.IsNullOrEmpty(Command))
            {
                Command = Tools.GetAppSetting("m_command");
            }


            ///如果此串口没有注册,就注册
            if (!SerialPortFactory.SerialPortPool.ContainsKey(PortName))
            {
                int m_boundRate = 9600;


                if (m_boundRate < 1)
                {
                    m_boundRate = Int32.Parse(Tools.GetAppSetting("m_boundRate"));
                }
                int m_dataBits = 8;



                Parity           m_parity   = Parity.None;
                StopBits         m_stopBits = StopBits.One;
                SerialPortEntity entity     = new SerialPortEntity(PortName,
                                                                   m_boundRate, m_dataBits, m_parity, m_stopBits, Command);
                SerialPortFactory.SerialPortPool.Add(PortName, entity);
            }

            ///否则取得注册过的串口的实体
            SerialPortEntity port = SerialPortFactory.SerialPortPool[PortName];



            if (!port.Port.IsOpen)
            {
                port.Port.Open();
            }



            ///不再等待,将sessionID推送到当前串口,从dataTeceived事件取回结果,推送回sessionID
            ///实际多少时间就是多少时间


            port.SendCommand(Command);

            BaseResponseBean bean = new BaseResponseBean();

            bean.ErrorCode    = 0;
            bean.ErrorMessage = "发送成功";

            bean.Key = "comSendCommand";

            return(JsonConvert.SerializeObject(bean));
        }