public object parse(byte[] package, int type, int tn, byte [] _pid) { msg _msg = new msg(); List <byte> rdm = new List <byte>(); if (package.Length > 0 && package != null && package[0] == 0x7E) { UInt16 size = package[2]; if (package[1] == 5) { size--; } for (int i = 0; i < size; i++) { int index = 0; if (package[1] == 5) { index = 5; } else { index = 4; } rdm.Add(package[index + i]); } Console.WriteLine("========================================================================="); foreach (var item in rdm.ToArray()) { Console.Write(item.ToString("X2") + " "); } Console.WriteLine("\n========================================================================="); if (rdm.Count > 16) { _msg.TransactionNumber = rdm[15]; _msg._pid[0] = rdm[21]; _msg._pid[1] = rdm[22]; switch (type) { case 0: break; case 1: /* * RESPONSE_TYPE_ACK 0x00 * RESPONSE_TYPE_ACK_TIMER 0x01 * RESPONSE_TYPE_NACK_REASON 0x02 * RESPONSE_TYPE_ACK_OVERFLOW 0x03 */ if ((rdm[16] == 0 || rdm[16] == 0x01) && tn == _msg.TransactionNumber && (_pid[0] == _msg._pid[0] && _pid[1] == _msg._pid[1])) { _msg.status = true; } switch (rdm[16]) { case 0x00: _msg.message = "RESPONSE_TYPE_ACK"; break; case 0x01: _msg.message = "RESPONSE_TYPE_ACK_TIMER"; break; case 0x02: _msg.message = "RESPONSE_TYPE_ACK_REASON"; break; case 0x03: _msg.message = "RESPONSE_TYPE_ACK_OVERFLOW"; break; default: break; } _msg.data = new byte[rdm[23]]; for (int i = 0; i < _msg.data.Length; i++) { _msg.data[i] = rdm[24 + i]; } break; default: break; } } } return(_msg); }
public object write(byte[] payload) { byte[] Start = { 0x00 }; byte[] MAB = new byte[256]; if (!dmx512Pro.IsOpen) { dmx512Pro.OpenByDescription("DMX USB PRO"); } //DMX_PRO.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_2, FTDI.FT_PARITY.FT_PARITY_NONE); dmx512Pro.Purge(FTDI.FT_PURGE.FT_PURGE_RX); dmx512Pro.Purge(FTDI.FT_PURGE.FT_PURGE_TX); //DMX_PRO.SetBreak(true); dmx512Pro.SetTimeouts(1000, 500); uint tx = 0; dmx512Pro.SetBaudRate(250000); dmx512Pro.SetBreak(true); System.Threading.Thread.Sleep(10); dmx512Pro.SetBreak(false); dmx512Pro.Write(MAB, 0, ref tx); dmx512Pro.Write(Start, Start.Length, ref tx); //DMX_PRO.Purge(FTDI.FT_PURGE.FT_PURGE_TX); uint rx = 0; int _try = 0; msg _out = (msg)parse(payload, 0, 0, null); _out.info(); Console.WriteLine(string.Format("Try: {0} ", ++_try)); dmx512Pro.Write(payload, payload.Length, ref tx); do { dmx512Pro.GetTxBytesWaiting(ref tx); //Console.WriteLine("Bytes Waiting: " + tx); System.Threading.Thread.Sleep(100); } while (tx != 0); dmx512Pro.GetRxBytesAvailable(ref rx); byte line_status = 0; byte[] buffer = new byte[rx]; dmx512Pro.Read(buffer, rx, ref rx); msg _in = (msg)parse(buffer, 1, _out.TransactionNumber, _out._pid); /////////////////////////////////////////////////////////////////// _in.info(); Console.WriteLine("\n"); /////////////////////////////////////////////////////////////////// return(_in); }