void myConnection_PDURecieved(Pdu pdu) { byte[] pdu_bt = pdu.ToBytes(); byte[] data = new byte[pdu_bt.Length + 3]; //+3 Bytes (COTP) Array.Copy(pdu_bt, 0, data, 3, pdu_bt.Length); //COTP data[0] = 2; data[1] = 0xf0; data[2] = 0x80; listBox1.Invoke( (MethodInvoker) delegate() { listBox1.Items.Add("Send (TCP): " + ByteExtensions.ToHexString(data)); }); myTCPServer.SendData(data); }
private Pdu ExchangePdu(Pdu myPdu) { ushort pduNrInt; byte[] ret; lock (lockpdu) { pduNr++; if (pduNr >= ushort.MaxValue) pduNr = 1; pduNrInt = pduNr; myPdu.header.number = pduNr; //int res; //*(dc->msgOut + 6) = 0x80; //*(dc->msgOut + 5) = 0xf0; //*(dc->msgOut + 4) = 0x02; //_daveSendISOPacket(dc, 3 + p->hlen + p->plen + p->dlen); byte[] pdu_b = myPdu.ToBytes(); byte[] _message = new byte[pdu_b.Length + 3]; _message[0] = 0x02; _message[1] = 0xf0; _message[2] = 0x80; Array.Copy(pdu_b, 0, _message, 3, pdu_b.Length); sendISOPacket(_message); ret = ReceiveData(); if (ret != null && ret.Length == 7) { //if (daveDebug & daveDebugByte) // LOG1("CPU sends funny 7 byte packets.\n"); ret = ReceiveData(); } if (ret == null) return new Pdu(); //daveResTimeout; /*if (daveDebug & daveDebugExchange) { LOG3("%s _daveExchangeTCP res from read %d\n", dc->iface->name, res); }*/ if (ret.Length <= daveConst.ISOTCPminPacketLength) return new Pdu(); //daveResShortPacket; //Interface.ExchangePdu(myPdu, this); } //Todo: maybe implement a Timeout here! //while (RecievedPdus[pduNrInt] == null) //{ } byte[] res = new byte[ret.Length - 7]; Array.Copy(ret, 7, res, 0, ret.Length - 7); Pdu retVal = new Pdu(res); //RecievedPdus.Remove(pduNrInt); return retVal; }
public void SendPdu(Pdu pdu, Connection connection) { if (!Disposed) { byte[] pdubytes = pdu.ToBytes(); S7OexchangeBlock fdr = new S7OexchangeBlock(); fdr.user = (ushort) connection.ConnectionNumber; fdr.subsystem = 64; fdr.opcode = 6; fdr.response = 255; fdr.fill_length_1 = (byte) pdubytes.Length; fdr.seg_length_1 = (byte) pdubytes.Length; fdr.application_block_opcode = connection.application_block_opcode; //if (!connection.ConnectionConfig.ConnectionToEthernet) fdr.application_block_subsystem = connection.application_block_subsystem; fdr.headerlength = 80; //Length of the Header (always 80) (but the 4 first unkown bytes are not count) fdr.rb_type = 2; //rb_type is always 2 fdr.offset_1 = 80; //Offset of the Begin of userdata (but the 4 first unkown bytes are not count) fdr.user_data_1 = new byte[260]; Array.Copy(pdubytes, fdr.user_data_1, pdubytes.Length); int len = Marshal.SizeOf(fdr); byte[] buffer = new byte[len]; IntPtr ptr = Marshal.AllocHGlobal(len); Marshal.StructureToPtr(fdr, ptr, true); Marshal.Copy(ptr, buffer, 0, len); Marshal.FreeHGlobal(ptr); int ret = SCP_send(_connectionHandle, (ushort)(fdr.seg_length_1 + fdr.headerlength), buffer); if (ret < 0) throw new S7OnlineException(SCP_get_errno()); } else { throw new ObjectDisposedException(this.ToString()); } }