private void myTCP_TelegrammRecievedRecieve(byte[] telegramm, TcpClient clnt) { if (quittConf == null || !quittConf.AutomaticQuitt) { //Automaticly send a quitt Telegramm byte[] quittTel = new byte[telegramm.Length]; Array.Copy(telegramm, quittTel, telegramm.Length); foreach (var quittReplacmentByte in quittConf.QuittReplacmentBytes) { Array.Copy(Encoding.ASCII.GetBytes(quittReplacmentByte.Value), 0, quittTel, quittReplacmentByte.Position, quittReplacmentByte.Value.Length); } myTCP_rec.SendData(quittTel); //Neues telegramm nur wenn keine WDH aktiv! //byte[] sequNr = new byte[quittConf.LengthSequenceNumber]; //Array.Copy(telegramm, quittConf.SequenceNumberPosition, sequNr, 0, quittConf.LengthSequenceNumber); //if (oldSequenceNumber == null || !sequNr.ByteArrayCompare(oldSequenceNumber)) grdEmpfang.Rows.Add(new object[] { Encoding.ASCII.GetString(telegramm) }); //oldSequenceNumber = sequNr; } else { grdEmpfang.Rows.Add(new object[] { Encoding.ASCII.GetString(telegramm) }); } }
public void ReadValue(PLCTag tag) { var telHead = new byte[] { (byte)'S', (byte)'5', 16, 1, 3, 5, 3, 8, PLCConnectionID, (byte)tag.DatablockNumber, 0, 0, 0, 0, 0xff, 2 }; LibNoDave.libnodave.putBCD16at(telHead, 10, tag.ByteAddress); LibNoDave.libnodave.putBCD16at(telHead, 10, tag.ReadByteSize); tcp.SendData(telHead); }
private void myTCP_TelegrammRecievedSend(byte[] telegramm, TcpClient clnt) { dtaSendQuittTable.Rows.Insert(0, new object[] { getEncoding().GetString(telegramm) }); if (quittConf != null && quittConf.AutomaticQuitt && Properties.Settings.Default.UseOnlyOneConnection) { if (quittConf.QuittReplacmentBytes.Count > 0) { bool okQuittTel = false; foreach (TCPFunctions.QuittConfig.QuittText quittReplacmentByte in quittConf.QuittReplacmentBytes) { if ( getEncoding().GetString(telegramm, quittReplacmentByte.Position, quittReplacmentByte.Value.Length) != quittReplacmentByte.Value) { okQuittTel = true; } } if (okQuittTel) { byte[] quittTel = new byte[telegramm.Length]; Array.Copy(telegramm, quittTel, telegramm.Length); foreach (var quittReplacmentByte in quittConf.QuittReplacmentBytes) { Array.Copy(getEncoding().GetBytes(quittReplacmentByte.Value), 0, quittTel, quittReplacmentByte.Position, quittReplacmentByte.Value.Length); } if (Properties.Settings.Default.MaxLengthQuitt > 0) { byte[] quittTel2 = new byte[Properties.Settings.Default.MaxLengthQuitt]; Array.Copy(quittTel, quittTel2, quittTel2.Length); quittTel = quittTel2; } myTCP_send.SendData(quittTel); } } } }
protected override void OnStart(string[] args) { tcp1 = new TCPFunctionsAsync(null, IPAddress.Parse(Settings.Default.SourceIP), Settings.Default.SourcePort, Settings.Default.SourceActive); tcp2 = new TCPFunctionsAsync(null, IPAddress.Parse(Settings.Default.DestinationIP), Settings.Default.DestinationPort, Settings.Default.DestinationActive); tcp1.DataRecieved += (data, tcpClient) => tcp2.SendData(data); tcp2.DataRecieved += (data, tcpClient) => tcp1.SendData(data); tcp1.Start(); tcp2.Start(); }
private void myTCP_TelegrammRecievedRecieve(byte[] telegramm, TcpClient clnt) { if (quittConf != null && quittConf.AutomaticQuitt) { //Automaticly send a quitt Telegramm byte[] quittTel = new byte[telegramm.Length]; Array.Copy(telegramm, quittTel, telegramm.Length); foreach (var quittReplacmentByte in quittConf.QuittReplacmentBytes) { Array.Copy(getEncoding().GetBytes(quittReplacmentByte.Value), 0, quittTel, quittReplacmentByte.Position, quittReplacmentByte.Value.Length); } if (Properties.Settings.Default.MaxLengthQuitt > 0) { byte[] quittTel2 = new byte[Properties.Settings.Default.MaxLengthQuitt]; Array.Copy(quittTel, quittTel2, quittTel2.Length); quittTel = quittTel2; } myTCP_rec.SendData(quittTel); //Neues telegramm nur wenn keine WDH aktiv! //byte[] sequNr = new byte[quittConf.LengthSequenceNumber]; //Array.Copy(telegramm, quittConf.SequenceNumberPosition, sequNr, 0, quittConf.LengthSequenceNumber); //if (oldSequenceNumber == null || !sequNr.ByteArrayCompare(oldSequenceNumber)) grdEmpfang.Rows.Insert(0, new object[] { getEncoding().GetString(telegramm) }); //oldSequenceNumber = sequNr; } else { grdEmpfang.Rows.Insert(0, new object[] { getEncoding().GetString(telegramm) }); } }
public void ReadValue(PLCTag tag) { if (_connected) { var rqHeader = new RequestHeader(); rqHeader.system_id = new[] { 'S', '5' }; rqHeader.header_length = (byte)Marshal.SizeOf(typeof(RequestHeader)); rqHeader.opcode_id = 1; rqHeader.opcode_length = 3; rqHeader.opcode = (byte)OperationCode.Fetch; rqHeader.org = 3; rqHeader.org_length = 8; switch (tag.TagDataSource) { case MemoryArea.Datablock: case MemoryArea.InstanceDatablock: rqHeader.org_id = (byte)OrgTypes.DB; break; case MemoryArea.Inputs: rqHeader.org_id = (byte)OrgTypes.Inputs; break; case MemoryArea.Outputs: rqHeader.org_id = (byte)OrgTypes.Outputs; break; case MemoryArea.S5_DX: rqHeader.org_id = (byte)OrgTypes.DBx; break; case MemoryArea.Flags: rqHeader.org_id = (byte)OrgTypes.Flags; break; case MemoryArea.Counter: rqHeader.org_id = (byte)OrgTypes.Counters; break; case MemoryArea.Timer: rqHeader.org_id = (byte)OrgTypes.Timer; break; } rqHeader.dbnr = (byte)tag.DataBlockNumber; rqHeader.start_address1 = (byte)(((tag.ByteAddress / 2) & 0xff00) >> 8); rqHeader.start_address2 = (byte)(((tag.ByteAddress / 2) & 0x00ff)); var sz = tag.ReadByteSize; if (sz % 2 > 0) { sz++; } rqHeader.length1 = (byte)(((sz / 2) & 0xff00) >> 8); rqHeader.length2 = (byte)(((sz / 2) & 0x00ff)); rqHeader.req_empty = 0xff; rqHeader.req_empty_length = 2; var bytes = getBytes(rqHeader); _tcp.SendData(bytes); var header = new byte[Marshal.SizeOf(typeof(ResponseHeader))]; var readBytes = 0; while (readBytes < header.Length) { readBytes += _tcp.NetworkStream.Read(header, readBytes, header.Length - readBytes); } var response = fromBytes <ResponseHeader>(header); var data = new byte[sz]; readBytes = 0; while (readBytes < sz) { readBytes += _tcp.NetworkStream.Read(data, readBytes, sz - readBytes); } tag.ParseValueFromByteArray(data, tag.ByteAddress % 2 > 0 ? 1 : 0); } }
public void WriteValue(PLCTag tag) { if (_connectedWrite) { var rqHeader = new RequestHeader(); rqHeader.system_id = new[] { 'S', '5' }; rqHeader.header_length = (byte)Marshal.SizeOf(typeof(RequestHeader)); rqHeader.opcode_id = 1; rqHeader.opcode_length = 3; rqHeader.opcode = (byte)OperationCode.Write; rqHeader.org = 3; rqHeader.org_length = 8; switch (tag.TagDataSource) { case MemoryArea.Datablock: case MemoryArea.InstanceDatablock: rqHeader.org_id = (byte)OrgTypes.DB; break; case MemoryArea.Inputs: rqHeader.org_id = (byte)OrgTypes.Inputs; break; case MemoryArea.Outputs: rqHeader.org_id = (byte)OrgTypes.Outputs; break; case MemoryArea.S5_DX: rqHeader.org_id = (byte)OrgTypes.DBx; break; case MemoryArea.Flags: rqHeader.org_id = (byte)OrgTypes.Flags; break; case MemoryArea.Counter: rqHeader.org_id = (byte)OrgTypes.Counters; break; case MemoryArea.Timer: rqHeader.org_id = (byte)OrgTypes.Timer; break; } rqHeader.dbnr = (byte)tag.DataBlockNumber; rqHeader.start_address1 = (byte)(((tag.ByteAddress / 2) & 0xff00) >> 8); rqHeader.start_address2 = (byte)(((tag.ByteAddress / 2) & 0x00ff)); var sz = tag.ReadByteSize; if (sz % 2 > 0) { sz++; } rqHeader.length1 = (byte)(((sz / 2) & 0xff00) >> 8); rqHeader.length2 = (byte)(((sz / 2) & 0x00ff)); rqHeader.req_empty = 0xff; rqHeader.req_empty_length = 2; var bytes = getBytes(rqHeader); var writeByte = new byte[bytes.Length + sz]; Array.Copy(bytes, 0, writeByte, 0, bytes.Length); var putPos = bytes.Length; if (tag.ReadByteSize % 2 > 0) { putPos++; } if (tag.TagDataType == TagDataType.Bool) { if (object.Equals(tag.Controlvalue, true)) { writeByte[putPos] = (byte)(Math.Pow(2, (tag.BitAddress))); } else { writeByte[putPos] = 0; } } else { tag._putControlValueIntoBuffer(writeByte, putPos); } _tcpWrite.SendData(writeByte); var data = new byte[Marshal.SizeOf(typeof(ResponseHeader))]; var readBytes = 0; while (readBytes < data.Length) { readBytes += _tcpWrite.NetworkStream.Read(data, readBytes, data.Length - readBytes); } var response = fromBytes <ResponseHeader>(data); } }
private void SendTel(string tel) { if (Properties.Settings.Default.EscapeSpecialChars) { tel = unescape(tel); } sendedTele.Add(tel); var wrt = tel; var sendbytes = getEncoding().GetBytes(tel); if (Properties.Settings.Default.LogDataAsHexBytes) { wrt = bytesToHexString(sendbytes); } string add = ""; if (Properties.Settings.Default.ShowDate) { add += DateTime.Now.ToString(Properties.Settings.Default.DateTimeFormat); } if (Properties.Settings.Default.ShowRecievedLen) { if (!string.IsNullOrEmpty(add)) { add += " - "; } add += sendbytes.Length.ToString().PadLeft(4, '0') + " Bytes"; } if (!string.IsNullOrEmpty(add)) { add += ": "; } if (Properties.Settings.Default.LogHexAndAscii && Properties.Settings.Default.LogDataAsHexBytes) { var akTel = tel; if (Properties.Settings.Default.EscapeSpecialChars) { akTel = escape(akTel); } txtSended.Text = add + wrt + Environment.NewLine + Environment.NewLine + txtSended.Text; txtSended.Text = add + akTel + Environment.NewLine + txtSended.Text; } else { var akTel = wrt; if (Properties.Settings.Default.EscapeSpecialChars) { akTel = escape(akTel, true); } txtSended.Text = add + akTel + Environment.NewLine + txtSended.Text; } if (tcpFunc != null) { tcpFunc.SendData(sendbytes); } if (!String.IsNullOrEmpty(Properties.Settings.Default.LogFile)) { try { lblLogFile.Text = this.GetTextFilename(); StreamWriter myFile = new StreamWriter(lblLogFile.Text, true); if (Properties.Settings.Default.LogHexAndAscii && Properties.Settings.Default.LogDataAsHexBytes) { var akTel = tel; if (Properties.Settings.Default.EscapeSpecialChars) { akTel = escape(akTel); } myFile.Write("Send : " + add + akTel + Environment.NewLine); myFile.Write("Bytes: " + add + wrt + Environment.NewLine); } else { var akTel = tel; if (Properties.Settings.Default.EscapeSpecialChars) { akTel = escape(akTel); } myFile.Write("Send : " + add + akTel + Environment.NewLine); } myFile.Close(); } catch (Exception ex) { lblException.Text = ex.Message; } } }
private void cmdSend_Click(object sender, EventArgs e) { lblStatus.Text = ""; try { Prüfen(); } catch (Exception ex) { lblStatus.Text = ex.Message; return; } string SendeString = SendeStringZusammenbauen(); byte[] bytes = Encoding.ASCII.GetBytes(SendeString); byte[] lnr = LaufnummerErzeugen(); try { Array.Copy(lnr, 0, bytes, Convert.ToInt32(Settings.Default.SequenceNumberPosition), lnr.Length); } catch (Exception) { } try { if (myTCP_send != null) { myTCP_send.SendData(bytes); dtaSendSendTable.Rows.Add(new object[] { Encoding.ASCII.GetString(bytes) }); } else { lblStatus.Text = "Senden nicht erfolgt, da nicht verbunden!"; } } catch (Exception ex) { lblStatus.Text = ex.Message; } Settings.Default.Laufnummer++; var value = Encoding.ASCII.GetString(bytes); if (value != null) { string sValue = (string)value; int pos = 0; foreach (DataGridViewRow row in dtaSendTabelle.Rows) { var tmp = row.Cells["Laenge"].Value; if (tmp != null) { int len = Convert.ToInt32(tmp); if (pos + len <= sValue.Length) { row.Cells["Wert"].Value = sValue.Substring(pos, len); } pos += len; } } } }