public bool gather(USBMsg msg, int tim) { int ptr = 8; if ((tim != 0) && (lites.Count > 0)) { UInt32 inten = lites[0].Get() & 0x0ff000000; bool doit = false; for (int ndx = 0; ndx < lites.Count; ndx++) { Clr newlvl = lites[ndx].Get(); Clr oldlvl = lstLvls[ndx]; if ((newlvl & 0x0ff000000) != inten) { doit = false; break; } if (((newlvl & 0x000ffffff) == (oldlvl & 0x000ffffff)) && ((newlvl & 0x0ff000000) != (oldlvl & 0x0ff000000))) { doit = true; } } if (doit) { msg.msg[ptr++] = (byte)0; msg.msg[ptr++] = (byte)0; UInt32 intn = inten >> 24; msg.msg[ptr++] = (byte)((intn > 204) ? 204 : intn); msg.msg[ptr++] = (byte)63; for (int ndx = 0; ndx < lites.Count; ndx++) { lstLvls[ndx] = (lstLvls[ndx] & 0x000ffffff) | inten; } } } for (int ndx = 0; ndx < lites.Count; ndx++) { Clr lvl = lites[ndx].Get(); if ((tim == 0) || (lvl != lstLvls[ndx])) { lstLvls[ndx] = lvl; msg.msg[ptr++] = (byte)(((lvl.R >> 4) & 0x0f) + (lvl.G & 0xf0)); msg.msg[ptr++] = (byte)((lvl.B >> 4) & 0x0f); byte i = lvl.I; msg.msg[ptr++] = (i > 212) ? (byte)212 : i; msg.msg[ptr++] = (byte)(ndx); } } msg.size = ptr; return(ptr > 8); }
public override void Send(int tim) { //if (state < 0) // throw new InvalidOperationException("Open failed: " + Name); USBMsg msg = null; foreach (GECEStrand strand in strands) { if (msg == null) { msg = bufrPool.Take(); msg.seq = 0x00; msg.chksum = 0xff; msg.time = tim; msg.size = 8; } if (strand.gather(msg, tim)) { msg.port = strand.Port; if (state < 0) { string hex = "Send: " + Name + ": "; int siz = msg.size; for (int ptr = 0; ptr < siz; ptr++) { hex += " " + msg.msg[ptr].ToString("x2"); } logger.Info(hex); } else { pendMsgs.Add(msg); msg = null; } } } if (msg != null) { bufrPool.Add(msg); } }
private void SendTxData() { logger.Info(">SendTx Ready:" + Name); FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OTHER_ERROR; USBMsg msg = null; UInt32 numBytesWritten = 0; while (!cts.IsCancellationRequested) { if ((pendMsgs.Count > 0) && ((avl - sentMsgs.Count) > 10)) { pendMsgs.TryTake(out msg); msg.seq = ++nxtseq; sentMsgs.Enqueue(msg); string hex = "Send: " + Name + ": "; int siz = msg.size; for (int ptr = 0; ptr < siz; ptr++) { hex += " " + msg.msg[ptr].ToString("x2"); } logger.Info(hex); ftStatus = myFtdiDevice.Write(msg.msg, msg.size, ref numBytesWritten); if (ftStatus != FTDI.FT_STATUS.FT_OK) { logger.Error("<failure: " + Name + ":" + ftStatus.ToString()); } else { logger.Info(string.Format("< {0:x2}", msg.seq)); } } else { Thread.Sleep(5); } } logger.Info(">SendTx Closed:" + Name); }
private void ShowRxData() { logger.Info(">ShowRx Ready: " + Name); FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OTHER_ERROR; UInt32 numBytesAvailable = 0; byte[] resp = new byte[4]; byte cod = 0; byte seq = 0; byte cnt = 0; byte chk = 0; USBMsg msg = null; UInt32 numBytesRead = 0; try { ManualResetEventSlim hdl = new ManualResetEventSlim(false); while (!cts.IsCancellationRequested) { ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to get number of bytes available to read:" + Name + " (error " + ftStatus.ToString() + ")"); return; } if (numBytesAvailable > 3) { ftStatus = myFtdiDevice.Read(resp, numBytesAvailable, ref numBytesRead); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to read data:" + Name + " (error " + ftStatus.ToString() + ")"); } cod = resp[0]; seq = resp[1]; cnt = resp[2]; chk = resp[3]; logger.Info(string.Format("Recv: {4}: {0:x2} {1:x2} {2:x2} {3:x2}", cod, seq, cnt, chk, Name)); switch (cod) { case 0x80: if (sentMsgs.TryPeek(out msg)) { if (msg.seq == seq) { if (sentMsgs.TryDequeue(out msg)) { bufrPool.Add(msg); } } } avl = cnt; if (avl > maxavl) { maxavl = avl; } break; case 0x90: avl = cnt; if (avl > maxavl) { maxavl = avl; } break; default: logger.Error(string.Format(">Invalid resp: {4}: {0:x2} {1:x2} {2:x2} {3:x2}", cod, seq, cnt, chk, Name)); break; } } else { //using (EventWaitHandle hdl = new EventWaitHandle(false, EventResetMode.ManualReset)) //{ // myFtdiDevice.SetEventNotification(0, hdl); // hdl.WaitOne(500); //} hdl.Reset(); //myFtdiDevice.SetEventNotification(FTD2XX_NET.FTDI.FT_EVENTS.FT_EVENT_RXCHAR, hdl.WaitHandle); hdl.Wait(-1, tkn); WaitHandle.WaitAny(new WaitHandle[] { hdl.WaitHandle, tkn.WaitHandle }, -1); } } } finally { } }