Arduino Serial Read issue. - Page 1
Forum section: electronics / general

author: PeterG
author_rank: Frequent Contributor
timestamp: March 23, 2012, 07:52:55 am
content: 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

author: Psi
author_rank: Super Contributor
timestamp: March 23, 2012, 08:39:55 am
content: 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 127You want an 'unsigned' byte, which can store between 0 and 2552) 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

author: PeterG
author_rank: Frequent Contributor
timestamp: March 23, 2012, 08:57:41 am
content: This is the code i am using at the moment.Code:if (Serial.available() >1) {byte inByte = Serial.read(); //According to the Aruino docs, a byte is unsigned. 0-255byte 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

author: amyk
author_rank: Super Contributor
timestamp: March 23, 2012, 09:20:40 am
content: Quote from: Psi on March 23, 2012, 08:39:55 amI second this, given thedocumentationdoesn't say anything about the port parameters other than baud rate despite the MCU's serial port having the capability for that.

author: Psi
author_rank: Super Contributor
timestamp: March 23, 2012, 09:48:07 am
content: 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.

author: PeterG
author_rank: Frequent Contributor
timestamp: March 23, 2012, 10:07:22 am
content: 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

author: johnboxall
author_rank: Unknown
timestamp: March 23, 2012, 10:37:16 am
content: Quote from: PeterG on March 23, 2012, 08:57:41 amTry changing the first line toif (Serial.available() >0) {

author: harnon
author_rank: Regular Contributor
timestamp: March 23, 2012, 02:04:32 pm
content: Quote from: tronixstuff on March 23, 2012, 10:37:16 amThat may cause issues when he calls "DataIn = Serial.read()". If there isn't a second byte available it will return -1Quote from: ArduinoDocs

author: harnon
author_rank: Regular Contributor
timestamp: March 23, 2012, 02:08:00 pm
content: 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 atCode: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 canhttp://arduino.cc/en/Reference/LiquidCrystalPrint

author: PeterG
author_rank: Frequent Contributor
timestamp: March 23, 2012, 11:00:16 pm
content: 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

author: harnon
author_rank: Regular Contributor
timestamp: March 24, 2012, 11:19:33 am
content: 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?

author: PeterG
author_rank: Frequent Contributor
timestamp: March 25, 2012, 01:58:00 am
content: 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

author: westfw
author_rank: Super Contributor
timestamp: March 25, 2012, 04:43:47 am
content: 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...

author: PeterG
author_rank: Frequent Contributor
timestamp: March 25, 2012, 06:00:45 am
content: Quote from: westfw on March 25, 2012, 04:43:47 amThat 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

author: westfw
author_rank: Super Contributor
timestamp: March 25, 2012, 07:52:39 am
content: >> 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);}}

author: Psi
author_rank: Super Contributor
timestamp: March 25, 2012, 08:32:19 am
content: Quote115200 is quite fast, you might be getting errors, try using 9600 (remember to change it on both the micro and the PC)QuoteJust out of interest, what happens if you code it to print the numbers on the LCD as decimallcd.print(datain,DEC);also try the slightly different println functionSerial.println(datain, DEC);you could also try HEXin VBCode: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?

author: PeterG
author_rank: Frequent Contributor
timestamp: March 25, 2012, 10:20:48 pm
content: 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

author: harnon
author_rank: Regular Contributor
timestamp: March 25, 2012, 10:43:28 pm
content: Quote from: PeterG on March 25, 2012, 10:20:48 pmHmm... 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 codeCode:#include <stdio.h>intmain (void){int i;while ((i = getchar()) != EOF){unsigned char c = i;printf ("read c = '%c'\n", c);}return 0;}taken fromhere. (Not so much the getchar(), but the cast from int to unsigned char)

author: PeterG
author_rank: Frequent Contributor
timestamp: March 26, 2012, 07:38:59 am
content: 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

author: Psi
author_rank: Super Contributor
timestamp: March 26, 2012, 11:24:24 am
content: Quote from: PeterG on March 26, 2012, 07:38:59 amReally, that's the most stupid thing ever.Makes me glad i use Delphi and not VBDid switching to an array fix your issues?

author: PeterG
author_rank: Frequent Contributor
timestamp: March 26, 2012, 11:31:26 am
content: 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

author: PeterG
author_rank: Frequent Contributor
timestamp: March 26, 2012, 11:34:12 am
content: 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

author: Psi
author_rank: Super Contributor
timestamp: March 26, 2012, 11:51:46 am
content: 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)

author: Teknotronix
author_rank: Regular Contributor
timestamp: March 26, 2012, 12:17:17 pm
content: 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.

author: Psi
author_rank: Super Contributor
timestamp: March 26, 2012, 11:55:01 pm
content: You need professional edition or greater for- 64bit- source code to the library's- mac/iphone supportStarter edition doesn't have those.Totally agree about Delphi 7, i'm still using that even though i bought Delphi XE

author: PeterG
author_rank: Frequent Contributor
timestamp: March 27, 2012, 04:38:42 am
content: 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.Delphi looks like it wont be too hard to get my head around.Thanks to Psi for pointing me in the right direction.Regards

author: Psi
author_rank: Super Contributor
timestamp: March 27, 2012, 05:05:27 am
content: Quote from: PeterG on March 27, 2012, 04:38:42 amNo problemThere'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.

author: westfw
author_rank: Super Contributor
timestamp: March 27, 2012, 05:07:49 am
content: 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...

author: PeterG
author_rank: Frequent Contributor
timestamp: March 27, 2012, 06:24:27 am
content: 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

author: Teknotronix
author_rank: Regular Contributor
timestamp: March 27, 2012, 06:48:08 am
content: Quote from: PeterG on March 27, 2012, 06:24:27 amhttp://sourceforge.net/projects/comport/

author: Psi
author_rank: Super Contributor
timestamp: March 27, 2012, 07:07:48 am
content: Quote from: Teknotronix on March 27, 2012, 06:48:08 amyep, 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 XE2Tcomport 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 okInstalling 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 doesCode:comport1.transmitchar( chr(50) );chr(50) just converts 50 from a number to a characterThere 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 portTo read data you can use the RxChar event for Tcomport.an event handler might look like thisCode:procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);varStr: String;beginComPort1.ReadStr(Str, Count); // read count number of bytes into the string strmemo1.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;

author: Teknotronix
author_rank: Regular Contributor
timestamp: March 27, 2012, 07:16:13 am
content: 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.

author: PeterG
author_rank: Frequent Contributor
timestamp: March 27, 2012, 07:27:33 am
content: Quote from: Teknotronix on March 27, 2012, 07:16:13 amnow that i am starting out with Delphi, i can guarantee there will be more Delphi threads on here for a while at least...Thanks for the help people.Regards