CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

RS232 sending variable

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tequilaa



Joined: 16 Aug 2019
Posts: 2

View user's profile Send private message

RS232 sending variable
PostPosted: Wed Aug 28, 2019 9:27 am     Reply with quote

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: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 28, 2019 9:43 am     Reply with quote

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 .... Very Happy

Jay
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Aug 29, 2019 2:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 29, 2019 3:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Sep 09, 2019 9:39 am     Reply with quote

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: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 09, 2019 2:59 pm     Reply with quote

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...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group