Write Boolean
32/64 bit multi-platform Ethernet S7 PLC communication suite
Brought to you by:
davenardella
After searching a couple of hours I figured out that it is nececcery to multiply the position of the bits with 8 to get shifted thorugh the DB while writing the values - else it is always writing to Byte 0.
I putted on the Sharp7 lib a new function for myself to not to do this work in my business classes (and forget after a while).
/// <summary>
/// WriteBit: writes a boolean to the PLC
/// </summary>
/// <param name="Area"></param>
/// <param name="DBNumber"></param>
/// <param name="Position"></param>
/// <param name="Bit"></param>
/// <param name="Amount"></param>
/// <param name="WordLen"></param>
/// <param name="Buffer"></param>
/// <returns></returns>
public int WriteBit(int Area, int DBNumber, int Position, int Bit, int Amount, int WordLen, byte[] Buffer)
{
int BytesWritten = 0;
int Start = Position * 8 + Bit;
return WriteArea(Area, DBNumber, Start, Amount, WordLen, Buffer, ref BytesWritten);
}
And this is my call in the PLCService
private int WriteBit(int db, int pos, int bit, bool value)
{
lock (_locker)
{
Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "\t WriteBit: " + "DB" + db + ".dbx " + pos + "." + bit + "\t" + value);
var buffer = new byte[1];
S7.SetBitAt(ref buffer, 0, bit, value);
return _client.WriteBit(S7Consts.S7AreaDB, db, pos, bit, buffer.Length, S7Consts.S7WLBit, buffer);
}
}
"After searching a couple of hours I figured out that it is nececcery to multiply the position of the bits with 8 to get shifted thorugh the DB while writing the values"
You could have saved a couple of hours if you had read pag.105 of the reference manual :D :D
Anyway thanks for the improvement ;) ;) in that way it's more confortable.