• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1314
  • Last Modified:

Comunication with PC and MCU over RS232.

My APP consist of two edit boxes:
Edit1Read for TX (transfer data from variable "outMCU") and
Edit2Write for RX (receive data into variable " inMCU").

Whats the way to approach to this problem?

0
pr2501
Asked:
pr2501
  • 27
  • 8
  • 8
1 Solution
 
MerijnBSr. Software EngineerCommented:
you should give a proper description of your problem, I have no idea what this is about.

What are the variables outMCU and inMCU, what are the edit boxes for, where are you using RS232, what is the problem?
0
 
pr2501Author Commented:
outMCU and inMCU are two variables in MCU.

Edit1 box is for showing data from MCU  and Edit2 is for sending data to  MCU.

MyAPP should use RS232 protocol for communication between PC and MCU.

I can't do that.
0
 
MerijnBSr. Software EngineerCommented:
RS232 is not a protocol, but a transport mechanism.

There is a good, easy and opensource RS232 driver here: http://sourceforge.net/projects/comport/
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
pr2501Author Commented:
It's some time that i had not worked with Turbo Delphi.

Pleas, explain me how to add libraries to all other Delphi libraries
and how add TComport object to Tool palete.
0
 
pr2501Author Commented:
  I have Turbo delphi.

 While trying to install com port components i get next error:

This version is not licensed to  load third-parity components or IDE addins.
0
 
Ustin19Commented:
need protocol of MCU data exchange for writing simple code.
Other way:
uses OoMisc, AdPort;
procedure doit;
var rs:TApdComPort
Begin
 rs:=TApdComPort.create(nil);
 try
 rs.ComNumber:=1;//com number
rs.Baud:=19200;//baud rate
rs.Open:=true;
rs.PutString('Hello, MCU, love Y');
...read help for asyncpro suite, http://sourceforge.net/projects/tpapro/
 finally
 rs.destroy;
 end;//try
end;
0
 
pr2501Author Commented:
Ustin19
Thank you:

I'LL begin to work on it this evening.
0
 
pr2501Author Commented:
To install TurboPower APRO into your IDE, take the following steps:

  1. Unzip the release files into a directory (e.g., d:\apro).

  2. Start Delphi or C++Builder.

  3. Add the source subdirectory (e.g., d:\apro\source) to the IDE's
     library path.

  4. Open & compile the runtime package specific to the IDE being
     used.

  5. Open & install the designtime package specific to the IDE being
     used. The IDE should notify you the components have been
     installed.




I have problem at point 3.
What must i write into atached picture in the Library path field if my files are located at
C:\Program Files\AsyncPro\source?
path.bmp
0
 
pr2501Author Commented:

And it looks next problem(obstacle) still remains.

This version is not licensed to  load third-parity components or IDE addins.
0
 
pr2501Author Commented:
Another thing way this this downloaded packet is exe file?
19exe.bmp
0
 
Ustin19Commented:
IDE is not licensed, but you CAN write
 uses somethirdpartyfile
and create\destroy components in runtime (as in my examle above)
0
 
pr2501Author Commented:

After adding of OoMisc, AdPort (pas) to my project i get next error:
[Pascal Fatal Error] OoMisc.pas(42): F1026 File not found: 'AWDEFINE.INC'

Where do i get missed file and where to copy it?



 
{Global defines potentially affecting this unit}
{$I AWDEFINE.INC}

Open in new window

0
 
pr2501Author Commented:

I got it. It is in:
C:\Program Files\Borland\BDS\4.0\source

What must i do to resolve this problem?
0
 
pr2501Author Commented:
Forget my last question.
At my home PC everething compiles fine.

Now can yo pleas write me code for  simple comunication betwin PC and MCU.


  Just a form with two tedits.
One for transmit  (after a button is pushed) and other for reading 3 ASCII characters when they come from MCU?

0
 
Ustin19Commented:
it is NO WAY to write universal code sample, which will work with all devices :)
So, i need mcu datasheet (at least, good is to use sample unit in lab) for do this sample. AFAIR, OnData event represents the RX data coming event, putstring\putchar methods invokes TX wire to send data. MCU can use any set of signals (fe DRS, RI etc) wires state up or down before sending\receiving data, this is depending ONLY FROM MCU. Read datasheet and all will be all right :)
0
 
