using System; using System.Collections.Generic; using System.IO.Ports; using NLog; using Xylem.Common.Hardware.Interfaces.Ports.SerialPorts; using Xylem.Common.Utils.Logging; namespace Xylem.Common.Ui.RfidFunctionalTestApp { internal class Program { private static readonly Lazy Logger = new Lazy(() => { return NLogHelper.CreateOrGetLogger("Program"); }); private static RfidSerialPort _rfidComPort; private static readonly Byte[] GenesisTest = { //write privilege level 8 with CRC error //0x5B, 0x0C, 0x00, 0x0B, 0x00, //0xC4, 0x65, //0x00, 0x04, 0x08, 0x00, 0x00, 0x00 //write privilege level 8 with CRC okay //0x5B, 0x0C, 0x00, 0x0B, 0x00, //0x2D, 0xDC, //0x00, 0x04, 0x08, 0x00, 0x00, 0x00 //read privilege level 0x5B, 0x0C, 0x00, 0x09, 0x00, 0x48, 0xD6, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00 }; private static readonly Byte[] WRiteGenesisPrivilegeLevel8 = { //write privilege level 8 with CRC okay 0x5B, 0x0C, 0x00, 0x0B, 0x00, 0x2D, 0xDC, 0x00, 0x04, 0x08, 0x00, 0x00, 0x00 }; private static readonly Byte[] WriteGenesisDevicePassword = { //write password with multiple write CMD (keep constant) //SY, length , CMD , next CMD 0x5B, 0x16, 0x00, 0x0F, 0x00, //CRC 0x9B, 0xDD, //register (keep constant) 0x01, 0x04, //number of writes in multiple write CMD 0x03, 0x00, //login level 8 password "119564D5A8B0" for Genesis Board SN 204707 //(change for individual Genesis Board) 0x31, 0x31, 0x39, 0x35, 0x36, 0x34, 0x44, 0x35, 0x41, 0x38, 0x42, 0x30 }; private static readonly Byte[] ReadGenesisDevicePcbId = { //read PCB ID 0x5b,0x0c,0x00,0x09,0x00, 0x6e,0x35, // UART //,0xab,0xdd, // RFID 0x0c,0x04,0x00, 0x00, 0x00, 0x00 }; private static readonly SerialPort Sr = new SerialPort { PortName = "COM57", BaudRate = 9600, Handshake = Handshake.None, Parity = Parity.None, StopBits = StopBits.One, DataBits = 8 }; private static readonly String Ident = $"Port:{Sr.PortName}, Protocol:Request, Type:RFID -"; private static void Main() { //var uartPort = new SerialPort("COM26", 9600, Parity.None, 8, StopBits.One); //uartPort.Handshake = Handshake.None; //uartPort.Open(); //var transmit = new RfidTransmitProtocol("COMXX"); //_rfidComPort = new RfidSerialPort( Ident, Sr, transmit.GetTransmitPortSettings()); //0x0B12F0590434C524 //39 12 F0 59 04 34 C5 24 //39 83 C8 58 05 C3 E4 24 //EA 27 F0 59 04 34 C5 24 //FF 11 F0 59 03 66 10 24 //45 12 F0 59 03 66 10 24 UInt64 processorUid = 0x0311F05903661024; var key = $"{(UInt16)((processorUid >> 48) ^ (processorUid >> 32)):X4}" + $"{(UInt32)((processorUid >> 16) ^ processorUid):X8}"; //F348F33F1342 _rfidComPort.Open(); //send the data to RFID SendDataViaRfid(GenesisTest); //SendDataViaRfid(ReadGenesisDevicePcbId); //uartPort.DataReceived += UartPort_DataReceived; //uartPort.Write(ReadGenesisDevicePcbId, 0, ReadGenesisDevicePcbId.Length); //SendDataViaRfid(WRiteGenesisPrivilegeLevel8); //SendDataViaRfid(WriteGenesisDevicePassword); _rfidComPort.Dispose(); Console.ReadLine(); } private static void UartPort_DataReceived(Object sender, SerialDataReceivedEventArgs e) { var sr = ((SerialPort)sender); var rxStringCommandchannel = new List(); sr.ReadTo("["); //ASCII value for 0x5B var length = (Byte)sr.ReadChar(); rxStringCommandchannel.Add(length); for (UInt16 i = 0; i < length - 1; i++) rxStringCommandchannel.Add((Byte)sr.ReadByte()); } public static void SendDataViaRfid(Byte[] data) { Logger.Value.Trace($"{Ident} sent: {BitConverter.ToString(data)}"); //this routine prepares the "UART" commands via RFID PrintMessage("UART Tx: ", data, DateTime.Now); Console.Write("\n"); //remind start time for communication var txStartTime = DateTime.Now; //send the data to RFID _rfidComPort.SendDataViaRfid(data); RfidRxState rfidState; //call state machine until ready do { //get RFID Tx and Rx buffer, these are being assembled after return from //the state machine routine PrepareAndDisplayRfidMessages(txStartTime); txStartTime = DateTime.Now; rfidState = _rfidComPort.RfidCommStateMachine(); } while (RfidRxState.CommunicationFinished != rfidState && RfidRxState.CommunicationFailed != rfidState); //get assembled answer from water meter var waterMeterRxData = _rfidComPort.GetDecodedDataFromRfid(); PrintMessage("UART Rx: ", waterMeterRxData, DateTime.Now); var commCounter = _rfidComPort.GetRfidComCounter(); Console.WriteLine(RfidRxState.CommunicationFinished == rfidState ? "\nRFID communication successful!" : "\nRFID COMMUNICATION FAILED!"); Console.WriteLine("RFID packets {0}", commCounter); Console.Write("\n"); Console.Write("\n"); } public static void PrepareAndDisplayRfidMessages(DateTime txDate) { var rfidRxBuffer = _rfidComPort.GetRfidRxBuffer(); var rfidTxBuffer = _rfidComPort.GetRfidTxBuffer(); const String rfidRxText = "RFID Rx: "; const String rfidTxText = "RFID Tx: "; PrintMessage(rfidTxText, rfidTxBuffer, txDate); PrintMessage(rfidRxText, rfidRxBuffer, DateTime.Now); Console.Write("\n"); } public static void PrintMessage(String text, Byte[] buffer, DateTime date) { var addSpace = true; Console.Write("{0:D2}:{1:D2}:{2:D3} " + text, date.Minute, date.Second, date.Millisecond); foreach (var t in buffer) { if (addSpace) { Console.Write("{0:X2} ", t); addSpace = false; } else { Console.Write("{0:X2}", t); addSpace = true; } } Console.Write("\n"); } } }