[SOLVED] Receiving values from serial port

Hello, I have little problem. On my arduino I made simple program, that reads values from analog pin (where is irda receiver) and via Serial I am sending it to PC. There I have simple application in Smalltalk, that reads these values and sets a color of one object to this value. It works but there is one error. From time to time it receives only part of value, for example "56 56 55 56 55 54 55 56 5 6 56 5 55...". Those one-digital values are truly false and are only part of real value. When it sends three-digital value, it is even worse. I have these setting in Squeak for serial port:

baudRate := 9600. "9600 baud" stopBitsType := 1. "one stop bit" parityType := 0. "no parity" dataBits := 8. "8 bits" outputFlowControlType := 0. "none" inputFlowControlType := 0. "none" xOnByte := 19. "ctrl-S" xOffByte := 24. "ctrl-X"

and reading from port is done one per 50 ms. On arduino is this simple program:

int ledPin = 13; int irValue = 0;

void setup() { pinMode(ledPin,OUTPUT); Serial.begin(9600); digitalWrite(ledPin,HIGH); }

void loop() { irValue = analogRead(0); Serial.println(irValue, DEC); }

and when I try to show received values in Arduino IDE, all values are right. Do you know, where would be an error?

There is no delay in your Arduino code (there are also no delimiters being sent...), so it is sending about 960 characters per second. If you're only looking at the serial line every 50ms, that's nearly 50 character's worth of delay. Can the Smalltalk side of things cope with that?

when I try to show received values in Arduino IDE, all values are right.

...suggests that there's nothing wrong with your Arduino code.

Forgive me if everyone already knows, but, where Groove says...

There is no delay in your Arduino code

... he is quite right! But don't worry... you don't have to delay between individual characters sent to the serial port. It is "clever", and buffers things... if you don't overwhelm it!

All that's needed is....

void loop()
{
 irValue = analogRead(0);
 Serial.println(irValue, DEC);
delay(500);//THIS LINE ONLY- new
}

The value will pass to the code in the serial library in an instant, and, behind the scenes will be "dribbled" (in computer-time terms) out over the serial port in the background.

The delay gives the serial software time to finish the above before the next value arrives for sending.

It is "clever", and buffers things

No, it isn't and it doesn't - there is no Tx buffer on the Arduino (well, no more than the hardware UART provides), so Serial.write is virtually synchronous. (the write method returns as soon as the last character to be sent is written to the transmit register).

However, if the PC can't buffer 50 characters, then there's a problem, but that's on the PC, not the Arduino.

If I put delay, it still doesn't work. Now it has same problem with receiving (time to time only last digit) but now it is even worse, because it is asynchronous, arduino not send and squeak not receives in the same time (only sometimes).

Forgive my ignorance, but what is "squeak"?

(BTW, you shouln't really have xon/xoff handshaking enabled)

With a name like that I had to look it up. http://squeak.org/

Oh. OK.

Since the problem doesn't lie with the Arduino, how about posting at the squeak forum?

Remember that when you use serial.printLN() you are also sendig a CR and a LF char with each value yuo send.

Unless squeak understands and reads that you will have problems.

Squeak is an implementation of Smalltalk programming language, partly "compiler", partly IDE, partly operation system. Based on the strict object oriented theorems. All is object. If I'am sending string "Hello world!" it times to times receive "orld! Hello w" and so one. But, maybe I have founded error. I have code like this (in my squeak):

readColor | s n | n := SerialPort new. n openPort: 7. s := n readString asInteger. Transcript show: s; cr. (s isNil) ifFalse: [s := s - 46 * (1/60).self color: (Color r:s g:s b:s)]. n close.

so I am opening and closing port every read. This may be the problem.

Ok, fine, if the problem lies in smalltalk code, I'll ask on some squeak forum. Thanks you all anyway.

I use a free application called PortMon to see just what is being sent and received on the PC com port. Might be a useful tool to use.

Wow, thanks, I have solved it. Now I am sending it from my arduino by Serial.print(irValue, BYTE); not by println and not by DEC. Now I have a simple morph (morph is object in Squeak, that is used for displaying. It is base for all GUI interfaces), a square that change its color from black to white by infared radiation that are striking on ifrared receiver connected to arduino. I'll use it for simple scanner, black color doesn't flash back irda ray and white does, so it will be the method for scanning black and white pictures. If someone wants code of receiving via serial port in squeak or if you want some videos, just tell me.