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

0 Members and 1 Guest are viewing this topic.

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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...
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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: 8275
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.
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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: 652
  • Country: au
  • You do nothing, you get nothing.
    • Books, services and more:
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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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...
 

Online westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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...
 

Online westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • 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);
  }
}
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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...
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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 PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • 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...
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • 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: 146
  • 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!

 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • 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)
 

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Re: Arduino Serial Read issue.
« Reply #25 on: March 27, 2012, 04:38:42 am »
Well, it looks like i may be a Delphi convert. Installed EX2 and it feels pretty good so far. The GUI is close to .NET.
Built a small form application and am impressed by there small footprint on the system. A lot smaller than any .NET based application.

Now i get to learn a whole new language. ;D ;D
Delphi looks like it wont be too hard to get my head around.

Thanks to Psi for pointing me in the right direction.

Regards
Testing one two three...
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #26 on: March 27, 2012, 05:05:27 am »
Well, it looks like i may be a Delphi convert. Installed EX2 and it feels pretty good so far. The GUI is close to .NET.
Built a small form application and am impressed by there small footprint on the system. A lot smaller than any .NET based application.

Now i get to learn a whole new language. ;D ;D
Delphi looks like it wont be too hard to get my head around.

Thanks to Psi for pointing me in the right direction.

Regards

No problem :)

There's an option which sets if you want your application forms to be separate windows or part of delphi IDE.  (While developing)
The default on Delphi XE1 is to be part of the IDE (which i hate).
If you want to change it and can't figure out how let me know and i'll find the option.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Arduino Serial Read issue.
« Reply #27 on: March 27, 2012, 05:07:49 am »
Perhaps also "REAL Studio" (http://www.realsoftware.com/realstudio/ ) ?  I haven't used either it or VS enough to compare them, but it is a cross-platform "gui" style development system.  IIRC, it's BASIC, though the current web page looks like they're trying to de-emphasize that...
 

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Re: Arduino Serial Read issue.
« Reply #28 on: March 27, 2012, 06:24:27 am »
I looked at Real Studio a while back. It only took a couple of hours before i uninstalled it.

I have been looking at Delphi this afternoon and dont seem to be able to find a control for Serial Port communication.
Does Delphi have a native control or do people usually roll there own?

Regards
Testing one two three...
 

Offline Teknotronix

  • Regular Contributor
  • *
  • Posts: 146
  • Country: au
Re: Arduino Serial Read issue.
« Reply #29 on: March 27, 2012, 06:48:08 am »
I looked at Real Studio a while back. It only took a couple of hours before i uninstalled it.

I have been looking at Delphi this afternoon and dont seem to be able to find a control for Serial Port communication.
Does Delphi have a native control or do people usually roll there own?

Regards

http://sourceforge.net/projects/comport/
Don't drone me bro!

 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9951
  • Country: nz
Re: Arduino Serial Read issue.
« Reply #30 on: March 27, 2012, 07:07:48 am »
I have been looking at Delphi this afternoon and dont seem to be able to find a control for Serial Port communication.
Does Delphi have a native control or do people usually roll there own?
http://sourceforge.net/projects/comport/

yep, that's the one.  Tcomport is what i use as well.

I just installed it into Delphi XE1 right now  (i usually uses Delphi 7)
This should also work for Delphi XE2

Tcomport is a bit strange in its installation compared to other Delphi components.
It has a designetime and runtime package to install. but its not hard to do.


Here is how i installed it.

Download the TComport component and extract the zip to a location where you want to store 3rd party components. It's best if this isn't part of the Delphi directory.   i use d:\profile\programming\delphicomponents\tcomport\   


Installing the runtime package
- file - open - cportlibdxe.dpk  (its in the source folder of the stuff you unzipped)
- Rightclick on the CPortLibDXE.bpl entry in the delphi project manager and select compile
- Now you need to add that 'source' folder to the delphi library source list
   - click - tools - options
   - Select Environment options - delphi options - library
   - Click the button for library path
   - Click the yellow folder icon and select the source folder for Tcomport
   - Click Add and then ok

Installing the designtime package
- Click File - Open DsgnCPortDXE.dpk
- Rightclick on the DsgnCPortDXE.BPL entry in the project manager and select COMPILE
- Now Rightclick on the DsgnCPortDXE.BPL entry again and select INSTALL
- Your finished. click - File - Close all (don't have to save)

Now you should have the components installed.
You can create a new project and the Tcomport components should be in the component pallet.

Project - add new project - VCL forms application

-Drop a Tcomport on your form
-Set its comport to com1 (or whatever) and the bitrate to what you want.
-Set its connected property to true.
-Add a button that does   
Code: [Select]
comport1.transmitchar(  chr(50) );   chr(50) just converts 50 from a number to a character
There are a few other ways to send data in buffers and stuff for more advanced applications but TransmitChar() is good to just get stuff working quickly.

Run your program and when you click the button it should send one byte over the serial port


To read data you can use the RxChar event for Tcomport.

an event handler might look like this

Code: [Select]
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
   Str: String;
begin
   ComPort1.ReadStr(Str, Count); // read count number of bytes into the string str
   memo1.text := memo1.text + str;  // append data to a Tmemo. (You might want to convert it to hex though otherwise any ascii commands like
                                                     // tab or new line etc.. will cause that to happen on the memo.
end;
« Last Edit: March 27, 2012, 07:21:10 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Teknotronix

  • Regular Contributor
  • *
  • Posts: 146
  • Country: au
Re: Arduino Serial Read issue.
« Reply #31 on: March 27, 2012, 07:16:13 am »
Great to see some delphi stuff on here.

A tip for the beginners looking at this thread. Unlike some other languages like C#, there are two features (among many) that I find very powerful for writing nice clean and readable code. They are "with" statements and nested functions. Try them out, they are great.
Don't drone me bro!

 

Offline PeterGTopic starter

  • Frequent Contributor
  • **
  • Posts: 831
  • Country: au
Re: Arduino Serial Read issue.
« Reply #32 on: March 27, 2012, 07:27:33 am »
Great to see some delphi stuff on here.

A tip for the beginners looking at this thread. Unlike some other languages like C#, there are two features (among many) that I find very powerful for writing nice clean and readable code. They are "with" statements and nested functions. Try them out, they are great.

now that i am starting out with Delphi, i can guarantee there will be more Delphi threads on here for a while at least... ;D ;D ;D

Thanks for the help people.

Regards
Testing one two three...
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf