1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| static void TestOperation()
{
PLCInputMemory A = new PLCInputMemory(new PLCRegister()); A.Name = "A"; Console.WriteLine(A.ToString()); Console.WriteLine("A = " + A.GetValue().ToString());
PLCInputMemory B = new PLCInputMemory(new PLCRegister()); B.Name = "B"; Console.WriteLine(B.ToString()); Console.WriteLine("B = " + B.GetValue().ToString());
PLCInputMemory C = new PLCInputMemory(new PLCRegister()); C.Name = "C"; Console.WriteLine(C.ToString()); Console.WriteLine("C = " + C.GetValue().ToString());
PLCInputMemory D = new PLCInputMemory(new PLCRegister()); D.Name = "D"; Console.WriteLine(D.ToString()); Console.WriteLine("D = " + D.GetValue().ToString());
PLCInputInstructionLD IA = new PLCInputInstructionLD(A); Console.WriteLine(IA.ToString());
PLCInputInstructionLD IB = new PLCInputInstructionLD(B); Console.WriteLine(IB.ToString());
PLCInputInstructionLD IC = new PLCInputInstructionLD(C); Console.WriteLine(IC.ToString());
PLCInputInstructionLD ID = new PLCInputInstructionLD(D); Console.WriteLine(ID.ToString());
//Autres instructions...
//PLCInputInstructionLDN IB = new PLCInputInstructionLDN(B); Console.WriteLine(IB.ToString());
//PLCInputInstructionLDR IC = new PLCInputInstructionLDR(C); Console.WriteLine(IC.ToString());
//PLCInputInstructionLDF ID = new PLCInputInstructionLDF(D); Console.WriteLine(ID.ToString());
OperationValue OpA = new OperationValue(IA); Console.WriteLine(OpA.ToString()); Console.WriteLine("A = " + OpA.Evaluate().ToString());
OperationValue OpB = new OperationValue(IB); Console.WriteLine(OpB.ToString()); Console.WriteLine("B = " + OpB.Evaluate().ToString());
OperationValue OpC = new OperationValue(IC); Console.WriteLine(OpC.ToString()); Console.WriteLine("C = " + OpC.Evaluate().ToString());
OperationValue OpD = new OperationValue(ID); Console.WriteLine(OpD.ToString()); Console.WriteLine("D = " + OpD.Evaluate().ToString());
/* (A*B*D + C) * B * D + C */
Console.WriteLine("Test opération");
string dbgstr;
//A
OperationBase expression = OpA; dbgstr = expression.ToString() + " = " + expression.Evaluate().ToString(); Console.WriteLine(dbgstr);
//A*B
expression = expression.AND(OpB); dbgstr = expression.ToString() + " = " + expression.Evaluate().ToString(); Console.WriteLine(dbgstr);
//A*B*D
expression = expression.AND(OpD); dbgstr = expression.ToString() + " = " + expression.Evaluate().ToString(); Console.WriteLine(dbgstr);
//A*B*D+C
expression = expression.OR(OpC); dbgstr = expression.ToString() + " = " + expression.Evaluate().ToString(); Console.WriteLine(dbgstr);
//(A*D*D+C)*B
expression = expression.AND(OpB); dbgstr = expression.ToString() + " = " + expression.Evaluate().ToString(); Console.WriteLine(dbgstr);
//(A*D*D+C)*B*D
expression = expression.AND(OpD); dbgstr = expression.ToString() + " = " + expression.Evaluate().ToString(); Console.WriteLine(dbgstr);
//(A*D*D+C)*B*D+C
expression = expression.OR(OpC); dbgstr = expression.ToString() + " = " + expression.Evaluate().ToString(); Console.WriteLine(dbgstr);
/* générer table de vérité */
Console.WriteLine("(A*D*D+C)*B*D+C");
GenererTableVerite(new PLCInputMemory[]{A,B,C,D}, expression);
//(A*D*D+C)*B*D+C = A*B*D+C
Console.WriteLine("(A*D*D+C)*B*D+C = A*B*D+C");
expression = OpA.AND(OpB).AND(OpD).OR(OpC);
GenererTableVerite(new PLCInputMemory[] { A, B, C, D }, expression);
//A*D
Console.WriteLine("A*D");
expression = OpA.AND(OpD);
GenererTableVerite(new PLCInputMemory[] { A, D }, expression);
//A+C
Console.WriteLine("A+C");
expression = OpA.OR(OpC);
GenererTableVerite(new PLCInputMemory[] { A, C }, expression);
Pause();
}
static void GenererTableVerite(PLCInputMemory[] Inputs_O, OperationBase Expression_O)
{
int NbBits = Inputs_O.Length;
int NbPossibilities = 1 << Inputs_O.Length;
Console.WriteLine("Table de vérité:");
Console.WriteLine("Equation: " + Expression_O.ToString());
Console.WriteLine("Nombre d'entrées: " + NbBits);
Console.WriteLine("Nombre de possibilités: " + NbPossibilities);
/* génération de toutes les possibilités */
bool[][] Possibilities_b = new bool[NbPossibilities][];
for (int i = 0; i < NbPossibilities; i++)
{
Possibilities_b[i] = new bool[NbBits];
}
/* On remplit le tableau */
bool value_b = false;
int count_i = 0;
int base_i = 2; //2,4,8,16, etc.
int column_i = NbBits - 1;
for (int j = 0; j < NbBits; j++)
{
count_i = 0;
base_i = 1 << j;
for (int i = 0; i < NbPossibilities; i++)
{
Possibilities_b[i][column_i] = value_b;
count_i++;
if (count_i == base_i)
{
count_i = 0;
value_b = !value_b;
}
}
column_i--;
}
for (int i = 0; i < NbPossibilities; i++)
{
for (int j = 0; j < NbBits; j++)
{
bool v = Possibilities_b[i][j];
if (v) Console.Write("1");
else Console.Write("0");
}
Console.Write(" = ");
for (int j = 0; j < NbBits; j++)
{
Inputs_O[j].SetValue(Possibilities_b[i][j]);
}
if (Expression_O.Evaluate())
Console.Write("1");
else
Console.Write("0");
Console.Write("\n");
}
Console.WriteLine();
} |
Partager