pr2501Author Commented:
Thank you very much.

1.
It is that i have done all this work in the past with PIC MCU (assembler) and with Java.
And there were more that a  year of hard work.

2.
Now i'm on ATmega8535 (Pascal) and Delphi.

In my school times(point1) i used first to communicate with Hyper terminal (windows).
And in my case have done all the work MCU (it sends characters and recives them (elaborate data) by
MCU code).

Now i have intention to do it in same way.
I'm able to send characters itch 3 seconds from mcu and see them in Hyper terminal  (communication program).
And if i type  a character into Hype terminal   my mcu can switch an output on.

First:
My Delphi app has to
- constantly check if character over the  serial com  port has arrived
- or to recognize an interrupt in that case.

Second:
Just to send data forward when it get it in specific buffer (or when  specific variable is different of initial value).
0
 
Ustin19Commented:
so, try use putstring to send smth and handle OnTrigger\OnTriggerAvail event and use .GetBlock method to retrive data (in prev post it was incorrectly written method names, sorry)
0
 
pr2501Author Commented:
ok,

 need some time, let you know
0
 
pr2501Author Commented:
I need more time to make my MCU work appropriately.
(i still don't get right  measured values(i'm observing them in Hyper terminal))

I could go on Delphi but then i'll have to turn back.
0
 
pr2501Author Commented:
I can send data now.
And i'll proceed with looking for procedures for reading.






0
 
Ustin19Commented:
MCU does answer you?
To read data use  .getchar\.getblock methods in OnTriggerAvail event
0
 
pr2501Author Commented:
Next code i use for sending data

var
  Form1: TForm1;
         sendCharacter:string;
         buffer:string[255];
 
implementation
 
{$R *.dfm}
 procedure doit;
var rs:TApdComPort;
Begin
 rs:=TApdComPort.create(nil);
 try
 rs.ComNumber:=1;//com number
rs.Baud:=4800;//baud rate
rs.Open:=true;
rs.PutString(sendCharacter);
 
 finally
 rs.destroy;
 end;//try
end;
 
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
 sendCharacter:='1';
 doit;
end;
 
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  sendCharacter:='0';
 doit;
end;
 
 
 
 
end.

Open in new window

0
 
pr2501Author Commented:
But can't  insert next:

   procedure Tform1.TriggerAvail (CP:TObject;Count:Word);
   var
   I:word;
   C:char;
   begin
   for  I:= 1 to count do begin
     c:= ApdcomPort.getChar;
   end;
   if(pos('x',buffer)>0) then
   caption:='x';
   end;

Open in new window

0
 
pr2501Author Commented:
I forgot to insert next line in code before:
Bufer:= Bufer+c;
 procedure Tform1.TriggerAvail (CP:TObject;Count:Word);
   var
   I:word;
   C:char;
   begin
   for  I:= 1 to count do begin
     c:= ApdcomPort.getChar;
   end;
Bufer:= Bufer+c;
   if(pos('x',buffer)>0) then
   caption:='x';
   end;

Open in new window

0
 
pr2501Author Commented:


I have got picture how to  create\destroy components in runtime , but i can't work with events
0
 
MerijnBSr. Software EngineerCommented:
can you be more specific what you need help with?
0
 
pr2501Author Commented:
After :
var rs:TApdComPort;
and:
rs:=TApdComPort.create(nil);
properties can be asignet to "rs":
rs.ComNumber:=1;//

But how do i deal with events(onTriggerAvail) of ApdcomPort component?




0
 
MerijnBSr. Software EngineerCommented:
first you have to make a event handler with the right arguments. For that, find in the sourcefile the declaration.

I don't use this comport object, so this is only an example, it's not correct:
procedure TForm1.OnTriggerAvail(Sender: TObject; Data; string); // an example, probably not correct!
begin
 // ..
end;
 
