Pages: [1]   Go Down
Author Topic: Arduino and Delphi - Serial communication problems ?  (Read 3688 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody!

Sorry for my English, I'm French...

I am trying to make a communication with Delphi and Arduino exactly like this stuff : http://www.thepenguincult.com/proyectos/arduino-delphi-lm35/

The future objective is to read the value of a sensor and visualize it with a program made with delphi using GLScene (experiment of visuals).

I made it simple to try it, you will have only to connect the arduino to the PC, without have to connect any component to the arduino.

The objective is to send a variable to the PC with the SERIAL/USB default interface included to the arduino.



Here, I made this program :



int numero;

void setup() {
  Serial.begin(9600);
  numero=0;
 }

void loop() {
  Serial.println(numero);
  delay(100);
  numero=numero++;
}




We send to the serial port each 100 millisecond a number which will increase.

I found this application of a serial terminal (You have to put your arduino into COM1 or COM2) and I added a line to recuperate the variable sent by the arduino into the caption of the form.

I have a result totally crasy, it's look like the program recieve varios packages... without the final "enter" del printLN...

I need to have 1 and 2 and 3 etc .... but not 12345 and 67891011 and 1213141516 ... into my form caption...

I need these variables separated correctly.



You will need to add the dclsockets package to the IDE.

To do this go to Component | Install Packages | Add (/bin/dclsockets70.bpl).

Here you can download this program delphi : http://www.megaupload.com/?d=TVOCY22U

Now, I've tried to make it with an other type of Arduino Program :




int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision

int i;
int n;

void setup()
{
  Serial.begin(9600); // start serial communication
}

void loop()
{
 
 
for(i = 0;i<=7;i++){ // gets 8 samples of temperature
 
 // samples = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
 // tempc = tempc + samples;
  delay(250);

}

tempc = tempc/8.0; // better precision

n = n + 1;

Serial.print(n,DEC);

tempc = 0;

delay(250); // delay before loop
}


This program is a modified version of this project : http://www.thepenguincult.com/proyectos/arduino-delphi-lm35/

What's the difference ? There is a boucle 0 to 7 before with a delay of 250 msec into.

With that, its works ... When I reduce the size of the boucle, it starts to don't respect the data an I have totally incoherent variables...

I don't understand what heppen exactly... I'm working with Windows 7 64 bits, i tried with an XP 32 bits, and I have the sames problems...

I tried to build that http://www.thepenguincult.com/proyectos/arduino-delphi-lm35/ but I recieve packets incorrect too.

I tried with TComPort, TpaPro too and I'm going to the same issues...

The arduino I use is a Atmega168 and it is a clone MC-NOVE multicomp.

I think the problem is my arduino-clone board or this is the program of the arduino (synchronisation ? internal clock-time problems ? What resources use the serial communicator ?)

Thanks for your help.

Best regards.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure what your problem is, but I'd try increasing the baud rate. 9600 is pretty slow.

Code:
numero=numero++;
The numero++ statement is equivalent to numero = numero + 1, so your statement is equivalent to numero = numero = numero + 1. Is that really what you want?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

Thanks for your answer.

In delphi, to make an increment of 1 we can write that :

numero := numero + 1;

With arduino (or C), this is it : numero=numero++;

But numero = numero + 1; works too ... What's the difference ? I don't understand this very....

But i don't think the problem is here.

I proved at 56000 K too and I got the same problem.

I receive one variable with 12345, an other of 67891011 , .....

What i want is receive 1, next time 2 , next 3 etc etc etc ...

What I want exactly is for example all the 500 Msec, the arduino send an integer number to my serial port, and be able to read it coorectly wity my delphi application.


Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

in C++
numero++; // this will add 1 to numero all by itself
numero = numero + 1; // does the same

in Delphi
numero := numero + 1;
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if I understand your code correctly, you are looking for #13 (CR) as a line terminator
try looking for #10 (LF) instead
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi mmcp42  smiley

Thanks for your answer  smiley-cool

It is not exactly this thing I'm looking for.

The thing I'm looking is the following :

Into the loop, for example each 200 millisecond, I want to send with the serial-port an integer variable (numero), using this code :

int numero;

void setup() {
  Serial.begin(9600);
  numero=0;
 }

void loop() {
  Serial.println(numero);
  delay(200);
  numero=numero++;
}


Normally, the arduino will send each 200msec the value of the number isn't it ? (I checked it with the arduino serial monitor).

0
(nothing during 200 msec)
1
(nothing during 200 msec)
2
(nothing during 200 msec)
3
(nothing during 200 msec)
etc ........

Now, with Delphi, using the ComPort component, I write this code :

procedure TMainForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
  Str: String;
begin

  ComPort1.ReadStr(Str, Count);
  label1.Caption:=Str;

end;



Now, each 200 msec, I have this value for STR :

0123
(nothing during 200 msec)
45678
(nothing during 200 msec)
910111213
(nothing during 200 msec)
1415
(nothing during 200 msec)
etc ........

I don't understand what happen because with this example http://www.thepenguincult.com/proyectos/arduino-delphi-lm35/ , it's looks to work well but with my arduino, I have the same problems (I used it with a Win 7 64 Bits and and XP 32 Bits and I get the same bad issues...)

I'm thinking this is the arduino I use because this is an unofficial... I don't have any idea of what happening...
« Last Edit: June 14, 2011, 06:16:16 pm by axxel » Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi mmcp42  smiley

Thanks for your answer  smiley-cool

It is not exactly this thing I'm looking for.

The thing I'm looking is the following :

Into the loop, for example each 200 millisecond, I want to send with the serial-port an integer variable (numero), using this code :

well first off
put the code inside a code box in between #

Code:
int numero;

void setup() {
  Serial.begin(9600);
  numero=0;
 }

void loop() {
  Serial.println(numero);
  delay(200);
  numero++;
}
now we can read it without it taking up too much space smiley
you'll see I also fixed your numero++ "buglet"

Normally, the arduino will send each 200msec the value of the number isn't it ? (I checked it with the arduino serial monitor).

0
(nothing during 200 msec)
1
(nothing during 200 msec)
2
(nothing during 200 msec)
3
(nothing during 200 msec)
etc ........

if it works with the serial monitor, then
a) the Arduino is working correctly
b) the sketch is working correctly
c) the problem must be in the Delphi code

