fix write x IO val/force

This commit is contained in:
Samuele E. Locatelli
2021-02-05 14:00:07 +01:00
parent 897dd274c7
commit 1c05c1eddf
2 changed files with 54 additions and 8 deletions
+41 -8
View File
@@ -3910,18 +3910,36 @@ namespace CMS_CORE_Library.S7Net
public override CmsError PLC_W_IO_DO_Val(Dictionary<int, bool> newValues)
{
CmsError libraryError = NO_ERROR;
List<ushort> readValuesDO = new List<ushort>();
List<ushort> rwValuesDO = new List<ushort>();
// leggo da PLC valori DO...
libraryError = MEM_RWWordList(R, 0, IO_DO_VAL_FOR.MemType, IO_DO_VAL_FOR.Address, IO_DO_VAL_FOR.SubAddress, IO_DO_VAL_FOR.Size, ref readValuesDO);
libraryError = MEM_RWWordList(R, 0, IO_DO_VAL_FOR.MemType, IO_DO_VAL_FOR.Address, IO_DO_VAL_FOR.SubAddress, IO_DO_VAL_FOR.Size, ref rwValuesDO);
if (libraryError.IsError())
return libraryError;
// aggiorno valori ricevuti
int bank = 0;
int pos = 0;
int size = 16;
// imposto i valori NUOVI... cerco il BANK da 16 bit...
foreach (var item in newValues)
{
bank = item.Key / size;
pos = item.Key % size;
// aggiorno
if (item.Value)
{
rwValuesDO[bank] = (ushort)(((int)rwValuesDO[bank]).SetBitOne(pos));
}
else
{
rwValuesDO[bank] = (ushort)(((int)rwValuesDO[bank]).SetBitZero(pos));
}
}
// scrivo valori
// scrivo forced
// scrivo!
libraryError = MEM_RWWordList(W, 0, IO_DO_VAL_FOR.MemType, IO_DO_VAL_FOR.Address, IO_DO_VAL_FOR.SubAddress, IO_DO_VAL_FOR.Size, ref rwValuesDO);
if (libraryError.IsError())
return libraryError;
return libraryError;
}
@@ -3987,13 +4005,21 @@ namespace CMS_CORE_Library.S7Net
bank = item.Key / size;
pos = item.Key % size;
// aggiorno
rwValuesDO[bank] = (ushort)(rwValuesDO[bank] & (1 << pos));
if (item.Value)
{
rwValuesDO[bank] = (ushort)(((int)rwValuesDO[bank]).SetBitOne(pos));
}
else
{
rwValuesDO[bank] = (ushort)(((int)rwValuesDO[bank]).SetBitZero(pos));
}
}
// scrivo!
libraryError = MEM_RWWordList(W, 0, IO_DO_FORCE.MemType, IO_DO_FORCE.Address, IO_DO_FORCE.SubAddress, IO_DO_FORCE.Size, ref rwValuesDO);
if (libraryError.IsError())
return libraryError;
return libraryError;
}
/// <summary>
@@ -4023,7 +4049,14 @@ namespace CMS_CORE_Library.S7Net
bank = item.Key/size;
pos = item.Key % size;
// aggiorno
rwValuesAO[bank] = (ushort)(rwValuesAO[bank] & (1 << pos));
if (item.Value)
{
rwValuesAO[bank] = (ushort)(((int)rwValuesAO[bank]).SetBitOne(pos));
}
else
{
rwValuesAO[bank] = (ushort)(((int)rwValuesAO[bank]).SetBitZero(pos));
}
}
// scrivo!
+13
View File
@@ -340,6 +340,19 @@ namespace CMS_CORE_Library.Utils
return (b & (1 << bitNumber)) != 0;
}
public static int SetBitOne(this int value, int position)
{
// Set a bit at position to 1.
return value |= (1 << position);
}
public static int SetBitZero(this int value, int position)
{
// Set a bit at position to 0.
return value & ~(1 << position);
}
public static CultureInfo GetCultureFromThreeLetter(string threeLetter)
{
// Get culture info from three letter iso standard