//private log4net.ILog log = log4net.LogManager.GetLogger("MyLogger"); public ProcThread() { UsbTxPrepare(); txQueue = usb.TxQueue; rxQueue = usb.RxQueue; latch = usb.Latch; }
public void Send(byte[] bytes, int length) { // Packet must start with correct header if (bytes[0] == 0xAA) { int dLen = bytes[3]; byte[] packet = new byte[dLen + 5]; Array.Copy(bytes, packet, bytes.Length); int chk = 0; for (int i = 1; i < bytes.Length; i++) { chk = chk + bytes[i]; } packet[packet.Length - 1] = (byte)chk; if (!TxQueue.TryToEnqueue(packet)) { ErrorLog.Error("Error in {0}, could not Enqueue packet to send", this.GetType().Name); } if (TxThread == null || TxThread.ThreadState != Thread.eThreadStates.ThreadRunning) { TxThread = new Thread(SendBufferProcess, null, Thread.eThreadStartOptions.CreateSuspended); TxThread.Priority = Thread.eThreadPriority.HighPriority; TxThread.Name = string.Format("Samsung Display ComPort - Tx Handler"); TxThread.Start(); } } else { throw new FormatException("Packet did not begin with correct value"); } }
object SendBufferProcess(object o) { while (true) { try { string data = TxQueue.Dequeue(); #if DEBUG CrestronConsole.Print("Revolabs Tx: {0}", data); #endif if (programStopping) { return(null); } else { this.ComPort.Send(data); } Thread.Sleep(50); } catch (Exception e) { if (e.Message != "ThreadAbortException") { ErrorLog.Exception(string.Format("{0} - Exception in tx buffer thread", GetType().Name), e); } } } }
object SendBufferProcess(object o) { while (!TxQueue.IsEmpty) { try { Byte[] packet = TxQueue.Dequeue(); #if DEBUG //CrestronConsole.Print("Samsung Tx: "); //Tools.PrintBytes(packet, packet.Length); #endif if (programStopping) { TxQueue.Clear(); return(null); } else { this.ComPort.Send(packet, packet.Length); CrestronEnvironment.AllowOtherAppsToRun(); Thread.Sleep(10); } } catch (Exception e) { if (e.Message != "ThreadAbortException") { ErrorLog.Exception(string.Format("{0} - Exception in tx buffer thread", GetType().Name), e); } } } return(null); }
/// <summary> /// Stops the current processes, and clears the TxQueue /// </summary> public virtual void Dispose() { isStopping = true; while (CommManagerThread.IsAlive) { ; } TxQueue.Clear(); }
public void Send(string stringToSend) { if (stringToSend.Length > 0 && stringToSend[stringToSend.Length - 1] != 0x0d) { stringToSend = stringToSend + "\r"; } if (stringToSend.Length > 0) { TxQueue.Enqueue(stringToSend); } }
public void TxQueueIntegrationTest() { var tx = new List <string>(); var queue = new TxQueue <string>((s) => tx.Add(s), 1); var timer = new TestTimer(); queue.Mutex = new TestMutex(); queue.Timer = timer; timer.ElapseImmediately = true; var commandsToSend = new string[] { "tx1", "tx2", "tx3", "tx4" }; Parallel.ForEach(commandsToSend, (c) => queue.Send(c)); tx.Count.Should().Be(4); tx.Should().Contain(commandsToSend); }
private USB() { _isUSBAvailable = false; _delayms = 10; usbName = "Bulkloop - no device"; try { usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB); usbDevices.DeviceAttached += new EventHandler(usbDevices_DeviceAttached); usbDevices.DeviceRemoved += new EventHandler(usbDevices_DeviceRemoved); intDevice(); txQueue = new TxQueue(); rxQueue = new RxQueue(); latch = new Latch(); } catch { // the old CyUSB.dll is not compatible with Win10 } }
/// <summary> /// Handles the reception and transmission of messages /// </summary> protected virtual void CommManager() { while (!isStopping) { lock (CommQueue) { if (CommQueue.Count > 0) { if (CommQueue.Peek().TargetID == ID) { OnMessageRxEvent(CommQueue.Dequeue()); } } } if (TxQueue.Count > 0) { CommQueue.Enqueue(TxQueue.Dequeue()); } Thread.Sleep(100); } }
/// <summary> /// Sends a message to other modules /// </summary> /// <param name="packet">The comm packet to send</param> public virtual void SendMessage(CommPacket packet) { packet.AuthorID = ID; TxQueue.Enqueue(packet); }