0

I'm working on a program in VS2010 C#. It has a GUI that is used to interact with an Arduino over the serial port.

The issue that I'm having is sending a byte value larger than 128(???) from the arduino to the program. I get an integer value on the arduino, break it into highBite and lowByte, and send each one, the reassemble on the other side. If I send 600, it will send highByte of 2 and lowByte of 88, and it reassembles to 600 properly via bitshiting <<8 of highByte.

If I try to send 700 which should be 188 and 2, then I am seeing the 188 show in in C# as 63. Why??? A byte should be unsigned on both arduino and C#, so I'm not sure what is going wrong.

Arduino code (relevant parts): (0x43 signals to C# which data packet it is receiving)

byte bytesToSend[3] = {0x43, byte(88), byte(2)}; // 600 broken down to high and low bytes
Serial.write(bytesToSend, 3); // send three bytes
Serial.println(); //send line break to terminate transmission

byte bytesToSend[3] = {0x43, byte(188), byte(2)}; // 700 broken down to high and low bytes
Serial.write(bytesToSend, 3); // send three bytes
Serial.println(); //send line break to terminate transmission

C# code: (relevant parts - May have missed a syntax or two since I cut/trimmed and pasted...)

string inString = "";
inString = port.ReadLine(); // read a line of data from the serial port
inString = inString.Trim(); //remove newline

byte[] buf = new byte[15]; // reserve space for incoming data
buf = System.Text.Encoding.ASCII.GetBytes(inString); //convert string to byte array I've tried a block copy here, but it didn't work either...

Console.Write("Data received: H: {0}, L: {1}. =", buf[2], buf[1]); //display high and low bytes
Console.WriteLine(Convert.ToUInt32((buf[2] << 8) + buf[1])); //display combined value

And this is what I get in the serial monitor where it writes out the values:

Data received: H: 2, L: 88. = 600
Data received: H: 2, L: 63. = 575

The low byte value gets changed or mis-interpreted from 188 to 63 somewhere in the process. What is causing this and how can I fix it? It seems to work fine when the byte value is below 128, but not when it is above.

1
  • Why are you reading the data from the port as text? It's not text - it's binary data. (And why are you allocating a 15 byte array and then ignoring it? GetBytes creates a new array.)
    – Jon Skeet
    Nov 29, 2013 at 17:38

3 Answers 3

0

I think this could be problem at your c# side code. You should debug this by printing the string which you are reading just after port.ReadLine(), to see what you are receiving.

Also I would suggest to use C# Read(Byte[], Int32, Int32), so that your data is read into Byte Array which is array of unsigned char. ReadLine() is reading data into string (array of char).

0

Your encoding is wrong. Change the line from:

buf = System.Text.Encoding.ASCII.GetBytes(inString);

to

buf = System.Text.Encoding.GetEncoding("Windows-1252").GetBytes(inString);

Better yet, when you instantiate your Port object just set the encoder property to this type.

...
SerialPort port = new SerialPort();
System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("Windows-1252");
port.Encoding = encoder;
...

Remember that ASCII is 7-bit so you will truncate values that are greater than decimal 127. The 1252 encoding is 8 bit and is great for binary data. The table shown at MSDN shows the full symbol support for the encoding.

0

Why, in C#, reading full string - which will force you to deal with encodings, ... - and do post-process instead of parsing in time?

System.IO.BinaryReader bin_port=new System.IO.BinaryReader(port); //Use binary reader
int b;
int data16;
b=bin_port.ReadByte();
switch (b) {
case 0x43: //Read integer
    data16=bin_port.ReadUInt16();
    while (bin_port.ReadByte()!=0x0a); //Discard all bytes until LF
    break;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.