Now, with Delphi, using the ComPort component, I write this code :

Code:
procedure TMainForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
  Str: String;
begin

  ComPort1.ReadStr(Str, Count);
  label1.Caption:=Str;

end;
your code is simply dumping the results of each read into your label1
the example appends it to memo.text

you see the difference!


Now, each 200 msec, I have this value for STR :

0123
(nothing during 200 msec)
45678
(nothing during 200 msec)
910111213
(nothing during 200 msec)
1415
(nothing during 200 msec)
etc ........

I don't understand what happen because with this example http://www.thepenguincult.com/proyectos/arduino-delphi-lm35/ , it's looks to work well but with my arduino, I have the same problems (I used it with a Win 7 64 Bits and and XP 32 Bits and I get the same bad issues...)

I'm thinking this is the arduino I use because this is an unofficial... I don't have any idea of what happening...

memo objects can handle newline characters
label objects cannot
so any newlines will be discarded (do you see funny "blobs" sometimes?)

hope that helps
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi mmcp42 smiley !

If you were from Barcelona, I invite you to drink a good beer with pleasure to talk of this stuff with you  smiley

Quote
if it works with the serial monitor, then
a) the Arduino is working correctly
b) the sketch is working correctly
c) the problem must be in the Delphi code

I'm totally agree with you - It's logical smiley

Quote
now we can read it without it taking up too much space
you'll see I also fixed your numero++ "buglet"

 smiley-wink

Now, with this example, it's works fine with delphi, exactly like the serial monitor of the Arduino Program.



Here is the sample delphi program...  smiley-roll

http://www.megaupload.com/?d=O8QJ1HBP

Into the TMemo, everything is perfect. But sometimes I have some "glitches"  so any newlines are discarded (and I funny "blobs" sometimes too)  smiley-mad

Now I don't understand the "logic" very well of this system....


Code:
0

(nothing during 200 msec)

the STR value should be "0"+CHR13

1

(nothing during 200 msec)

the STR value should be "1"+CHR13

2

etc ........


What I want to do exactly is to have into my delphi program a variable of type Integer with the content of the ultimate result sended by the arduino, without using the TMemo, because it will keep all these datas into the RAM of the computer and saturate it. What I want to do looks like a straming.

I thinks this source-code dosen't works too ?

http://www.thepenguincult.com/proyectos/arduino-delphi-lm35/

Thxxx a lot for these answers  smiley-yell



Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what you will have to do is what TMemo does
put each character into a buffer as it arrives from the com port
scan the buffer looking for newline ( chr(10) or chr(13) )
anything before the newline is the characters you extract
remove them from the buffer and convert to an integer

PS the sample code is terrible in just so many ways!
« Last Edit: June 16, 2011, 10:45:31 am by mmcp42 » Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi MMCP42  smiley

I've done it, thanks a lot for your help !

Have a look here, this is my awful source-code but it works very well smiley

http://arduino.cc/forum/index.php/topic,65721.0.html

Best regards.

Axxel.
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For more on Arduino-PC comms, with the original Pascal based Delphi, see the following... which also has many general points in it...

http://sheepdogguides.com/arduino/art4SeIntro.htm

« Last Edit: July 10, 2011, 08:01:06 am by tkbyd » Logged

Pages: [1]   Go Up
Jump to: