Go Down

Topic: Arduino and Delphi - Serial communication problems ? (Read 4 times) previous topic - next topic

axxel

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.

PaulS

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

Code: [Select]
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?

axxel

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.



mmcp42

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

in Delphi
numero := numero + 1;
there are only 10 types of people
them that understands binary
and them that doesn't

mmcp42

if I understand your code correctly, you are looking for #13 (CR) as a line terminator
try looking for #10 (LF) instead
there are only 10 types of people
them that understands binary
and them that doesn't

axxel

#5
Jun 15, 2011, 01:14 am Last Edit: Jun 15, 2011, 01:16 am by axxel Reason: 1
Hi mmcp42  :)

Thanks for your answer  8)

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...

mmcp42


Hi mmcp42  :)

Thanks for your answer  8)

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: [Select]
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 :)
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: [Select]
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
there are only 10 types of people
them that understands binary
and them that doesn't

axxel

Hi mmcp42 :) !

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

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 :)

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


;)

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)  :0

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


Code: [Select]
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:




mmcp42

#8
Jun 16, 2011, 05:43 pm Last Edit: Jun 16, 2011, 05:45 pm by mmcp42 Reason: 1
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!
there are only 10 types of people
them that understands binary
and them that doesn't

axxel

Hi MMCP42  :)

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 :)

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

Best regards.

Axxel.

tkbyd

#10
Jul 10, 2011, 02:59 pm Last Edit: Jul 10, 2011, 03:01 pm by tkbyd Reason: 1
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


Go Up