Solved

Comunication with PC  and MCU over RS232.

Posted on 2008-06-12
43
1,211 Views
Last Modified: 2011-10-19
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
Comment
Question by:pr2501
  • 27
  • 8
  • 8
43 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 21772349
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
 

Author Comment

by:pr2501
ID: 21780985
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 21781772
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
 

Author Comment

by:pr2501
ID: 21786695
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
 

Author Comment

by:pr2501
ID: 21828811
  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
 
LVL 2

Expert Comment

by:Ustin19
ID: 21837236
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
 

Author Comment

by:pr2501
ID: 21873263
Ustin19
Thank you:

I'LL begin to work on it this evening.
0
 

Author Comment

by:pr2501
ID: 21879583
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
 

Author Comment

by:pr2501
ID: 21879626

And it looks next problem(obstacle) still remains.

This version is not licensed to  load third-parity components or IDE addins.
0
 

Author Comment

by:pr2501
ID: 21881080
Another thing way this this downloaded packet is exe file?
19exe.bmp
0
 
LVL 2

Expert Comment

by:Ustin19
ID: 21881762
IDE is not licensed, but you CAN write
 uses somethirdpartyfile
and create\destroy components in runtime (as in my examle above)
0
 

Author Comment

by:pr2501
ID: 21883454

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
 

Author Comment

by:pr2501
ID: 21883571

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

What must i do to resolve this problem?
0
 

Author Comment

by:pr2501
ID: 21885536
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
 
LVL 2

Expert Comment

by:Ustin19
ID: 21892550
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
 

Author Comment

by:pr2501
ID: 21893401
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
 
LVL 2

Expert Comment

by:Ustin19
ID: 21896842
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
 

Author Comment

by:pr2501
ID: 21898757
ok,

 need some time, let you know
0
 

Author Comment

by:pr2501
ID: 21983381
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
 

Author Comment

by:pr2501
ID: 21990161
I can send data now.
And i'll proceed with looking for procedures for reading.






0
 
LVL 2

Expert Comment

by:Ustin19
ID: 21997369
MCU does answer you?
To read data use  .getchar\.getblock methods in OnTriggerAvail event
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:pr2501
ID: 22007943
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
 

Author Comment

by:pr2501
ID: 22007948
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
 

Author Comment

by:pr2501
ID: 22007988
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
 

Author Comment

by:pr2501
ID: 22010146


I have got picture how to  create\destroy components in runtime , but i can't work with events
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 22013838
can you be more specific what you need help with?
0
 

Author Comment

by:pr2501
ID: 22014273
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 22014409
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
 
LVL 2

Expert Comment

by:Ustin19
ID: 22015249
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
 
LVL 2

Expert Comment

by:Ustin19
ID: 22015260
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
 
LVL 2

Expert Comment

by:Ustin19
ID: 22015269
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 22015665
> 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
 

Author Comment

by:pr2501
ID: 22025362
Today i had no time.
I'LL go on at evening or tomorow.
0
 

Author Comment

by:pr2501
ID: 22037920

It says:

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

for next line:

rs.OnTriggerAvail:=myOnTriggerAvail;
0
 

Author Comment

by:pr2501
ID: 22086287

I supose if there is posible to manupolate (create and destroy) with components it must be
posible to work with events.
0
 

Author Comment

by:pr2501
ID: 22124757
"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
 
LVL 19

Expert Comment

by:MerijnB
ID: 22127469
yes, now look what TTriggerAvailEvent looks like, find the declaration.
0
 

Author Comment

by:pr2501
ID: 22127932
{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
 
LVL 19

Accepted Solution

by:
MerijnB earned 500 total points
ID: 22129046
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
 

Author Comment

by:pr2501
ID: 22148240

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
 

Author Comment

by:pr2501
ID: 22148759



ccc:=CCC+c;
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 22150217
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
 

Author Comment

by:pr2501
ID: 22151002
jes,

latly i'll surely find it

thank you again
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now