Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delphi Anyone??? Breaking Down Serial Transmission

Status
Not open for further replies.

crush

New Member
Hi All,
Recieving 5 bytes of information continually.The data being sent
serially is hex numbers, but when i see the data recieved it shows
characters. How do i change these characters back into integers?
i tried strtoint but it dit not work:

procedure TForm1.VaComm1RxChar(Sender: TObject; Count: Integer);
var
character: string;
begin
character:= VaComm1.ReadText;
Label1.Caption:=label1.Caption +character;
Label3.Caption:= strtoint(Label1.Caption); // gives me error of
imcompatible types
end;


I also want to seperate these 5 bytes.
first byte(actually a bit) is a start byte value 0, the 3 bytes that follow i want to
assign to 3 different variables, the last byte is a stop byte value 0.
how do i seperate these bytes?


The data being recieved is what i am transmitting from PIC16f877a
the transmiting code is
start =0; //declared as bits
stop=0;
TXREG = start; //transmission start bit
while(TRMT == 0){};
TXREG = id; //variable 1 which depicts the id i
require
while(TRMT == 0){};
TXREG = mux_number; //variable 2 depicting the mux number in use
while(TRMT == 0){};
TXREG = grid_number; //variable 3 depicting the grid being read
while(TRMT == 0){};
TXREG = stop; //transmits Stop bit
while(TRMT == 0){};


The goal is to display the id on a GUI relative to the physical setup.
I am a programming novice.
I have written most of the Delphi code such that i recieve 3 intereger
variables(as above), based on which i display the id. This part of my
code works, i have checked it by generating random numbers.


Now stuck with characters which has no start or end it seems! Any help
would be much appreciated.


Also,
VaComm1 allows for one stop bit should this be a 0 or a 1?
 
crush said:
Hi All,
Recieving 5 bytes of information continually.The data being sent
serially is hex numbers, but when i see the data recieved it shows
characters. How do i change these characters back into integers?
i tried strtoint but it dit not work:

procedure TForm1.VaComm1RxChar(Sender: TObject; Count: Integer);
var
character: string;
begin
character:= VaComm1.ReadText;
Label1.Caption:=label1.Caption +character;
Label3.Caption:= strtoint(Label1.Caption); // gives me error of
imcompatible types
end;

OK, point 1 is that you code wont work because you are converting Label1.caption to an INTEGER and trying to assign an INTEGER to a caption property which is a string type.

character:= VaComm1.ReadText;

Does the line above only ever receive 1 character at a time? If so then I can supply you some code for that, if not I can supply code for multiple characters - just let me know. Also please let me know what you are expecting to see in the labels and I will correct that too :)

Peter Dove
 
PeterDove said:
Does the line above only ever receive 1 character at a time? If so then I can supply you some code for that, if not I can supply code for multiple characters - just let me know. Also please let me know what you are expecting to see in the labels and I will correct that too :)

Peter Dove
Thanx Peter,

The above line recieves streaming Data. Cuz i am continously transmitting data of various id's I capture. They come in blocks of 5 at a time:
start-variable1, variable2, variable 3, stop (start and stop =1)
i am sure i am gettin this data as i have captured it in serial watcheras shown in the figure. i have marked the data bits in red.
But they dont come in blocks like that, they arrive in a stream. I have just got them one below the other in the pic to make it easier to show the information


I was just using Labels to see what i was getting from the com port. I do not want anthing displayed on a Label really.
The goal here is to assign the 3 data variables to variables in Delphi, such that i can use them in the code i have already developed
eg of use:
procedure TForm1.display_robot(muxid,grid_number,robot_id: integer);

where in i am hoping that i have somehow assigned variable1 to robot_id, variable2 to muxid and variable3 to grid_number. And i expect them to be integers as i am transmitting them as such.

To basically use them to display robot id's on the GUI (as shown in second figure)

Would appreciate any help very much, as i have been playin with Delphi since 9 am with this problem (itz now 9pm), but of no avail.
 

Attachments

  • Serial_Communication_grids.JPG
    Serial_Communication_grids.JPG
    55.6 KB · Views: 211
  • GUI.JPG
    GUI.JPG
    38.8 KB · Views: 200
Last edited:
I don't know delphi but it looks very similar to VB. In VB I would use the Asc function.

So, Label3.Caption:= asc(left(Label1.Caption,1));
would give the first byte in the label
and Label1.Caption:=mid(Label1.Caption,2);
would get rid of the first character so you can access the second.

Mike.
 
crush said:
Would appreciate any help very much, as i have been playin with Delphi since 9 am with this problem (itz now 9pm), but of no avail.

OK, so first of all you need to understand delphi strings..

character:= VaComm1.ReadText;

This you say is pulling in 5 bytes.. but of course they are characters..

character[1]
character[2]
etc is the way to access each character

Then you can do an ord on each one to get the integer value

var
intValue : Integer;
character : string;
begin
character:= VaComm1.ReadText;
Label3.Caption:= IntToStr( Ord(character[1]));
Label3.Caption:= Label3.Caption + ',' + IntToStr( Ord(character[2]));
Label3.Caption:= Label3.Caption + ',' + IntToStr( Ord(character[3]));
Label3.Caption:= Label3.Caption + ',' + IntToStr( Ord(character[4]));
Label3.Caption:= Label3.Caption + ',' + IntToStr( Ord(character[5]));
end;

The above could should give you a string of numbers on the caption like
1,20,1,2,1

Hope this helps.

Peter
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top