Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[51] Sending a variable on UART

Status
Not open for further replies.

hithesh123

Full Member level 6
Full Member level 6
Joined
Nov 21, 2009
Messages
324
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
lax
Visit site
Activity points
3,548
I know how to send text on serial port.
How do you send a variable on serial port.

Let's say my variable x is of type int.

The variable can have different values at different times.
 

what means by add the variable by 0x30 ?
note : i am using mikroC compiler
 

I know how to send text on serial port.
How do you send a variable on serial port.

Let's say my variable x is of type int.

The variable can have different values at different times.

you can convert the variable value to a text string, e.g. using sprintf()
https://www.cplusplus.com/reference/cstdio/sprintf/

Code:
int x=1234;
char text[20];
sprintf(text,"%d",x);
UART2PrintString(text);

or itoa()
https://www.cplusplus.com/reference/cstdlib/itoa/
e.g.
Code:
int x=1234;
char text[20];
itoa(text,x,10);
UART2PrintString(text);

or you can transmit the binary value, e.g. assuming an int is 16bits (two bytes)
Code:
int x=1234;
UART2PutChar(x&0xff);   // send least significant byte
UART2PutChar((x>>8)&0xff);  // send most significat byte
 

Use IntToStr() to convert integer value to ascii value and then send this string using UART1_Write_Text() function.
 
The real answer depends on how it is to be interpreted at the receiving end. Horace1 gave the correct and simplest answer at the end of his post, simply split the 16-bit int into two 8-bit chunks and send them one after the other.

If the receiving software can only accept the ASCII character set, change each character to it's ASCII equivalent using the other methods mentioned. Note that simply adding 0x30 will not work in most cases, in fact it will only work if the int value is between zero and 0x0009.

Brian.
 

You may use printf() i think...like this..........
Code:
printf("Temp::\t%3d r",(signed int8)T);

Here T is the variable that you want sent....Signed int8 is the type definition of the variable..and %3 specifies the number digit to display the variable.........

Thank You
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top