nuovi metodi conversione word/dword

This commit is contained in:
Samuele E. Locatelli
2019-11-18 12:52:17 +01:00
parent ea0f2a8290
commit 66ff8065cd
3 changed files with 174 additions and 11 deletions
+157 -6
View File
@@ -98,10 +98,10 @@ namespace IOB_WIN
return answ;
}
/// <summary>
/// Converte direttamente un valore Int su un oggetto Word=byte[2]
/// Converte direttamente un valore Short su un oggetto byte[2]
/// </summary>
/// <param name="valore">valore da scrivere</param>
public byte[] wordToByte(string valore)
public byte[] intToByte(string valore)
{
byte[] answ = new byte[2];
try
@@ -119,7 +119,49 @@ namespace IOB_WIN
return answ;
}
/// <summary>
/// Converte direttamente un valore Int su un oggetto DWord=byte[2]
/// Converte direttamente un valore Int su un oggetto byte[4]
/// </summary>
/// <param name="valore">valore da scrivere</param>
public byte[] dintToByte(string valore)
{
byte[] answ = new byte[4];
try
{
int valInt = 0;
int.TryParse(valore, out valInt);
byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt);
int byteLen = 4;
Buffer.BlockCopy(strByte, 0, answ, 0, byteLen);
}
catch (Exception exc)
{
lgError($"Errore in gestione scrittura DINT {valore} in byte{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// Converte direttamente un valore UInt16 su un oggetto byte[2]
/// </summary>
/// <param name="valore">valore da scrivere</param>
public byte[] wordToByte(string valore)
{
byte[] answ = new byte[2];
try
{
ushort valInt = 0;
ushort.TryParse(valore, out valInt);
byte[] strByte = S7.Net.Types.Word.ToByteArray(valInt);
int byteLen = 2;
Buffer.BlockCopy(strByte, 0, answ, 0, byteLen);
}
catch (Exception exc)
{
lgError($"Errore in gestione scrittura INT {valore} in byte{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// Converte direttamente un valore UInt32 su un oggetto byte[4]
/// </summary>
/// <param name="valore">valore da scrivere</param>
public byte[] dwordToByte(string valore)
@@ -127,9 +169,9 @@ namespace IOB_WIN
byte[] answ = new byte[4];
try
{
short valInt = 0;
short.TryParse(valore, out valInt);
byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt);
ushort valInt = 0;
ushort.TryParse(valore, out valInt);
byte[] strByte = S7.Net.Types.DWord.ToByteArray(valInt);
int byteLen = 4;
Buffer.BlockCopy(strByte, 0, answ, 0, byteLen);
}
@@ -288,6 +330,94 @@ namespace IOB_WIN
}
}
/// <summary>
/// Salvo in memblock il valore DInt indicato con formattazione siemens
/// </summary>
/// <param name="MemBlock">Blocco memoria come byte[] dove scrivere</param>
/// <param name="startPos">Posizione inizio scrittura</param>
/// <param name="valore">Valore da scrivere</param>
public void saveWordOnMemBlock(ref byte[] MemBlock, int startPos, string valore)
{
try
{
byte[] stringPar = new byte[4];
int valInt = 0;
int.TryParse(valore, out valInt);
byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt);
int byteLen = 4;
Buffer.BlockCopy(strByte, 0, MemBlock, startPos, byteLen);
//var verifica = S7.Net.Types.String.FromByteArray(MemBlock);
}
catch (Exception exc)
{
lgError($"Errore in gestione scrittura DINT {valore} alla posizione {startPos} byte{Environment.NewLine}{exc}");
}
}
/// <summary>
/// Salvo in memblock il valore DInt indicato con formattazione siemens
/// </summary>
/// <param name="MemBlock">Blocco memoria come byte[] dove scrivere</param>
/// <param name="stringKey">Valore da scrivere</param>
/// <param name="startPos">Posizione inizio scrittura</param>
public void saveWordOnMemBlock(ref byte[] MemBlock, string stringKey, int startPos)
{
if (currProdData.ContainsKey(stringKey))
{
try
{
string valore = currProdData[stringKey];
saveDIntOnMemBlock(ref MemBlock, startPos, valore);
}
catch (Exception exc)
{
lgError($"Errore in gestione scrittura DINT {stringKey}{Environment.NewLine}{exc}");
}
}
}
/// <summary>
/// Salvo in memblock il valore DInt indicato con formattazione siemens
/// </summary>
/// <param name="MemBlock">Blocco memoria come byte[] dove scrivere</param>
/// <param name="startPos">Posizione inizio scrittura</param>
/// <param name="valore">Valore da scrivere</param>
public void saveDWordOnMemBlock(ref byte[] MemBlock, int startPos, string valore)
{
try
{
byte[] stringPar = new byte[4];
int valInt = 0;
int.TryParse(valore, out valInt);
byte[] strByte = S7.Net.Types.DInt.ToByteArray(valInt);
int byteLen = 4;
Buffer.BlockCopy(strByte, 0, MemBlock, startPos, byteLen);
//var verifica = S7.Net.Types.String.FromByteArray(MemBlock);
}
catch (Exception exc)
{
lgError($"Errore in gestione scrittura DINT {valore} alla posizione {startPos} byte{Environment.NewLine}{exc}");
}
}
/// <summary>
/// Salvo in memblock il valore DInt indicato con formattazione siemens
/// </summary>
/// <param name="MemBlock">Blocco memoria come byte[] dove scrivere</param>
/// <param name="stringKey">Valore da scrivere</param>
/// <param name="startPos">Posizione inizio scrittura</param>
public void saveDWordOnMemBlock(ref byte[] MemBlock, string stringKey, int startPos)
{
if (currProdData.ContainsKey(stringKey))
{
try
{
string valore = currProdData[stringKey];
saveDIntOnMemBlock(ref MemBlock, startPos, valore);
}
catch (Exception exc)
{
lgError($"Errore in gestione scrittura DINT {stringKey}{Environment.NewLine}{exc}");
}
}
}
/// <summary>
/// Salvo in memblock il valore stringa indicato con formattazione siemens
/// </summary>
/// <param name="MemBlock">Blocco memoria come byte[] dove scrivere</param>
@@ -1203,6 +1333,7 @@ namespace IOB_WIN
{
memAddrWrite = "";
int valInt = 0;
uint valUInt = 0;
// cerco in area memMapWrite...
if (memMap.mMapWrite.ContainsKey(item.uid))
{
@@ -1230,6 +1361,14 @@ namespace IOB_WIN
valInt = getScaledInt(currMem);
saveDIntOnMemBlock(ref MemBlock, 0, valInt.ToString());
break;
case plcDataType.Word:
valUInt = getScaledUInt(currMem);
saveWordOnMemBlock(ref MemBlock, 0, valInt.ToString());
break;
case plcDataType.DWord:
valUInt = getScaledUInt(currMem);
saveDWordOnMemBlock(ref MemBlock, 0, valInt.ToString());
break;
case plcDataType.Real:
saveRealOnMemBlock(ref MemBlock, 0, currMem.value);
break;
@@ -1290,6 +1429,18 @@ namespace IOB_WIN
return valInt;
}
private static uint getScaledUInt(dataConf currMem)
{
uint valUInt;
// prima faccio eventuale fattore di scala...
uint.TryParse(currMem.value, out valUInt);
if (currMem.factor > 1)
{
valUInt = valUInt * (uint)currMem.factor;
}
return valUInt;
}
/// <summary>
/// Metodo dispose x il currPLC contenuto
+1 -1
View File
@@ -144,7 +144,7 @@ namespace IOB_WIN
case taskType.sendWatchDogMes2Plc:
int valore = counterMes2Plc;
saveProdData(item);
MemBlock = dwordToByte(valore.ToString());
MemBlock = dintToByte(valore.ToString());
memAddrWrite = "DB700.DBB12";
taskVal = $"VALUE DB700.DBB12 --> {counterMes2Plc}";
break;
+16 -4
View File
@@ -83,17 +83,29 @@ namespace IOB_WIN
{
saveStringOnMemBlock(ref MemBlock, item.Key, 0, byteSize);
}
else if (currMem.tipoMem == plcDataType.Int)
{
short valDInt = 0;
short.TryParse(item.Value, out valDInt);
MemBlock = S7.Net.Types.Int.ToByteArray(valDInt);
}
else if (currMem.tipoMem == plcDataType.DInt)
{
int valDInt = 0;
int.TryParse(item.Value, out valDInt);
MemBlock = S7.Net.Types.DInt.ToByteArray(valDInt);
}
else if (currMem.tipoMem == plcDataType.Int)
else if (currMem.tipoMem == plcDataType.Word)
{
short valDInt = 0;
short.TryParse(item.Value, out valDInt);
MemBlock = S7.Net.Types.Int.ToByteArray(valDInt);
ushort valDInt = 0;
ushort.TryParse(item.Value, out valDInt);
MemBlock = S7.Net.Types.Word.ToByteArray(valDInt);
}
else if (currMem.tipoMem == plcDataType.DWord)
{
uint valDInt = 0;
uint.TryParse(item.Value, out valDInt);
MemBlock = S7.Net.Types.DWord.ToByteArray(valDInt);
}
}
taskVal = item.Value;