Author Topic: Arduino Serial Read issue.  (Read 16531 times)

0 Members and 1 Guest are viewing this topic.

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Arduino Serial Read issue.
« on: March 23, 2012, 07:52:55 am »
Hi all,
   i am probably over looking something here but here goes.

I have hooked up my Arduino board to my PC and am sending data to the arduino in 2 byte blocks. The first byte appears to be being read ok. However the second byte appears to be trimmed down to 6 bits in length.

Is there some reason this would happen, i am looking to read a value from 0 to 190. But anything over 127 reverts back to 63.

Regards
Testing one two three...
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7513
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #1 on: March 23, 2012, 08:39:55 am »
Few thoughts...

1) Have a look at the variable you're using to store the data.

If it's a 'signed' byte then it can only store between negative 128  and positive 127

You want an 'unsigned' byte, which can store between 0 and 255

2) Are you waiting for the byte to be fully received before reading it out.  Im not sure how Arduino do this but there should be some sort of a wait or loop to check that a byte is ready to be read.

3) Check that you're initializing the serial port correctly and to the same settings that your PC is using.
etc.  number of bits, stop bits, start bits, parity
« Last Edit: March 23, 2012, 08:47:00 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #2 on: March 23, 2012, 08:57:41 am »
This is the code i am using at the moment.



Code: [Select]
if (Serial.available() >1) {

       byte inByte = Serial.read();  //According to the Aruino docs, a byte is unsigned. 0-255
       byte DataIn = 0;
          if (inByte = 255) {            // This does trigger when the first byte is 255.
            DataIn = Serial.read();  // Read the second byte.
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Read Value is: ");
            lcd.print(DataIn );         // LCD displays correct for values from 0 to 127.
          }
      }

Regards
Testing one two three...
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 6681
Re: Arduino Serial Read issue.
« Reply #3 on: March 23, 2012, 09:20:40 am »
3) Check that you're initializing the serial port correctly and to the same settings that your PC is using.
etc.  number of bits, stop bits, start bits, parity
I second this, given the documentation doesn't say anything about the port parameters other than baud rate despite the MCU's serial port having the capability for that.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7513
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #4 on: March 23, 2012, 09:48:07 am »
how does the lcd.print() command know to display a number (0-255) or to display the ascii character for that number?

Maybe the LCD only supports a 0-127 character set?


Try an IF/ELSE statement that checks if the received data is the value expected.
Then does either lcd.print('correct') or lcd.print('wrong')

That way you can prove or eliminate the lcd as a cause of the issue.
« Last Edit: March 23, 2012, 09:53:14 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #5 on: March 23, 2012, 10:07:22 am »
I didn't question the LCD because it shows the correct value up to 127.

I have done the same thing many times with othe micros, this is the first time i have had this issue.

Regards
Testing one two three...
 

Offline johnboxall

  • Supporter
  • ****
  • Posts: 526
  • Country: au
  • Problems are solved by taking action.
Re: Arduino Serial Read issue.
« Reply #6 on: March 23, 2012, 10:37:16 am »
This is the code i am using at the moment.



Code: [Select]
if (Serial.available() >1) {

       byte inByte = Serial.read();  //According to the Aruino docs, a byte is unsigned. 0-255
       byte DataIn = 0;
          if (inByte = 255) {            // This does trigger when the first byte is 255.
            DataIn = Serial.read();  // Read the second byte.
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Read Value is: ");
            lcd.print(DataIn );         // LCD displays correct for values from 0 to 127.
          }
      }

Regards

