private void OnToolDisconnect(HcToolInfo info) { var lockTaken = false; try { // lock Monitor.TryEnter(Sessions, TickPeriod, ref lockTaken); // check lock taken if (!lockTaken) { return; } // get session var session = Sessions.Find(x => x.ToolInfo.Mac == info.Mac); // check session if (session == null) { return; } // disconnect session.Disconnect(); } finally { // check lock taken if (lockTaken) { // unlock Monitor.Exit(Sessions); } } }
private void InterfaceOnReceivedMsg(HcToolInfo info, Command cmd, int addr, int[] values) { // check mac address if (SelectedSession == null || SelectedSession.ToolInfo.Mac != info.Mac) { return; } // add log if (cmd == Command.Mor) { AddLog($@"== {info.Ip} : Cmd:{cmd} / Addr:{addr} / Len:{values.Length}"); } // check command switch (cmd) { case Command.Read: Invoke(new EventHandler(delegate { nmCount.Value = values[0]; })); break; case Command.Mor: break; case Command.Write: break; case Command.Info: // check tool serial if (info.Serial != string.Empty) { break; } Invoke(new EventHandler(delegate { lbIp.Text = $@"IP: {info.Ip}"; btOpen.Text = @"CLOSE"; })); break; case Command.Graph: break; case Command.GraphRes: break; case Command.GraphAd: break; case Command.Error: break; default: throw new ArgumentOutOfRangeException(nameof(cmd), cmd, null); } }
/// <summary> /// Disconnect manual tool /// </summary> /// <param name="portName"></param> public void DisConnectManualTool(string portName) { var info = new HcToolInfo(); // get values var values = info.GetValues(); // change com port values[23] = Convert.ToByte(portName.Substring(3)); // change mac number values[31] = values[23]; // set values info.SetValues(values); // disconnect tool OnToolRemoved(info); }
private void ScannerOnToolAttach(HcToolInfo info) { var lockTakenScan = false; var lockTakenRegister = false; try { // lock Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan); Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister); // check lock taken if (!lockTakenScan || !lockTakenRegister) { return; } // get tools var register = FindToolInfo(RegisteredTools, info.Mac); var scan = FindToolInfo(ScannedTools, info.Mac); // check scan tool switch (scan) { case null when register == null: // add scan tool ScannedTools.Add(info); break; case null: // set values register.SetValues(info.GetValues()); // tool connect event ToolConnect?.Invoke(register); break; } } finally { // unlock if (lockTakenScan) { Monitor.Exit(ScannedTools); } if (lockTakenRegister) { Monitor.Exit(RegisteredTools); } } }
private void OnToolConnect(HcToolInfo info) { var lockTaken = false; try { // lock Monitor.TryEnter(Sessions, TickPeriod, ref lockTaken); // check lock taken if (!lockTaken) { return; } // find session var session = Sessions.Find(x => x.ToolInfo.Mac == info.Mac); // check session if (session == null) { // new session session = new HcSession(info); // set session event session.ConnectionChanged += OnConnectionChanged; session.SessionReceived += OnSessionReceived; session.EventReceived += OnSessionReceived; // add session Sessions.Add(session); } // setup session.SetUp(info.Serial != string.Empty ? CommType.Ethernet : CommType.Serial); // set message queue size and block size session.MaxQueueSize = MaxQueueSize; session.MaxBlockSize = MaxBlockSize; // connect session session.Connect(); } finally { // check lock taken if (lockTaken) { // unlock Monitor.Exit(Sessions); } } }
/// <summary> /// Connect manual tool /// </summary> /// <param name="portName">port name</param> public void ConnectManualTool(string portName) { var info = new HcToolInfo(); // get values var values = info.GetValues(); // change com port values[23] = Convert.ToByte(portName.Substring(3)); // change baud rate values[24] = (57600 >> 8) & 0xFF; values[25] = 57600 & 0xFF; // change mac number values[31] = values[23]; // set values info.SetValues(values); // connect tool OnToolConnect(info); }
private void ScannerOnToolAlive(HcToolInfo info) { var lockTakenScan = false; var lockTakenRegister = false; try { // lock Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan); Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister); // check lock taken if (!lockTakenScan || !lockTakenRegister) { return; } // get tools var register = FindToolInfo(RegisteredTools, info.Mac); var scan = FindToolInfo(ScannedTools, info.Mac); // check scan/register tool if (scan == null && register == null) { // add scan tool ScannedTools.Add(info); } else if (register != null) { // event ToolAlive?.Invoke(info); } } finally { // unlock if (lockTakenScan) { Monitor.Exit(ScannedTools); } if (lockTakenRegister) { Monitor.Exit(RegisteredTools); } } }
/// <summary> /// Tool register /// </summary> /// <param name="info">Tool information</param> /// <returns>result</returns> public bool RegisterTool(HcToolInfo info) { var lockTakenScan = false; var lockTakenRegister = false; try { // lock Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan); Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister); // check lock taken if (!lockTakenScan || !lockTakenRegister) { return(false); } // get register tool if (FindToolInfo(RegisteredTools, info.Mac) != null) { return(false); } // remove scan tool ScannedTools.Remove(FindToolInfo(ScannedTools, info.Mac)); // add register tool RegisteredTools.Add(info); // tool added event ToolAdded?.Invoke(info); return(true); } finally { // unlock if (lockTakenScan) { Monitor.Exit(ScannedTools); } if (lockTakenRegister) { Monitor.Exit(RegisteredTools); } } }
/// <summary> /// Get session /// </summary> /// <param name="info">tool information</param> /// <returns>result</returns> public HcSession GetSession(HcToolInfo info) { var lockTaken = false; try { // lock Monitor.TryEnter(Sessions, TickPeriod, ref lockTaken); // find session return(!lockTaken ? null : Sessions.Find(x => x.ToolInfo.Mac == info.Mac)); } finally { // check lock taken if (lockTaken) { // unlock Monitor.Exit(Sessions); } } }
private void OnChangedConnect(HcToolInfo info, ConnectionState state) { // check tool serial if (info.Serial != string.Empty) { // check mac address if (SelectedSession == null || SelectedSession.ToolInfo.Mac != info.Mac) { return; } } else { // set selected tool SelectedSession = HCommAir.GetSession(info); } Invoke(new EventHandler(delegate { // set state lbState.Text = $@"STATE: {state}"; })); }
private void ClientReceived(IAsyncResult ar) { // end receive point var endPoint = new IPEndPoint(IPAddress.Any, McPort); // receive data var recv = Client.EndReceive(ar, ref endPoint); // begin receive Client.BeginReceive(ClientReceived, null); // check length if (recv.Length < 4) { return; } // check header var id = recv[0] << 8 | recv[1]; var cmd = (ScanCommand)(recv[2] << 8 | recv[3]); // check scan acknowledge and id and length if (cmd != ScanCommand.ScanAck || id != TransactionId || recv.Length - 4 != HcToolInfo.Count) { return; } // set tool information var info = new HcToolInfo(recv.Skip(4)); // check MD/MDTC if (info.ToolType == HcToolInfo.ToolModelType.None || info.ToolType == HcToolInfo.ToolModelType.MD || info.ToolType == HcToolInfo.ToolModelType.MDT) { return; } // lock searched tool list if (!Monitor.TryEnter(SearchTools, _timeoutSpan)) { return; } try { // find tool var tool = SearchTools.Find(x => x.Mac == info.Mac); // check find tool if (tool == null) { // set timeout time info.Timeout = ScanPeriod + 1000; // debug console Console.WriteLine($@"Attach Tool: {info.Ip}"); Console.WriteLine($@"Attach Tool: {info.Mac}"); // add tool SearchTools.Add(info); // attached ToolAttach?.Invoke(info); } else { // refresh tool tool.ResetTime(); // alive ToolAlive?.Invoke(tool); } } finally { // unlock Monitor.Exit(SearchTools); } }
/// <summary> /// Registered tools load binary file /// </summary> /// <param name="path">file path</param> /// <returns>result</returns> public bool LoadRegisterTools(string path) { var lockTakenScan = false; var lockTakenRegister = false; try { // lock Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan); Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister); // check lock taken if (!lockTakenRegister) { return(false); } // file stream using (var fs = new FileStream(path, FileMode.Open)) { // binary writer using (var br = new BinaryReader(fs)) { do { // get tool information var tool = new HcToolInfo(br.ReadBytes(HcToolInfo.Count)); // find scan tool var scan = FindToolInfo(ScannedTools, tool.Mac); // check scan tool if (scan != null) { // remove ScannedTools.Remove(scan); } // find register tool if (FindToolInfo(RegisteredTools, tool.Mac) != null) { continue; } // register tool RegisteredTools.Add(tool); // check file length } while (br.BaseStream.Position < br.BaseStream.Length); } } return(true); } catch (Exception e) { // debug Console.WriteLine($@"{e.Message}"); // result return(false); } finally { // unlock if (lockTakenScan) { Monitor.Exit(ScannedTools); } if (lockTakenRegister) { Monitor.Exit(RegisteredTools); } } }
private void OnSessionReceived(HcToolInfo info, Command cmd, int addr, int[] values) { ReceivedMsg?.Invoke(info, cmd, addr, values); }
private void OnConnectionChanged(HcToolInfo info, ConnectionState state) { // event ChangedConnect?.Invoke(info, state); }
/// <summary> /// UnRegister tool /// </summary> /// <param name="info">tool information</param> /// <returns>result</returns> public bool UnRegisterTool(HcToolInfo info) => Manager.UnRegisterTool(info);