procedure TForm1.FormCreate();
var rs:TApdComPort;
begin
 rs:=TApdComPort.create(nil);
 rs.ComNumber:=1;//
 rs.OnTriggerAvail := OnTriggerAvail;

Open in new window

0
 
Ustin19Commented:
so easy:

type TOnTriggerAvail=procedure(Sender: TObject; Data; string);

procedure myOnTriggerAvail(Sender: TObject; Data; string);
Begin
//do smth;
end;
...

yuor:
var rs:TApdComPort;
and:
rs:=TApdComPort.create(nil);
properties can be asignet to "rs":
rs.ComNumber:=1;//
rs.OnTriggerAvail:=myOnTriggerAvail;

0
 
Ustin19Commented:
my private email is z642d@freemail.ru and i will read it periodically (now i have been deportated to russia with very silly internet)
0
 
Ustin19Commented:
and probably (if my solution will not work) you need try rewrite declaration of NOnTriggerAvail as
type TOnTriggerAvail=procedure(Sender: TObject; Data; string) of object;
0
 
MerijnBSr. Software EngineerCommented:
> type TOnTriggerAvail=procedure(Sender: TObject; Data; string) of object;

you almost never have to do this if you use a component, only if you are writing one
0
 
pr2501Author Commented:
Today i had no time.
I'LL go on at evening or tomorow.
0
 
pr2501Author Commented:

It says:

[Pascal Error] Unit1.pas(49): E2009 Incompatible types: 'method pointer and regular procedure'

for next line:

rs.OnTriggerAvail:=myOnTriggerAvail;
0
 
pr2501Author Commented:

I supose if there is posible to manupolate (create and destroy) with components it must be
posible to work with events.
0
 
pr2501Author Commented:
"first you have to make a event handler with the right arguments. For that, find in the sourcefile the declaration."

Is the next code declaration which  i should look for?
 {Trigger events}
 
    property OnTriggerAvail : TTriggerAvailEvent
      read FOnTriggerAvail write SetOnTriggerAvail;

Open in new window

0
 
MerijnBSr. Software EngineerCommented:
yes, now look what TTriggerAvailEvent looks like, find the declaration.
0
 
pr2501Author Commented:
{Specific trigger event handlers}
  TTriggerAvailEvent = procedure(CP : TObject; Count : Word) of object;


If is this what i need,
pleas tich me how to use it (and that is for all what must be done if i wont to use events).
0
 
MerijnBSr. Software EngineerCommented:
ok, this is asuming you use the component from a from called TForm1
// in your private section add
 procedure OnTriggerAvail(CP: TObject; Count: word);
 
 fADPComport: TApdComPort;
 
 
// ...
 
implementation
 
// also create this procedure in the implementation part (or press ctr+alt+c)
procedure TForm.OnTriggerAvail(CP: TObject; Count: word);
begin
 // do what you need here
end;
 
 
// in your formcreate, create the object and tie the event to it
procedure TForm1.FormCreate();
var 
begin
 fADPComport: = TApdComPort.create(nil);
 fADPComport.ComNumber : =1;
 fADPComport.OnTriggerAvail := OnTriggerAvail;
 // ...
end;
 
// don't forget to free it in the form destry
procedure TForm1.FormDestroy();
begin
 fADPComport.Free();
end;

Open in new window

0
 
pr2501Author Commented:

Thank you very much

Just another thing:
because i'm receiving   more characters not only one c  into TEdit.
How to  make next work:  edit1.Text:= ccc;

for i:=1 to count  do begin
  c:=  fADPComport.Getchar;

// ccc:=ccc and c;

end;

procedure TForm1.OnTriggerAvail(CP: TObject; Count: word);
var
i:word;
c:char;
begin
for i:=1 to count  do begin
  c:=  fADPComport.Getchar;
end;
edit1.Text:= c;
end;

Open in new window

0
 
pr2501Author Commented:



ccc:=CCC+c;
0
 
MerijnBSr. Software EngineerCommented:
yes, that's it. Although it seems logical that the component has a method which gives you all data at once as a string.
0
 
pr2501Author Commented:
jes,

latly i'll surely find it

thank you again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 27
  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now