View previous topic :: View next topic |
Author |
Message |
shen Guest
|
How to send an array of integers through an RS232 |
Posted: Mon Jan 21, 2008 7:14 am |
|
|
I can send and receive RS232 single character,
but I want to send a group of data once with datahead and datatail,
the array of integer data, Must be converted to character?
For example: I would like to send data 55 56 57 58 to the PC, with the datahead 03 and datatail 95, such as an integer array 03 55 56 57 58 95
Must be they converted to character array can be passed PC? How to send it? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jan 21, 2008 7:42 am |
|
|
It all depends on what the receiving program does with them.
If it is hyperterminal then it will try and display the character equivalent. As 03 is a control character it will be displayed as garbage, not at all or will do something strange. So you would need to convert to ascii as in printf("%d", 3);
If you have an application receiving the data and that just processes the values then all should be fine.
You send and receive the same as you would any ascii char putc and getc.
Loop through your array and call putc.
It may be possible to just use printf to print a string of control chars but this will depend on the printf routine. Also a character of value 0 represents an end of string. |
|
|
shen Guest
|
|
Posted: Mon Jan 21, 2008 9:28 am |
|
|
Is there a good example about Sending array int data? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 21, 2008 12:28 pm |
|
|
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
|
|
Posted: Mon Jan 21, 2008 2:23 pm |
|
|
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
|
|
Posted: Mon Jan 21, 2008 8:45 pm |
|
|
I understand a little, I try it again, Thank you |
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Sun Mar 23, 2008 8:14 pm |
|
|
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
|
|
Posted: Mon Mar 24, 2008 4:21 am |
|
|
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
|
Someone please help me? |
Posted: Sun Apr 13, 2008 2:59 am |
|
|
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
|
|
Posted: Sun Apr 13, 2008 9:53 am |
|
|
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
|
|
Posted: Sun Apr 13, 2008 10:46 am |
|
|
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. |
|
|
|