View previous topic :: View next topic |
Author |
Message |
tequilaa
Joined: 16 Aug 2019 Posts: 2
|
RS232 sending variable |
Posted: Wed Aug 28, 2019 9:27 am |
|
|
Hello everyone, I would like to use RS232 to communicate between two pic and planning to send variable to each other.
I am new to pic and I would like to ask if there are any method to send integer from one pic to another pic using rs232.
I know getc/gets could help but I would like to know if there are some easier method to reduce complexity. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Aug 28, 2019 9:43 am |
|
|
What you ask for is in the manual, in the FAQ section,as well there's examples, in the 'examples' folder.
If dealing with bytes, putc(), getc() are the basic 'building blocks' of 'RS232' communications.
Easiest is of course using Assembler ....
Jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Aug 29, 2019 2:05 am |
|
|
CCS provides sample programs for both sending and receiving characters.
I prefer to work with ASCII characters so that I can use a PC to sniff at the data and be able to easily read what's happening.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Thu Aug 29, 2019 3:39 am |
|
|
Lets make some comments.
Now a 'variable', is just a number of bytes in memory. So this can be
transmitted just like any other byte value.
However there is a significant 'downside' to sending variables as raw
byte values. This is that it then prevents you using byte values as
'markers' (end of line, end of transmission etc.), since the values can
contain any possible number.
So it is generally much easier to send as 'ASCII' (so in what is effectively
a 'human readable' form).
printf allows you to output variables in this sort of form.
There are then a 'suite' of functions that convert text values back into
'numbers'. So atoi, atof, atoi32, etc. etc..
Downside of the ASCII approach though is 'size'. A 32bit variable sent as
'raw', is just four bytes, sent as ASCII decimal, it requires ten bytes. One
'compromise', is to send as hex. Sent as hex, the same variable needs just
8 characters, is still quite easy to read as a 'human', still allows all the
formatting characters. This is why quite a few transmission formats use
hex. |
|
|
rookiee
Joined: 31 Aug 2019 Posts: 5
|
|
Posted: Mon Sep 09, 2019 9:39 am |
|
|
I would also like to ask how to sending multiple variables using rs232 with two pic.
For example my transmitter side are constantly sending the sensor values.
Code: |
//Transmitter
void main()
{
int val1,val2;
while(1)
{
val1++;
putc(val1);
val2++;
putc(val2);
}
}
|
And the receiver side
Code: |
void main()
int val1,val2;
{
while(1)
{
val1=getc();
val2=getc();
}
}
|
How should I manage that the val1 from the transmitter is indeed store into val1 of the receiver and also for the val2. And how the transmitter side know the receiver had received the value such that it can proceed another transmission? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Mon Sep 09, 2019 2:59 pm |
|
|
short answer...
You need to have the 'transmitter' PIC send a 'sync' byte before the 2 data bytes. The 'receiver' PIC will sit in a loop until it sees the 'sync' byte THEN store the next 2 bytes into 'val1' and 'val2'.
Have a look at any of the 'GPS' code in the 'code library' where programmers have to 'parse' the incoming data stream.
Now there is a problem IF either of the 2 data bytes are the same as the 'sync' byte......but, check the code, to see how it's handled... |
|
|
|