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

How to send an array of integers through an RS232

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 21, 2008 12:28 pm     Reply with quote

Here is an example of how to send an array of bytes:
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define HEAD 0x03
#define TAIL 0x95

//======================================
void main()
{
int8 data[] = {0x55, 0x56, 0x57, 0x58};
int8 i;

putc(HEAD);

for(i = 0; i < sizeof(data); i++)
   {
    putc(data[i]);
   }

putc(TAIL);

while(1);

}
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Mon Jan 21, 2008 2:23 pm     Reply with quote

One tip that might be useful.

If you have to send arbitrary data, you can get situations where a data element has the same value as your HEAD byte. A way to deal with this is to say that any time this happens, you will send the same element again. That means that if the receiving end of your link encounters a HEAD byte that isn't immediately repeated, it can assume that it's the start of a new packet. This helps keep packets straight if (for instance) there are occasional breaks in the transmission line, or units getting turned off and on.

But if you know ahead of time what's going to be sent, perhaps it's certain that the HEAD byte represents itself and nothing else. In that case, no problem.
shen
Guest







PostPosted: Mon Jan 21, 2008 8:45 pm     Reply with quote

I understand a little, I try it again, Thank you
umka



Joined: 28 Aug 2007
Posts: 99
Location: New Zealand

View user's profile Send private message

PostPosted: Sun Mar 23, 2008 8:14 pm     Reply with quote

Can one have an array of variables? or can one change a single byte in an array without having to update the whole array?

can one use for example data[2] = 0x25; to change just the second number in the array to 0x25.

I want to send an array through RS232 to show button states, each byte will represent 8 buttons.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Mon Mar 24, 2008 4:21 am     Reply with quote

There are many ways to learn. The most successful is often to learn by doing. It involves experimenting. The PIC environment allows both simulation and debugging. You could have used the simulator and assigned the array element and discovered that it would work. This has advantages since it strengthens confidence and self reliance and allows a programmer to discover his/her own errors. As code grows more complex and lengthy fewer people are willing to spend time discovering another's errors so self reliance becomes necessary. Help for a vexing problem can still be needed and in that case for a speedy response code must be trimmed down to a dozen lines that
exhibit the problem. The value to this is that often times the act of trimming it down makes the issue obvious to the very programmer with the issue.
huutanbkpic



Joined: 13 Apr 2008
Posts: 1
Location: Vietnam

View user's profile Send private message Send e-mail Visit poster's website

Someone please help me?
PostPosted: Sun Apr 13, 2008 2:59 am     Reply with quote

I would like to send the percentage of PWM module from PC to PIC, but i do not know how to send the floating-point number over RS232 please help me!
For example: can anyone provide me a code to send 78.05 from PC in visual C++ and the code for the int_rda to receive the data.
Best regards.
_________________
Thanks for your attention
KU5D



Joined: 10 Feb 2008
Posts: 46
Location: Asheville, North Carolina

View user's profile Send private message

PostPosted: Sun Apr 13, 2008 9:53 am     Reply with quote

Perhaps multiply your 78.05 x 100 in the PC and send it as two bytes (7805). When the PIC receives it, stick it in an array. If you're printing this for a user you can use formatting to display the MSB to the left of the decimal point, and the LSB to the right. Better than dealing with floats.
_________________
Confidence is the feeling you have right before you fully understand the situation...
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Apr 13, 2008 10:46 am     Reply with quote

Floating point notation has many forms. The CCS form is described in the CCS manual. I will assume here you are using the 4 byte notation of CCS.
This means if this 4 bytes of notation is sent across a RS232 interface the notation when successfully received will still be just CCS notation. Now the PC program has to be able to understand this notation. If you search this forum you will find solutions to your issue of compatability of CCS notation with PC software notation. Expect to learn about unions to map the CCS notation to a 4 byte array so the bytes of notation can be individually transmitted. Expect to learn about how the various PC software programs use their specific floating pt notation. Now a way out is to let the PIC via a printf statement convert the internal floating pt notation to ascii char notation for the number. Expect to learn about printf formatting. At the PC end most software accepts ascii notation for numbers Ex "1234.5678901" and will convert from that notation to its specific float notation.
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