using System.IO.Ports; using Sensus; using Sensus.CommunicationDevices; using Sensus.Protocols.FlexNet.Serial; using Sensus.Protocols.FlexNet.Serial.FNv2; namespace NfcC7_DLL.NfcHanler.Utils { /// /// Provides a transaction manager for a CommandLink /// public class SerialManager: ISerialConnection, IDisposable { public FlexNetSerialConnection FactoryUart = null; public string ComPortString { get; private set; } /// /// Sets up Serial for communication /// /// Serial COM port public SerialManager(string serialcomPort) { // Initialize Serial Port SerialPort factoryUart = new SerialPort(serialcomPort, 115200, Parity.None, 8, StopBits.One); FactoryUart = new FlexNetSerialConnection(serialcomPort); FactoryUart.SetBaudRate(115200); ComPortString = serialcomPort; } public void Open() { // Open Factory UART if (!FactoryUart.IsConnected) { FactoryUart.Connect(null); } } public void Close() { FactoryUart.Disconnect(); } public bool IsConnected { get { return FactoryUart.IsConnected; } } object objectLock = new Object(); public event EventHandler> MessageReceived { add { lock (objectLock) { FactoryUart.MessageReceived += value; } } remove { lock (objectLock) { FactoryUart.MessageReceived -= value; } } } public event EventHandler Disconnected { add { lock (objectLock) { FactoryUart.Disconnected += value; } } remove { lock (objectLock) { FactoryUart.Disconnected -= value; } } } public ClassicSerialMessageFrame SendFlexNetSerial(ISerialCommandFrame cmd, bool responseExpected, UInt32 timeoutMs) { ClassicSerialMessageFrame response = null; if(responseExpected) { try { response = FactoryUart.Send(cmd, TimeSpan.FromMilliseconds(timeoutMs)) as ClassicSerialMessageFrame; } catch { Log.Write("No response before timeout\n"); } } else { FactoryUart.Send(cmd); } return response; } public NA2WSerialFrame SendNa2wSerial(ISerialCommandFrame cmdFrame, bool responseExpected, UInt32 timeoutMs) { NA2WSerialFrame response = null; if (responseExpected) { try { response = FactoryUart.Send(cmdFrame, TimeSpan.FromMilliseconds(timeoutMs)) as NA2WSerialFrame; } catch (TimeoutException e) { Log.Write("No response before timeout\n"); } } else { FactoryUart.Send(cmdFrame); } return response; } public NA2WSerialFrame SendNa2wSerial(ISerialCommandFrame cmdFrame, bool responseExpected) { return SendNa2wSerial(cmdFrame, responseExpected, 5000); } public NA2WSerialFrame SendNa2wSerial(ISerialCommandFrame cmdFrame, byte responseCommandCode) { ISerialMessageFrame response = null; using (AutoResetEvent e = new AutoResetEvent(false)) { EventHandler> ackChecker = (mrs, mre) => { if (mre.Frame.CommandCode == responseCommandCode) { response = mre.Frame; e.Set(); } }; bool foundResponse = false; try { FactoryUart.MessageReceived += ackChecker; FactoryUart.Send(cmdFrame); foundResponse = e.WaitOne(TimeSpan.FromSeconds(8)); } finally { FactoryUart.MessageReceived -= ackChecker; } if (!foundResponse) { Log.Write("No response before timeout\n"); } } if ((response != null) && (response is NA2WSerialFrame)) { return ((NA2WSerialFrame) response); } else { return null; } } bool disposed = false; public void Dispose() { if (disposed) return; Close(); disposed = true; } } }