Try changing the first line to
if (Serial.available() >0) {

 

Offline harnon

  • Regular Contributor
  • *
  • Posts: 215
  • Country: au
  • Is this thing on?
    • My Personal Website
Re: Arduino Serial Read issue.
« Reply #7 on: March 23, 2012, 02:04:32 pm »
Try changing the first line to
if (Serial.available() >0) {
That may cause issues when he calls "DataIn = Serial.read()".  If there isn't a second byte available it will return -1


Quote from: ArduinoDocs
the first byte of incoming serial data available (or -1 if no data is available) - int
 

Offline harnon

  • Regular Contributor
  • *
  • Posts: 215
  • Country: au
  • Is this thing on?
    • My Personal Website
Re: Arduino Serial Read issue.
« Reply #8 on: March 23, 2012, 02:08:00 pm »
A couple of other things you could look at -
the Arduino docs state the return type is "int", maybe change your variables to match?

Also have a look at
Code: [Select]
Serial.println(incomingByte, DEC);

I haven't used the LCD library but can you declare the print type (e.g. "DEC")?

EDIT: looks like you can http://arduino.cc/en/Reference/LiquidCrystalPrint
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #9 on: March 23, 2012, 11:00:16 pm »
Thanks for your input everyone.

I set up a test to see if this was limited to the LCD lib. I attached a servo and set the range from 0 to 180.

The servo will only move from position 0 to 127. I feel this proves the serial data is being read as signed values.

I got the same result declaring as either Byte, Unsigned Int or int.

Regards
Testing one two three...
 

Offline harnon

  • Regular Contributor
  • *
  • Posts: 215
  • Country: au
  • Is this thing on?
    • My Personal Website
Re: Arduino Serial Read issue.
« Reply #10 on: March 24, 2012, 11:19:33 am »
Did you try the LCD.print(var, DEC/BIN/...) method?

You could also try subtracting 128 from the variable you receive and see what you get...does it go negative on the LCD?
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #11 on: March 25, 2012, 01:58:00 am »
I have tried everything i can think of. I have done this many times with PIC projects with no issue.
I cant seem to figure out why Arduino wants to sign serial data.

I have attached the files i am using for this issue. The zip contains both the Arduino code and the VB.net code(there is an exe compiled for those who dont have VB.net).

Regards
Testing one two three...
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 3139
  • Country: us
Re: Arduino Serial Read issue.
« Reply #12 on: March 25, 2012, 04:43:47 am »
The Arduino code looks OK to me.  It should return 0 to 255 or -1.

So...  Add the display of the decimal output and see what it really is.

Other things to consider:

MODE of the comm port on the PC set properly?  (8bits and not 7 from some random initialization?)

Is this set right: "The returned value of CHR() depends on the code page for the current thread, which is contained in the ANSICodePage property of the TextInfo class in the System.Globalization namespace." ?
It also seems like it might be something that accidentally gets changed based on something randomish.  I'd add display code to see what the values were before transmitting as well...
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #13 on: March 25, 2012, 06:00:45 am »
The Arduino code looks OK to me.  It should return 0 to 255 or -1.


That is the problem i am faced with, it 'SHOULD' work. On the VB end i have configured the port as standard 8,n,1.
I am thinking the next stage is to capture the rx of the Arduino with a DSO to see what the Arduino sees.

This is starting to get frustrating.

Regards
Testing one two three...
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 3139
  • Country: us
Re: Arduino Serial Read issue.
« Reply #14 on: March 25, 2012, 07:52:39 am »
>> But anything over 127 reverts back to 63.
     :
>> I feel this proves the serial data is being read as signed values.

So which is happening now?  LCD.print should actually show negative numbers as "-86" for 170, for example if you're actually getting sign extension at an inappropriate time.
Everything over 127 showing up as a single value sounds like some sort of special encoding scheme for non-ascii characters that windows could be doing, except that 63 is not a reasonable value for a prefix character.

This simple sketch seems to work correctly.  I can type 8-bit characters on my Mac with option-keystroke (for instance, option-2 sends 170), so I suspect that the serial monitor for windows should allow 8bit characters as well (via the numlock-keypad hack, or whatever it was?)

void setup()
{
  Serial.begin(115200);
}
void loop()
{
  int c = Serial.read();
 
  if (c != -1) {
    Serial.println(c);
  }
}
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7513
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #15 on: March 25, 2012, 08:32:19 am »
Quote
Serial.begin(115200);

115200 is quite fast, you might be getting errors, try using 9600  (remember to change it on both the micro and the PC)

Quote
lcd.print(DataIn);        //For some reason, any data recieved via serial is Signed.

Just out of interest, what happens if you code it to print the numbers on the LCD as decimal
lcd.print(datain,DEC);

also try the slightly different println function
Serial.println(datain, DEC);

you could also try HEX



in VB
Code: [Select]
outStr = Chr(NumericUpDown1.Value) + Chr(NumericUpDown2.Value)check both NumericUpDown1 AND  NumericUpDown2 have their .Maximum    .Minimum values set correct?
Maybe NumericUpDown2 never got changed and is set to -127 +127 or something.


Another thing you could test. If you change your first byte so its 200 instead of 50, does that stop working as well?
« Last Edit: March 25, 2012, 09:16:51 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #16 on: March 25, 2012, 10:20:48 pm »
After hunting down a serial port sniffer that works on Win7 64bit. I have discovered the issue is with Microsoft. it seems whoever developed the 2010 version of .net decided bytes are signed by default. So far, everything i have tried results in the serial port sending signed bytes. The Microsoft website days Bytes are unsigned, but they magicly turn into signed bytes when i send them to the serial port.

The battle continues.

Regards
Testing one two three...
 

Offline harnon

  • Regular Contributor
  • *
  • Posts: 215
  • Country: au
  • Is this thing on?
    • My Personal Website
Re: Arduino Serial Read issue.
« Reply #17 on: March 25, 2012, 10:43:28 pm »
After hunting down a serial port sniffer that works on Win7 64bit. I have discovered the issue is with Microsoft. it seems whoever developed the 2010 version of .net decided bytes are signed by default. So far, everything i have tried results in the serial port sending signed bytes. The Microsoft website days Bytes are unsigned, but they magicly turn into signed bytes when i send them to the serial port.

The battle continues.

Regards

Hmm... strange, I've never come across this issue in C#.NET 2010.  I'd suggest going right back to basics and seeing what bits you get with lcd.print(var, bin).  Is the result what you expect?

If VB.Net is doing something funny you may want to have a look at this code
Code: [Select]
#include <stdio.h>

int
main (void)
{
  int i;
  while ((i = getchar()) != EOF)
    {
      unsigned char c = i;
      printf ("read c = '%c'\n", c);
    }
  return 0;
}

taken from here.  (Not so much the getchar(), but the cast from int to unsigned char)
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #18 on: March 26, 2012, 07:38:59 am »
After digging around the msdn site and others. I have found that .NET will only send unsigned bytes if they are set up as an array of bytes. Sending a single byte or string will always be sent as signed bytes.

Regards
Testing one two three...
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7513
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #19 on: March 26, 2012, 11:24:24 am »
After digging around the msdn site and others. I have found that .NET will only send unsigned bytes if they are set up as an array of bytes. Sending a single byte or string will always be sent as signed bytes.

Regards

Really, that's the most stupid thing ever.

Makes me glad i use Delphi and not VB :)

Did switching to an array fix your issues?
« Last Edit: March 26, 2012, 11:26:52 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #20 on: March 26, 2012, 11:31:26 am »
Yep, fixed everything. I was questioning Arduino, but it turns out to be another .NET thing. It took a serial port sniffer app to resolve it tho.

I am thinking, in this day and age, there should be something out there that offers a comparable GUI to VS, but better.

Regards
Testing one two three...
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: au
Re: Arduino Serial Read issue.
« Reply #21 on: March 26, 2012, 11:34:12 am »
I hear a lot of people drop the Delphi word. i must look into it some day. Does it have some sort of gui or is it some sort of command line thing?

Regards
Testing one two three...
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7513
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #22 on: March 26, 2012, 11:51:46 am »
It's full GUI, the Delphi IDE is similar to Visual Studio.
Microsoft and Embarcadero actually exchanged some head software engineers. The Delphi IDE got restyled to be bit more like visual studio and vice versa.
Delphi also got all the .NET stuff (although i never use that, i just make win32/64 apps)

Delphi Starter-Edition is $199 (limited commercial license, you can sell your apps but cant make more than $1000 a year)  Next level is Professional for $899 with no license limits.

They also have 30day free trials if you want to play around.

Here's a pic of the latest Delphi IDE  (Delphi XE2)
« Last Edit: March 26, 2012, 12:03:41 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Teknotronix

  • Regular Contributor
  • *
  • Posts: 145
  • Country: au
Re: Arduino Serial Read issue.
« Reply #23 on: March 26, 2012, 12:17:17 pm »
Delphi (object pascal) has always been known as the language that is the fastest to understand the logic of any one piece of code. The compiler is also super fast and Embarcadero/Borland give you access to all the code in the libraries so you can debug to the nth degree.

Delphi 7 was such a good tool that sales dropped off for all versions after it because developers (including myself) felt no need to upgrade, it just had everything you needed to get the job done. That has changed a little since they did a big survey asking developers what they wanted. Turned out us delphi devs are all purists and told them to pull out the .Net crap into another product. The new version can compile Mac OSX an iOS apps now too, but I have no experience with these. If you want to code native apps, you cant go past it.

In the context of Arduino, it would have been smarter if they used it as the base language for their IDE for the sake of budding coders.
« Last Edit: March 26, 2012, 12:35:05 pm by Teknotronix »
Don't drone me bro!

 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7513
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #24 on: March 26, 2012, 11:55:01 pm »
You need professional edition or greater for
- 64bit
- source code to the library's
- mac/iphone support
Starter edition doesn't have those.

Totally agree about Delphi 7, i'm still using that even though i bought Delphi XE
Greek letter 'Psi' (not Pounds per Square Inch)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf