unit plc;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Snap7, StdCtrls ;
function RealRead(B1,B2,B3,B4:byte):Single ;
Procedure SymplyGet;
var
MyDB32 : packed array[0..1134] of byte; // generic buffer
MyClient : TS7Client;
implementation
uses main;
function RealRead(B1,B2,B3,B4:byte):Single ;
Var
FTransportArray: Array[0..3] of Byte;
FKopieSingle: Single;
begin
FTransportArray[3]:= B1;
FTransportArray[2]:= B2;
FTransportArray[1]:= B3;
FTransportArray[0]:= B4;
// Byte array decoding to single
Result := PSingle(@FTransportArray)^;
end;
Procedure SymplyGet;
Begin
MyClient:=TS7Client.Create;
MyClient.ConnectTo('192.168.0.1',0,1);
MyClient.DBRead(1488, // DB Number
0, // Start from
4, // How many
@MyDB32); // Target address
MyClient.Free;
End;
end.
Sonra C# ********************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Snap7;
namespace snap7_example
{
public partial class frmMain : Form
{
S7Client MyClient;
public frmMain()
{
InitializeComponent();
}
// 1 byte lık alandan 1 biti bulan function
// Yapı GetBit( Byte Değişken , index)
//Not İndex : 0...7 arası rakam olmalıdır..8 olamaz../snap7 ye gore
public bool GetBit(byte byt, int index)
{
if (index < 0 || index > 7)
throw new ArgumentOutOfRangeException();
byte bitMask = (byte)(1 << index);
byte masked = (byte)(byt & bitMask);
return masked != 0;
}
private void button1_Click(object sender, EventArgs e)
{
MyClient = new S7Client();
MyClient.ConnectTo("192.168.0.1", 0, 1);
if (MyClient.Connected())
{
//Verileri cekme
byte[] MyDB32 = new byte[6];
MyClient.DBRead(1, 0, 6, MyDB32);
MyClient.Disconnect();
MyClient = null;
//Real sayı okuma
byte[] b = new byte[4];
b[0] = MyDB32[5]; b[1] = MyDB32[4]; b[2] = MyDB32[3]; b[3] = MyDB32[2];
float myFloat = System.BitConverter.ToSingle(b, 0);
label1.Text = Convert.ToString(myFloat);
//Bit okuma
label2.Text = Convert.ToString(GetBit(MyDB32[0], 2));