Ok allora, Signal è un mio oggetto custom.
Un utente da interfaccia imposta il valore di un segnale(esempio: 1, 2, 3, 10) e questo viene tradotto in bit.
Lato mio nel codice vado a recuperare questo dato e lo salvo in un mio oggetto custom chiamato Signal, nella variabile Value dell' oggetto, di tipo stringa. Questo valore però viene ribaltato, cioè assegno alla variabile locale "value" il valore di Value con una reverseString.
Ciclo la stringa e inserisco ogni valore in un array di byte chiamato bitArray nel codice che corrispondono ai vaori(1 = true, 2 = false).
Esempio, se la mia stringa sarà:
1001 avrò: [true, false, false, true].
Una volta che il mio array è completo vado ad inserirlo in una lista di byte.
Quindi l'output sarà: una lista di byte contenente tutti i bitarray, ovviamente convertiti in byte.
L'oggetto converter è anche esso custom, mi serve per convertire dei valori appunto.
Spero di essere stato più o meno chiaro. Il mio codice è questo, molto contorto.
public byte[] CreateMessageBytes(Message message)
{
try
{
//Creating an object to convert bitArray to byte, retrieve the selected message on the form and the signals
Converter converter = new Converter();
Message msgToSend = message;
List<Signal> signals = msgToSend.Signals.OrderBy(x => x.BitStart).ToList();
List<byte> byteList = new List<byte>(8);
BitArray toConvert = null;
Signal signal = null;
//It is used to do the difference between the startbit position of the next message with the previous in order
//to view if there are some byte between them to put to 0 value
int startBitOld = -1;
bool JumpSection = false;
//byte array that contains the values inserted into the byte list (the list is used cause it is more comfortble)
byte[] byteArray = new byte[8];
for (int i = 0; i < signals.Count; i++)
{
//it is used if there is a signal longer than 1 byte, so the next
//instruction (if (!JumpSection)) if false is not executed
JumpSection = false;
//only the first time, cause at the bottom of the method there is another signal we have get
if (i == 0)
{
signal = signals[i];
}
//informatiom on the startBit of the signal, signal lenght, count the number of bit, the index
//of the array in which insert the bit
int startBit = signal.BitStart;
startBitOld = startBit;
//Console.WriteLine("bitstart " + i + "is: " + startBit.ToString());
int lenght = signal.Length;
int countBit = lenght;
int charArrayIndex = 0;
//Console.WriteLine(signal.Name.ToString());
if (i > 0) i = i - 1; //the index was increased at the end of the while
bool[] bitArray = new bool[8];
//the signal is longer than 1 byte
if (lenght > 8)
{
// string s = converter.ConvertDecimalToHex(signal.Value.ToString());
////convert to string the signal value
string value = Converter.ReverseString(Convert.ToString(signal.Value, 2));
int arrayPosition = -1;
arrayPosition = startBit;
int counter = 0;
//the byte to insert inside the list
BitArray firstByte = new BitArray(8);
BitArray secondByte = new BitArray(8);
//loop for all the signal lenght from start to the end of the index we need to insert values
//we
while (counter < value.Length)
{
charArrayIndex = 0;
bitArray = new bool[8];
while (counter < value.Length)
{
//split the value string into char array in order to insert the single bit inside the array indexes
if (value.ToCharArray()[counter++].Equals('1'))
{
bitArray[charArrayIndex] = true;
}
else
{
bitArray[charArrayIndex] = false;
}
charArrayIndex++;
if (charArrayIndex % 8 == 0) // if the index is 8 the byte is complete,
{
charArrayIndex = 0;
//saving this byte with this name cause it is read for first, but it has to be inserted on the list has second byte
secondByte = new BitArray(bitArray);
}
else if (counter >= value.Length) //if the lenght is not multiple of 8 some bits have to be set to 0
{
while (charArrayIndex % 8 != 0)
{
bitArray[charArrayIndex++] = false; //set the other bit of the byte to 0
}
charArrayIndex = 0;
//saving this byte with this name cause it is read for second, but it has to be inserted on the list has first byte
firstByte = new BitArray(bitArray);
}
}
}
byteList.Add(converter.ConvertToByte(firstByte));
byteList.Add(converter.ConvertToByte(secondByte));
//reset parameters
startBitOld = startBit;
countBit = 0;
if (i != signals.Count - 1)
signal = signals[++i];
//Console.WriteLine(signal.Name.ToString());
startBit = signal.BitStart;
// Console.WriteLine("bitstart " + i + "is: " + startBit.ToString());
lenght = signal.Length;
countBit = countBit + lenght;
charArrayIndex = 0;
JumpSection = true;
}
//if the previous code has been executed this section is obsolete
// cause there was a signal longer than 1 byte and we have to read the next signal
if (!JumpSection)
{
while (countBit <= 8) // if it is true there are others signals to scroll and put inside the same byte
{
//convert to string the signal value
string value = Converter.ReverseString(Convert.ToString(signal.Value, 2));
int arrayPosition = -1;
arrayPosition = startBit;
while (!(arrayPosition >= 0 && arrayPosition <= 7)) //in order to retrieve the position of the array
//it is a multiple of 8
{
arrayPosition = arrayPosition - 8;
}
//loop for all the signal lenght from start to the end of the index we need to insert values
//we
for (int j = arrayPosition; (j <= (arrayPosition + lenght) - 1); j++)
{
if (charArrayIndex < value.Length)
{
//split the value string into char array in order to insert the single bit inside the array indexes
if (value.ToCharArray()[charArrayIndex++].Equals('1'))
{
if (((7 - j) + lenght - 1) < 7)
bitArray[(7 - j) + lenght - 1] = true;
else
bitArray[7 - j] = true;
}
else
{
if (((7 - j) + lenght - 1) < 7)
bitArray[(7 - j) + lenght - 1] = false;
else
bitArray[7 - j] = false;
}
}
}
//saving this value cause it is useful inside a math differece between indexes to retrieve
//some bytes that are empty(with 0 values)
startBitOld = startBit;
if (i < signals.Count - 1)
{
signal = signals[++i];
//Console.WriteLine(signal.Name.ToString());
startBit = signal.BitStart;
// Console.WriteLine("bitstart " + i + "is: " + startBit.ToString());
lenght = signal.Length;
countBit = countBit + lenght;
charArrayIndex = 0;
}
if (startBitOld == startBit)
{
countBit = 9;
}
}
if ((startBit - startBitOld > 8)) //if true there are bytes between the actual signal and the next one
{
byteList.Add(converter.ConvertToByte(toConvert)); //add the last signal
for (int byteVoid = 0; byteVoid < (((startBit - startBitOld) / 8) - 1); byteVoid++)
//until there are bytes with 0 values create the byte and insert into the list
{
byteList.Add(0);
//Console.WriteLine("Byte has bit config: " + toConvert[0] + " " + toConvert[1] + " "
//+ toConvert[2] + " " + toConvert[3] + " " + toConvert[4] + " "
//+ toConvert[5] + " " + toConvert[6] + " " + toConvert[7] + " ");
}
}
//convert the bitarray into byte but first we have to reverse its values
toConvert = ReverseBitArray(new BitArray(bitArray));
byteList.Add(converter.ConvertToByte(toConvert));
//Console.WriteLine("Byte has bit config: " + toConvert[0] + " " + toConvert[1] + " "
//+ toConvert[2] + " " + toConvert[3] + " " + toConvert[4] + " "
//+ toConvert[5] + " " + toConvert[6] + " " + toConvert[7] + " ");
}
}
//DEBUG------------------------
//for (int i = 0; i < 8; i++)
//{
// Console.WriteLine("Byte " + i + "is: " + byteList.ElementAt(i).ToString());
//}
for (int i = 0; i < byteList.Count; i++)
{
if (i < 8)
byteArray[i] = byteList[i];
}
return byteArray;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return new byte[8];
}
}