Completato test e salvataggio valori x OMRON hex/int

This commit is contained in:
Samuele E. Locatelli
2019-11-01 16:19:13 +01:00
parent 62bff0939b
commit a013c4eb8d
2 changed files with 80 additions and 27 deletions
+2 -2
View File
@@ -39,10 +39,10 @@ CMDIOB2CALL=/IOB/getIob2call?GWIP=
;STARTLIST=VL25
; INTERCLAYS FORNO
STARTLIST=INTERCL_01
;STARTLIST=INTERCL_01
; INTERCLAYS MISCELA
;STARTLIST=INTERCL_02
STARTLIST=INTERCL_02
+78 -25
View File
@@ -46,6 +46,69 @@ namespace IOB_WIN
/// </summary>
protected short[] memWriteWR;
// <summary>
/// Calcola la conversione da byte --> num decimale --> HEX --> conversione come stringa in INT
/// </summary>
/// <param name="valore"></param>
/// <returns></returns>
protected int convDHD(short valore)
{
int answ = 0;
string hexVal = valore.ToString("x");
int.TryParse(hexVal, out answ);
return answ;
}
// <summary>
/// Calcola la conversione da INTERO a intero HEX x OMRON
/// </summary>
/// <param name="valore"></param>
/// <returns></returns>
protected int convHD(string hexVal)
{
int answ = 0;
try
{
if (!hexVal.StartsWith("0x"))
{
hexVal = "0x" + hexVal;
}
answ = Convert.ToInt32(hexVal, 16);
}
catch
{ }
return answ;
}
/// <summary>
/// Converte un valore di 2 short in un unico numero accostato:
/// es: legge delle coppie di valori INT, vanno trasformati in HEX e POI accodati, dove il primo è x 1 e il secondo x 10000 (in pratica va in testa)
/// 12540 --> diviso in 1 | 2540 --> in HEX diventa 1 | 9472, ma il 9472 va su byte[0] e 1 su byte[1]
/// </summary>
/// <param name="valori"></param>
/// <returns></returns>
protected int convFromHex(short[] valori)
{
int answ = 0;
string fullVal = $"{convDHD(valori[1]).ToString("D4")}{convDHD(valori[0]).ToString("D4")}";
int.TryParse(fullVal, out answ);
return answ;
}
/// <summary>
/// Converte un valore intero in 2 short che il PLC riporterà ad HEX x visualizzare:
/// es: legge delle coppie di valori INT, vanno trasformati in HEX e POI accodati, dove il primo è x 1 e il secondo x 10000 (in pratica va in testa)
/// 12540 --> diviso in 1 | 2540 --> in HEX diventa 1 | 9472, ma il 9472 va su byte[0] e 1 su byte[1]
/// </summary>
/// <param name="valoreInt"></param>
/// <returns></returns>
private short[] convToHex(int valoreInt)
{
short[] answ = new short[2];
string fullVal = valoreInt.ToString("D8");
// converto un pezzo alla volta...
answ[0] = (short)convHD(fullVal.Substring(4, 4));
answ[1] = (short)convHD(fullVal.Substring(0, 4));
return answ;
}
/// <summary>
/// estende l'init della classe base...
/// </summary>
@@ -58,6 +121,18 @@ namespace IOB_WIN
lastPzCountSend = DateTime.Now;
lastWarnODL = DateTime.Now;
// test conversione valori...
short[] valDM = new short[2];
// cario dati
valDM[0] = 9472;
valDM[1] = 1;
// legge delle coppie di valori INT, vanno trasformati in HEX e POI accodati, dove il primo è x 1 e il secondo x 10000 (in pratica va in testa)
// esempio 12540 --> diviso in 1 | 2540 --> in HEX diventa 1 | 9472, ma il 9472 va su byte[0] e 1 su byte[1]
var testDec = convFromHex(valDM);
var testHex = convToHex(testDec);
#if false
OMRON_ref = new OmronFinsTCP.Net.EtherNetPLC();
OMRON_ref.Link("192.168.250.1", 9600, 500);
@@ -96,6 +171,7 @@ namespace IOB_WIN
omronFullTest();
#endif
}
/// <summary>
/// Test completo funzioni OMRON
/// </summary>
@@ -423,29 +499,6 @@ namespace IOB_WIN
{
// controllo tutta la memoria allarmi SE richiesto
}
// <summary>
/// Calcola la conversione da byte --> num decimale --> HEX --> conversione come stringa in INT
/// </summary>
/// <param name="valore"></param>
/// <returns></returns>
protected int convDHD(short valore)
{
int answ = 0;
string hexVal = valore.ToString("x");
int.TryParse(hexVal, out answ);
return answ;
}
// <summary>
/// Calcola la conversione da INTERO a intero HEX x OMRON
/// </summary>
/// <param name="valore"></param>
/// <returns></returns>
protected short convHD(string hexVal)
{
short answ = 0;
short.TryParse(hexVal, out answ);
return answ;
}
/// <summary>
/// Oggetto per lettura PESO rilevato (DM20-21)
/// </summary>
@@ -494,8 +547,8 @@ namespace IOB_WIN
short[] valDM = new short[2];
int highVal = value / 10000;
int lowVal = value - (highVal * 10000);
valDM[0] = convHD(lowVal.ToString("x"));
valDM[1] = convHD(highVal.ToString("x"));
valDM[0] = (short)convHD(lowVal.ToString("x"));
valDM[1] = (short)convHD(highVal.ToString("x"));
OMRON_ref.WriteWords(OmronFinsTCP.Net.PlcMemory.DM, 26, 2, valDM);
}
}