public static bool Close() { try { Device.Close(); return(true); } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } return(false); }
public static bool Open(string strPortName) { Device.PortName = strPortName; try { Device.Open(); return(true); } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } return(false); }
private static void Moitor(object sender, ElapsedEventArgs e) { m_Mutex.WaitOne(); if (m_Computer.Ports.SerialPortNames.Count != m_PortCount) { try { m_PortCount = m_Computer.Ports.SerialPortNames.Count; JavascriptEvent.PortChange(); if (SerialPortManager.Device.IsOpen) { try { bool exist = m_Computer.Ports.SerialPortNames.Contains <string>(SerialPortManager.Device.PortName); if (!exist) { SerialPortManager.Close(); } } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } finally { JavascriptEvent.SerialChange(); } } if (!SerialPortManager.Device.IsOpen) { ConnectionManager.Start(); } } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } } m_Mutex.ReleaseMutex(); }
public static bool Write(string deal) { try { if (Device.IsOpen) { Device.Write(deal); return(true); } } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } return(false); }
private static void ConnectionDeviceThread(object sender, ElapsedEventArgs e) { m_TimerEnabled = true; try { if (SerialPortManager.Device.IsOpen) { return; } string deal = PortAgreement.EncryptionDevice(); for (int i = 0; i < 3; i++) { if (m_StopTimer) { return; } foreach (string item in PortMonitor.GetPortNames) { if (m_StopTimer) { return; } if (m_Serial == null) { m_Serial = InitSerial(); } m_Serial.PortName = item; try { m_Serial.Open(); m_Serial.Write(deal); bool ret = WaitResult(); if (ret) { m_Serial.DataReceived -= SerialDataReceived; Thread.Sleep(10); SerialPortManager.SetSerial(m_Serial); return; } m_Serial.Close(); } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); } } } if (!SerialPortManager.Device.IsOpen) { JavascriptEvent.ConnectionFail(); } } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } finally { m_Serial = null; m_ConnectionDevice = false; m_TimerEnabled = false; m_StopTimer = false; } }
private static void SerialDataReceived(int port) { Thread.Sleep(10); try { int len = Device.GetIqueue; if (len <= 0) { try { OverTimer.Stop(); switch (m_Bytes[0]) { case 10: if (m_Bytes[1] == 8) { JavascriptEvent.OperationOver(); } else { DataManager.VerifyingRepetition(m_Bytes); OverTimer.start(); } break; case 30: if (m_Bytes[1] == 8) { OperationResult = OperationResults.Fail; } else { OperationResult = OperationResults.Success; } break; case 43: bool ret = m_Bytes[1] != 8; JavascriptEvent.EndMessage(ret); if (ret) { JavascriptEvent.IncrementingNumber(); } break; case 160: JavascriptEvent.OperationOver(); if (m_Bytes[1] == 0) { JavascriptEvent.NewsMessage("发行器编号设置成功。"); } else { JavascriptEvent.NewsMessage("发行器编号设置失败,请重新操作。"); } break; } } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } finally { m_Bytes.Clear(); } return; } byte[] by = Device.Read(len); if (m_Bytes == null) { m_Bytes = new List <byte>(); } m_Bytes.AddRange(by); } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } }
public Main() : base("http://res.app.local/index.html") { InitializeComponent(); MainForm = this; LoadHandler.OnLoadEnd += HtmlLoadEnd; GlobalObject.AddFunction("ShowDevTools").Execute += (func, args) => { this.RequireUIThread(() => { Chromium.ShowDevTools(); }); }; GlobalObject.AddFunction("ChangeSerialConnection").Execute += (func, args) => { this.RequireUIThread(() => { string portName = args.Arguments[0].StringValue; if (SerialPortManager.Device.IsOpen) { SerialPortManager.Close(); } else { SerialPortManager.Open(portName); } args.SetReturnValue(SerialPortManager.Device.IsOpen); }); }; GlobalObject.AddFunction("ChangeConnectionState").Execute += (func, args) => { this.RequireUIThread(() => { bool state = args.Arguments[0].BoolValue; if (state) { ConnectionManager.Start(); } else { ConnectionManager.Stop(); } }); }; GlobalObject.AddFunction("ReconnectDevice").Execute += (func, args) => { this.RequireUIThread(() => { ConnectionManager.Start(); }); }; GlobalObject.AddFunction("RefreshClick").Execute += (func, args) => { this.RequireUIThread(() => { bool ret = false; try { DataManager.Params.Clear(); string deal = PortAgreement.ReadAllCard(); SerialPortManager.Write(deal); OverTimer.start(); ret = true; } catch (Exception ex) { Log4Helper.ErrorInfo(ex.Message, ex); JavascriptEvent.ErrorMessage(ex.Message); } args.SetReturnValue(ret); }); }; GlobalObject.AddFunction("DownloadClick").Execute += (func, args) => { int count = DataManager.Params.Where(e => e.State != "设置成功" && e.DataType == "正常").Count(); if (count == 0) { args.SetReturnValue(count); return; } this.RequireUIThread(() => { string strClientNumber = args.Arguments[0].StringValue; Task.Factory.StartNew(() => { foreach (Param item in DataManager.Params) { if (item.State != "设置成功" && item.DataType == "正常") { string deal = PortAgreement.WriteClientNumber(item.CardNumber, strClientNumber); bool ret = SerialPortManager.Write(deal); if (ret) { SerialPortManager.OperationResult = OperationResults.None; for (int i = 0; i < 500; i++) { Thread.Sleep(10); if (SerialPortManager.OperationResult != OperationResults.None) { if (SerialPortManager.OperationResult == OperationResults.Success) { item.State = "设置成功"; DataManager.ViewListDisplay(); } break; } } } } } JavascriptEvent.OperationOver(); }); }); args.SetReturnValue(-1); }; GlobalObject.AddFunction("SetDeviceClient").Execute += (func, args) => { this.RequireUIThread(() => { string strClientNumber = args.Arguments[0].StringValue; string deal = PortAgreement.EncryptionDevice(strClientNumber); bool ret = SerialPortManager.Write(deal); args.SetReturnValue(ret); }); }; GlobalObject.AddFunction("SetCardNumber").Execute += (func, args) => { this.RequireUIThread(() => { string strOldNumber = args.Arguments[0].StringValue; string strCardNumber = args.Arguments[1].StringValue; string strType = args.Arguments[2].StringValue; string deal = PortAgreement.WriteCardNumber(strOldNumber, strCardNumber, strType); bool ret = SerialPortManager.Write(deal); if (ret) { if (strCardNumber != "797979" || strCardNumber != "123456") { ConfigManager.SetConfig("Number", strCardNumber); } } args.SetReturnValue(ret); }); }; }