public CommPacketHeader Deserialize(byte[] buffer) { //byte swap network to host //this will only work with ushort data fields, 2 bytes for (int i = 0; i < buffer.Length; i += 2) { byte byte1 = buffer[i]; byte byte2 = buffer[i + 1]; buffer[i] = byte2; buffer[i + 1] = byte1; } GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); CommPacketHeader header = (CommPacketHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(CommPacketHeader)); handle.Free(); return(header); }
public static string Dump(CommPacketHeader hdrDPK, bool /*labelled?*/ isLbl = false) { // There's supposed to be a way to do this via Reflection ... oh well.. string /*text (returned)*/ txt = (isLbl? "CommPacketHeader" : string.Empty) + "{"; txt += (isLbl? "SyncID " : string.Empty) + hdrDPK.SyncID.ToString(); txt += "," + (isLbl ? "MessageID " : string.Empty) + hdrDPK.MessageID.ToString(); txt += "..." + (isLbl ? "ConfigurationMode " : string.Empty) + hdrDPK.ConfigurationMode.ToString(); txt += "," + (isLbl ? "DeviceState " : string.Empty) + hdrDPK.DeviceState.ToString(); txt += "," + (isLbl ? "XRayDataState " : string.Empty) + hdrDPK.XRayDataState.ToString(); txt += "," + (isLbl ? "DataTransferMode " : string.Empty) + hdrDPK.DataTransferMode.ToString(); txt += "," + (isLbl ? "NVMWriteStatus " : string.Empty) + hdrDPK.NVMWriteStatus.ToString(); txt += "," + (isLbl ? "PulseCount " : string.Empty) + hdrDPK.PulseCount.ToString(); txt += "..." + (isLbl ? "NcbId " : string.Empty) + hdrDPK.NcbId.ToString(); txt += "," + (isLbl ? "PulsePeriod " : string.Empty) + hdrDPK.PulsePeriod.ToString(); txt += "," + (isLbl ? "NumOfChannels " : string.Empty) + hdrDPK.NumOfChannels.ToString(); txt += "..." + (isLbl ? "NumOfReferenceDetectors " : string.Empty) + hdrDPK.NumOfReferenceDetectors.ToString(); txt += "..." + (isLbl ? "PulseDelayPeriod " : string.Empty) + hdrDPK.PulseDelayPeriod.ToString(); txt += "," + (isLbl ? "SignOfLifeInterval " : string.Empty) + hdrDPK.SignOfLifeInterval.ToString(); txt += "..."; txt += "}"; return(txt); }
public static string Dump(CommPacketHeader hdrDPK, bool /*labelled?*/ isLbl = false) { // There's supposed to be a way to do this via Reflection ... oh well.. string /*text (returned)*/ txt = (isLbl? "CommPacketHeader" : string.Empty) + "{"; txt += (isLbl? "SyncID " : string.Empty) + hdrDPK.SyncID.ToString(); txt += "," + (isLbl ? "MessageID " : string.Empty) + hdrDPK.MessageID.ToString(); txt += "..." + (isLbl ? "ConfigurationMode " : string.Empty) + hdrDPK.ConfigurationMode.ToString(); txt += "," + (isLbl ? "DeviceState " : string.Empty) + hdrDPK.DeviceState.ToString(); txt += "," + (isLbl ? "XRayDataState " : string.Empty) + hdrDPK.XRayDataState.ToString(); txt += "," + (isLbl ? "DataTransferMode " : string.Empty) + hdrDPK.DataTransferMode.ToString(); txt += "," + (isLbl ? "NVMWriteStatus " : string.Empty) + hdrDPK.NVMWriteStatus.ToString(); txt += "," + (isLbl ? "PulseCount " : string.Empty) + hdrDPK.PulseCount.ToString(); txt += "..." + (isLbl ? "NcbId " : string.Empty) + hdrDPK.NcbId.ToString(); txt += "," + (isLbl ? "PulsePeriod " : string.Empty) + hdrDPK.PulsePeriod.ToString(); txt += "," + (isLbl ? "NumOfChannels " : string.Empty) + hdrDPK.NumOfChannels.ToString(); txt += "..." + (isLbl ? "NumOfReferenceDetectors " : string.Empty) + hdrDPK.NumOfReferenceDetectors.ToString(); txt += "..." + (isLbl ? "PulseDelayPeriod " : string.Empty) + hdrDPK.PulseDelayPeriod.ToString(); txt += "," + (isLbl ? "SignOfLifeInterval " : string.Empty) + hdrDPK.SignOfLifeInterval.ToString(); txt += "..."; txt += "}"; return txt; }
public xrayDataState GetXRayDataState(int ncbIndex = 0) { string traceText = MethodBase.GetCurrentMethod().Name + "(" + ncbIndex.ToString() + ")"; xrayDataState returnedXrayState = xrayDataState.Ready; try { CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_XRAY_DATA_STATE_QUERY; CommPacketHeader? reply = CommandSend(command, messageType.CX_XRAY_DATA_STATE_ACK, ncbIndex); if (/*no response?*/ reply == null) throw new Exception(traceText + ": no/null response"); returnedXrayState = reply.Value.XRayDataState; } catch { throw; } traceText += " returns " + returnedXrayState.ToString(); //if tracing ever done return returnedXrayState; }
public deviceState GetDeviceState(out ushort sensorCount, out ushort referenceCount, int ncbIndex = 0) { referenceCount = sensorCount = 0; deviceState returnedDeviceState = deviceState.Configuration; CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_DEVICE_STATE_QUERY; CommPacketHeader? reply = CommandSend(command, messageType.CX_DEVICE_STATE_ACK, ncbIndex); if (/*no response?*/ reply == null) throw new Exception("no/null response"); returnedDeviceState = reply.Value.DeviceState; referenceCount = reply.Value.NumOfReferenceDetectors; sensorCount = reply.Value.NumOfChannels; return returnedDeviceState; }
public int GetIdentification(int ncbIndex = 0) { int returnedIdentity = 0; CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_IDENTFY_QUERY; CommPacketHeader? reply = CommandSend(command, messageType.CX_IDENTFY_ACK, ncbIndex); if (/*no response?*/ reply == null) throw new Exception("no/null response"); returnedIdentity = reply.Value.NcbId; return returnedIdentity; }
private CommPacketHeader? CommandSend(CommPacketHeader command, messageType expectedResponse, int ncbIndex = 0) { CommPacketHeader? reply = null; try { if (/*run?*/ !_commandMonitorEnd.WaitOne(_commandMargin.Remaining)) lock (_commandSendLock) if (/*run?*/ !_commandMonitorEnd.WaitOne(0)) { Debug.Assert((ncbIndex >= 0) && (ncbIndex < NCBCount)); if (/*not connected?*/ CommandStream(ncbIndex) == null) throw new Exception("not connected to " + NCBName(ncbIndex)); command.SyncID = 0xA55A55AA; byte[] buffer = command.Serialize(); CommandStream(ncbIndex).WriteTimeout = TimeTENTH; Debug.Assert(CommandStream(ncbIndex).CanWrite); CommandStream(ncbIndex).Write(buffer, 0, buffer.Length); CommandStream(ncbIndex).Flush(); _commandMargin.Reset(); buffer = new byte[Marshal.SizeOf(typeof(CommPacketHeader))]; for (int retry = 0; /*OK?*/ (retry < CommandSendReads); reply = null, retry++) if (/*run?*/ !_commandMonitorEnd.WaitOne(retry * TimeTENTH)) { // Read blocks/waits up through // TcpClient.GetStream().ReadTimeout; if the // client is disposed, the read faults. CommandStream(ncbIndex).ReadTimeout = TimeTENTH; Debug.Assert(CommandStream(ncbIndex).CanRead); if (/*read OK?*/ CommandStream(ncbIndex).Read(buffer, 0, buffer.Length) >= 1) { reply = command.Deserialize(buffer); if (/*OK?*/ reply != null) if (/*OK?*/ reply.HasValue) if (reply.Value.MessageID == expectedResponse) break; else if (reply.Value.MessageID == messageType.CX_SIGNOFLIFE_NORMAL) retry--; /*don't count sign of life responses as tries*/ } } } } catch (Exception ex) { CommandConnectionDispose(); reply = null; throw ex; } return reply; }
public void GetConfigParameters(out ushort pulseDelay, out ushort signOfLife) { pulseDelay = (ushort)PulseDelay; Debug.Assert(pulseDelay <= ushort.MaxValue); signOfLife = (ushort)SignOfLife; Debug.Assert((signOfLife >= 1) && (signOfLife <= ushort.MaxValue)); string traceText = MethodBase.GetCurrentMethod().Name + "(" + pulseDelay.ToString() + " * 20ns, " + signOfLife.ToString() + "cs)"; try { CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_CONFIG_PARAMETERS_QUERY; CommPacketHeader? reply = CommandSend(command, messageType.CX_CONFIG_PARAMETERS_ACK); if (/*no response?*/ reply == null) throw new Exception(traceText + ": no/null response"); pulseDelay = reply.Value.PulseDelayPeriod; signOfLife = reply.Value.SignOfLifeInterval; } catch { throw; } }
public void SetDataTransferMode(dataTransferMode mode) { string traceText = MethodBase.GetCurrentMethod().Name + "(" + mode.ToString() + ")"; for (int /*NCB index*/ ncb = 0; ncb < NCBCount; ncb++) { try { CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_XRAY_DATA_STATE_NORMAL_WITH_ACK; command.DataTransferMode = mode; CommPacketHeader? reply = CommandSend(command, messageType.CX_XRAY_DATA_STATE_ACK, ncb); if (/*no response?*/ reply == null) throw new Exception(traceText + " " + NCBName(ncb) + ": no/null response"); if (reply.Value.DataTransferMode != mode) throw new Exception(traceText + " " + NCBName(ncb) + ": unexpected response (" + reply.Value.DataTransferMode.ToString() + ")"); } catch { throw; } } }
public void SetConfigParameterToNVM() { for (int /*NCB index*/ ix = 0; ix < NCBCount; ix++) { try { CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_CONFIG_PARAMETERS_WRITE_NORMAL_WITH_ACK; CommPacketHeader? reply = CommandSend(command, messageType.CX_CONFIG_PARAMETERS_WRITE_ACK); if (/*no response?*/ reply == null) throw new Exception("no/null response"); if ((nvmWriteStatus)reply.Value.NVMWriteStatus != nvmWriteStatus.Success) throw new Exception("failed- response.NVMWriteStatus is " + ((nvmWriteStatus)reply.Value.NVMWriteStatus).ToString()); } catch { throw; } } }
private void SetConfigurationParameters(ushort pulseDelay, ushort signOfLife, int ncbIndex) { string traceText = MethodBase.GetCurrentMethod().Name + "(" + pulseDelay.ToString() + ", " + signOfLife.ToString() + ", " + ncbIndex.ToString() + ")"; Debug.Assert(pulseDelay <= ushort.MaxValue); Debug.Assert(signOfLife <= ushort.MaxValue); Debug.Assert((ncbIndex >= 0) && (ncbIndex < NCBCount)); try { CommPacketHeader command = new CommPacketHeader(); command.PulseDelayPeriod = pulseDelay; command.SignOfLifeInterval = signOfLife; command.MessageID = messageType.CX_CONFIG_PARAMETERS_NORMAL_WITH_ACK; command.ReservedB6toB7 = 0x0000; command.ReservedB8toB9 = 0x0000; command.ReservedB10toB11 = 0x0000; command.ReservedB12toB13 = 0x0000; command.ReservedB18toB19 = 0x0000; command.ReservedB20toB31 = new byte[12]; CommPacketHeader? reply = CommandSend(command, messageType.CX_CONFIG_PARAMETERS_ACK, ncbIndex); if (/*no response?*/ reply == null) throw new Exception(traceText + ": no/null response"); if (reply.Value.PulseDelayPeriod != pulseDelay) throw new Exception(traceText + ": unexpected pulse delay response of " + reply.Value.PulseDelayPeriod.ToString()); if (reply.Value.SignOfLifeInterval != signOfLife) throw new Exception(traceText + ": unexpected sign of life response of " + reply.Value.SignOfLifeInterval.ToString()); } catch { throw; } }
private void SetConfigurationMode(configurationMode mode, int ncbIndex) { Debug.Assert(Enum.IsDefined(typeof(configurationMode), mode)); Debug.Assert((ncbIndex >= 0) && (ncbIndex < NCBCount)); string traceText = MethodBase.GetCurrentMethod().Name + "(" + mode.ToString() + ", " + ncbIndex.ToString() + ")"; try { CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_DEVICE_CONFIG_NORMAL_WITH_ACK; command.ConfigurationMode = mode; CommPacketHeader? reply = CommandSend(command, messageType.CX_DEVICE_CONFIG_ACK, ncbIndex); if (/*no response?*/ reply == null) throw new Exception(traceText + ": no/null response"); if (reply.Value.ConfigurationMode != mode) throw new Exception(traceText + ": unexpected response (" + reply.Value.ConfigurationMode.ToString() + ")"); } catch { throw; } }
public void ResetLineCount() { string traceText = MethodBase.GetCurrentMethod().Name; if (/*multi NCB?*/ NCBCount >= 2) { Exception /*exception*/ exc = null; for (int ix = 0; /*run?*/ !_dataGatherEnd.WaitOne(0) && (ix < NCBCount); ix++) { try { CommPacketHeader command = new CommPacketHeader(); command.MessageID = messageType.CX_RESET_LINE_COUNT_WITH_ACK; CommPacketHeader? reply = CommandSend(command, messageType.CX_RESET_LINE_COUNT_ACK, ix); if (/*no response?*/ reply == null) throw new Exception(traceText + ": no/null response"); } catch (Exception ex) { if (/*first anomaly?*/ exc == null) exc = ex; } } if (/*anomaly?*/ exc != null) throw exc; Logger.LogInfo(traceText + ": all NCBs' line identities reset"); } }