I'm currently working on a master/slave where the Master is a C# program and the Slave is an Arduino Uno. The Arduino is reading several values and is working as expected, but I'm having some troubles on the C# side. I'm reading 3 bytes from an AD converter (AD7680), which returns 3 bytes of data structured in the following way:
0000 | 16 bit number | 0000
My C# program is reading the returned value in a double, which is the expected value. BUT I didn't find out how to get rid of the last four 0's and obtain the 2 byte number I need.
What should be the best approach to get the right value without loosing data? I tried 'BitConverter' but it´s not what I'm expecting, and I have no clue how to proceed. I currently can´t attach the code unfortunately, but I could reference anything on it if needed.
Thanks for reading!
EDIT: This is the function on the C# side:
public double result(byte[] command)
{
try
{
byte[] buffer = command;
arduinoBoard.Open();
arduinoBoard.Write(buffer, 0, 3);
int intReturnASCII = 0;
char charReturnValue = (Char)intReturnASCII;
Thread.Sleep(200);
int count = arduinoBoard.BytesToRead;
double returnResult = 0;
string returnMessage = "";
while (count > 0)
{
intReturnASCII = arduinoBoard.ReadByte();
//string str = char.ConvertFromUtf32(intReturnASCII);
returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
count--;
}
returnResult = double.Parse(returnMessage, System.Globalization.CultureInfo.InvariantCulture);
arduinoBoard.Close();
return returnResult;
}
catch (Exception e)
{
return 0;
}
}
And the Arduino function that communicates with it is this one:
unsigned long ReturnPressure(){
long lBuffer = 0;
byte rtnVal[3];
digitalWrite(SLAVESELECT , LOW);
delayMicroseconds(1);
rtnVal[0] = SPI.transfer(0x00);
delayMicroseconds(1);
rtnVal[1] = SPI.transfer(0x00);
delayMicroseconds(1);
rtnVal[2] = SPI.transfer(0x00);
delayMicroseconds(1);
digitalWrite(SLAVESELECT, HIGH);
// assemble into long type
lBuffer = lBuffer | rtnVal[0];
lBuffer = lBuffer << 8;
lBuffer = lBuffer | rtnVal[1];
lBuffer = lBuffer << 8;
lBuffer = lBuffer | rtnVal[2];
return lBuffer;
}
Please show how you receive the data from the arduino in your C# code
– frarugi87