public static byte GetDataTypeSize(ElementaryDataType dataType) { switch (dataType) { case ElementaryDataType.SINT: return(1); case ElementaryDataType.INT: return(2); case ElementaryDataType.DINT: return(4); case ElementaryDataType.LINT: return(8); case ElementaryDataType.USINT: return(1); case ElementaryDataType.UINT: return(2); case ElementaryDataType.UDINT: return(4); case ElementaryDataType.ULINT: return(8); case ElementaryDataType.REAL: return(4); case ElementaryDataType.STRING: return(1); case ElementaryDataType.BYTE: return(1); default: throw new Exception(string.Format("Data type '{0}' not implemented yet", dataType)); } }
public IncomingTag() { DeviceType = TagDeviceType.A2700M; Channel = Channel.Memory; ChannelId = 0; DataType = ElementaryDataType.Bool; }
private void OnConnRecReceivedDataMsg(string remoteEndPoint, string symbol, ElementaryDataType dataType, byte[] data) { try { OnConnRecReceiveMsgData?.Invoke(remoteEndPoint, symbol, dataType, data); } catch (Exception e) { Trace(e); } }
public void SendData(string symbol, ElementaryDataType dataType, byte[] dataBytes, byte linkaddress = 0) { lock (m_SndMutex) if (m_SocketClient.Connected) { if (m_ClientConnStates != ClientConnStates.SendReceive) { throw new Exception("Waiting registration..."); } m_SendResetEvent.Reset(); m_SenderContext = DateTime.Now.Ticks; m_Symbol = symbol; m_LinkAddress = linkaddress; m_DataToSend = dataBytes; m_DataTypeToSend = dataType; m_SendStatusResult = null; m_ThreadResetEvent.Set(); try { if (!m_SendResetEvent.WaitOne(SEND_TIME_OUT)) { m_SocketClient.TryToReconnect(); throw new Exception("Send time-out! The connection will be closed."); } else { if (m_SendStatusResult.HasValue) { if (m_SendStatusResult.Value != 0) { throw new Exception(string.Format("Send error code: {0}-{1}", m_SendStatusResult.Value, (CipStatusCode)m_SendStatusResult.Value)); } } else { m_SocketClient.TryToReconnect(); throw new Exception("Send failed! The connection will be closed."); } } } finally { m_DataToSend = null; } } else { throw new Exception("Could not send data. Send connection not established yet."); } }
// Function to receive data from incoming connections // param "remoteEndPoint" -> IP/port from the remote side (used to distingue multiples incoming connections) // param "symbol" -> the tag name // param "dataType" -> data type (SINT for example) // param "data" -> data private static void OnConnRecReceiveMsgData(string remoteEndPoint, string symbol, ElementaryDataType dataType, byte[] data) { // Log string strData = string.Join("|", data.Select(a => a.ToString())); Cip_OnEventTrace(Traceable.EventType.Info, string.Format("Data received ({0} bytes). Tag: {1} Type: {2} Data: [{3}]", data.Length, symbol, dataType, strData)); }
private void MessageFactory(CommandEtherNetIPHeader header, byte[] bodyBytes) { try { Trace(EventType.Full, string.Format("{0} - Receive msg. '{1}'", LOG_TAG, header.Command)); long headerSize = Marshal.SizeOf(typeof(CommandEtherNetIPHeader)); switch (header.Command) { case EncapsulationCommands.ListServices: { if (bodyBytes.Length == 0) { MsgListServiceReply msg = new MsgListServiceReply(); msg.CommandSpecificDataListServices = new CommandSpecificDataListServices(); msg.CommandSpecificDataListServices.ItemCount = 1; msg.CommandSpecificDataListServices.Items = new CommandSpecificDataListServicesItem[1]; msg.CommandSpecificDataListServices.Items[0] = new CommandSpecificDataListServicesItem { TypeCode = CommonPacketItemID.ListServicesResponse, Version = 1, CapabilityFlags = Convert.ToUInt16("100100000", 2), ServiceName = Encoding.ASCII.GetBytes("Communications\0\0") }; header.Length = msg.SizeOf(); msg.CommandSpecificDataListServices.Items[0].Length = (ushort)(msg.CommandSpecificDataListServices.Items[0].SizeOf() - 2 - 2); SendMessage(header, msg); } break; } case EncapsulationCommands.RegisterSession: { int pointer = 0; CommandSpecificDataRegisterSession cmdSpecData = (CommandSpecificDataRegisterSession)CommandSpecificDataRegisterSession.Deserialize( typeof(CommandSpecificDataRegisterSession), bodyBytes, ref pointer); MsgRegisterSessionReply msg = new MsgRegisterSessionReply(); // TODO: check the protocol version to accept the registration msg.CommandSpecificDataRegisterSession = cmdSpecData; m_SessionHandle = (uint)((DateTime.Now.Ticks / 10) & 0xFFFFFFFF); header.SessionHandle = m_SessionHandle; header.Length = msg.SizeOf(); SendMessage(header, msg); Trace(EventType.Info, string.Format("{0} - Registration session number: {1}", LOG_TAG, header.SessionHandle)); break; } case EncapsulationCommands.SendRRData: { if (header.SessionHandle != m_SessionHandle) { throw new Exception(string.Format("Received invalid session handle (unregistred) in SendRRData message: ", header.SessionHandle)); } int pointer = 0; MsgUnconnectedSendRequest unconnSndReq = (MsgUnconnectedSendRequest)MsgUnconnectedSendRequest.Deserialize( typeof(MsgUnconnectedSendRequest), bodyBytes, ref pointer); string symbol = Encoding.ASCII.GetString(((DataPathSegmentANSISymb)unconnSndReq.CIPConnectionManagerUnconnSnd.CommonIndustrialProtocolRequest.PathSegmentList[0]).ANSISymbol); ElementaryDataType dataType = unconnSndReq.CIPConnectionManagerUnconnSnd.CIPClassGeneric.DataType; OnReceiveData?.Invoke(m_ScktConn.RemoteEndPoint, symbol.Replace("\0", ""), dataType, unconnSndReq.CIPConnectionManagerUnconnSnd.CIPClassGeneric.CIPClassGenericCmdSpecificData); MsgUnconnectedSendReply msg = new MsgUnconnectedSendReply(); msg.CommandSpecificDataSendRRData = unconnSndReq.CommandSpecificDataSendRRData; CommandSpecificDataSendRRDataItem item = msg.CommandSpecificDataSendRRData.List.First( a => a.TypeID == CommonPacketItemID.UnconnectedMessage); msg.CommonIndustrialProtocolReply = new CommonIndustrialProtocolReply { Service = 0xcd, Reserved = 0x0, GeneralStatus = 0x0, // <- success AdditionalStatusSize = 0x0, AdditionalStatus = new ushort[0] }; item.Length = msg.CommonIndustrialProtocolReply.SizeOf(); header.Length = msg.SizeOf(); SendMessage(header, msg); break; } case EncapsulationCommands.UnRegisterSession: { break; } default: { Trace(EventType.Error, string.Format("{0} - Command {1} not implemented", LOG_TAG, header.Command)); break; } } } catch (Exception e) { Trace(e); } }