static DeviceMgr() { bool bDebugGPIB = false; SysLog(""); SysLog(DateTime.Now.ToLongTimeString()); Thread.Sleep(3000); WaitEvent = new AutoResetEvent(false); OverEvent = new AutoResetEvent(false); inbuffer = new StringBuilder(); cmdbuffer = new StringBuilder(); port = new SerialPort(); #region init port port.BaudRate = 9600; if (!bDebugGPIB) { port.PortName = "COM1"; //for old , it is COM3, for new it is COM1 } else { port.PortName = "COM1";//for old , it is COM3, for new it is COM1 } port.Parity = Parity.None; port.DataBits = 8; port.StopBits = StopBits.One; port.NewLine = "\r"; int i; for (i = 0; i < 10; i++) { if (bDebugGPIB) { break; } port.Open(); if (port.IsOpen) { break; } Thread.Sleep(3000); } if (i >= 10) { Program.DbgShow("Failed to open " + port.PortName); throw new Exception("Failed to open port A"); } #endregion #region init cmdport cmdport = new SerialPort(); cmdport.BaudRate = 9600; if (!bDebugGPIB) { cmdport.PortName = "COM2"; //for old is COM2, for new is COM4 } else { cmdport.PortName = "COM2"; //for old is COM2, for new is COM4 } cmdport.Parity = Parity.None; cmdport.DataBits = 8; cmdport.StopBits = StopBits.One; cmdport.NewLine = "\r"; cmdport.Open(); if (!cmdport.IsOpen) { port.Close(); Program.DbgShow("fail: cmdport open " + cmdport.PortName); throw new Exception("Failed to open port B"); } else { cmdport.DiscardInBuffer(); cmdport.DataReceived += new SerialDataReceivedEventHandler(cmdport_DataReceived); } #endregion agent_access = false; actmsg = new ActMessage(); msg_loop = new Thread(new ThreadStart(MessageHandler)); msg_loop.IsBackground = false; if (bDebugGPIB) { return; } msg_loop.Start(); }
static DeviceMgr() { bool bDebugGPIB = false; SysLog(""); SysLog(DateTime.Now.ToLongTimeString()); Thread.Sleep(3000); WaitEvent = new AutoResetEvent(false); OverEvent = new AutoResetEvent(false); inbuffer = new StringBuilder(); cmdbuffer = new StringBuilder(); port = new SerialPort(); #region init port port.BaudRate = 9600; if (!bDebugGPIB) port.PortName = "COM5"; else port.PortName = "COM11"; port.Parity = Parity.None; port.DataBits = 8; port.StopBits = StopBits.One; port.NewLine = "\r"; int i; for (i = 0; i < 10; i++) { if (bDebugGPIB) break; port.Open(); if (port.IsOpen) break; Thread.Sleep(3000); } if(i >= 10) throw new Exception("Failed to open port A"); #endregion #region init cmdport cmdport = new SerialPort(); cmdport.BaudRate = 9600; if(!bDebugGPIB) cmdport.PortName = "COM4"; else cmdport.PortName = "COM10"; cmdport.Parity = Parity.None; cmdport.DataBits = 8; cmdport.StopBits = StopBits.One; cmdport.NewLine = "\r"; cmdport.Open(); #endregion if (!cmdport.IsOpen) { port.Close(); throw new Exception("Failed to open port B"); } cmdport.DiscardInBuffer(); cmdport.DataReceived += new SerialDataReceivedEventHandler(cmdport_DataReceived); agent_access = false; actmsg = new ActMessage(); msg_loop = new Thread(new ThreadStart(MessageHandler)); msg_loop.IsBackground = false; if(bDebugGPIB) return; msg_loop.Start(); }
static public void MessageHandler() { Reset(); Loaded = true; while (true) { if (!WaitEvent.WaitOne(10, false)) { ScanPort(); continue; } WaitEvent.Reset(); try { #region navmeter action ActMessage msg = DeviceMgr.actmsg; if (msg.action == "navto30v" || msg.action == "navto1v" || msg.action == "navto120mv" || msg.action == "navto10mv") { if (msg.action == "navto1v") { // port.Write(NAV_INIT); //init port.Write(StringResource.str("NAV_1V_" + RunWnd.syscfg.sNavmeter)); //1v // DelayWrite(NAV_AFLTON); //slowmode and aflton } if (msg.action == "navto120mv") { port.Write(StringResource.str("NAV_120MV_" + RunWnd.syscfg.sNavmeter)); //120mv } if (msg.action == "navto10mv") { port.Write(StringResource.str("NAV_10MV_" + RunWnd.syscfg.sNavmeter)); //10mv } if (msg.action == "navto30v") { port.Write(StringResource.str("NAV_30V_" + RunWnd.syscfg.sNavmeter)); //10mv } Thread.Sleep(4000); actmsg.action = "done"; OverEvent.Set(); continue; } if (msg.action == "navread") { reading = -9999; port.DiscardInBuffer(); port.Write(StringResource.str("NAV_READ_" + RunWnd.syscfg.sNavmeter)); //1v Thread.Sleep(5); int timeout = 400; while ((timeout-- > 0) && (reading < -9000)) { ScanPort(); Thread.Sleep(5); } actmsg.action = "done"; OverEvent.Set(); continue; } if (msg.action == "turnk") { KState(msg.value); actmsg.action = "done"; OverEvent.Set(); continue; } if (msg.action.StartsWith("MEAS")) { string[] cmd = msg.action.Split(new char[] { '|' }); if (cmd.Length == 6) { RelayState(cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5]); } actmsg.action = "done"; OverEvent.Set(); continue; } #endregion if (actmsg.action != "") { throw new Exception("Invalid command " + actmsg.action); } } catch (Exception ex) { SysLog(ex.Message); Debug.WriteLine(ex.Message); actmsg.action = "fail"; OverEvent.Set(); } } }
static DeviceMgr() { bool bDebugGPIB = true; Thread.Sleep(3000); WaitEvent = new AutoResetEvent(false); OverEvent = new AutoResetEvent(false); inbuffer = new StringBuilder(); cmdbuffer = new StringBuilder(); port = new SerialPort(); #region init port port.BaudRate = 9600; if (!bDebugGPIB) { port.PortName = "COM10"; } else { port.PortName = "COM11"; } port.Parity = Parity.None; port.DataBits = 8; port.StopBits = StopBits.One; port.NewLine = "\r"; int i; for (i = 0; i < 10; i++) { if (bDebugGPIB) { break; } port.Open(); if (port.IsOpen) { break; } Thread.Sleep(3000); } if (i >= 10) { throw new Exception("Failed to open port A"); } #endregion /* * #region init cmdport * cmdport = new SerialPort(); * cmdport.BaudRate = 9600; * if(!bDebugGPIB) * cmdport.PortName = "COM11"; * else * cmdport.PortName = "COM10"; * cmdport.Parity = Parity.None; * cmdport.DataBits = 8; * cmdport.StopBits = StopBits.One; * cmdport.NewLine = "\r"; * cmdport.Open(); #endregion * if (!cmdport.IsOpen) * { * port.Close(); * throw new Exception("Failed to open port B"); * } * cmdport.DiscardInBuffer(); * cmdport.DataReceived += new SerialDataReceivedEventHandler(cmdport_DataReceived); */ agent_access = false; actmsg = new ActMessage(); msg_loop = new Thread(new ThreadStart(MessageHandler)); msg_loop.IsBackground = false; if (bDebugGPIB) { return; } msg_loop.Start(); }