|
|
View previous topic :: View next topic |
Author |
Message |
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
help on custom Send_string_RS232 function |
Posted: Thu Aug 10, 2006 3:41 am |
|
|
Hello,
I want to send a constant string via RS232 that is formatted to our protocol instead of using printf functions.
definition:
Code: | #SEPARATE
void Send_String_RS232 ( const char * s , int8 datalength, int8 databyte ); |
function:
Code: | //*************************** Verstuur via RS232 *****************************//
#SEPARATE
void Send_String_RS232 ( const char * s , int8 datalength, int8 databyte )
{
int8 k;
putc ( 0 );
putc ( 170 );
for ( k = 0 ; k < 4 ; k++ )
putc ( s [k] );
putc ( datalength );
if ( datalength )
putc ( databyte );
} |
use:
Code: | #define ADAPTER_IN_COMMANDO "ADIN"
Send_String_RS232 ( ADAPTER_IN_COMMANDO , 0 ,0 ) ; |
Now I get all sorts of compiler errors like declaration of const char * is wrong and " cannot create a pointer to a constant ".
Can you sort this out to get this good? You may not use strcpy functions. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Aug 10, 2006 3:47 am |
|
|
Code: |
char const ADAPTER_IN_COMMANDO[] = "ADIN";
Send_String_RS232 ( ADAPTER_IN_COMMANDO , 0 ,0 ) ; |
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 10, 2006 4:16 am |
|
|
What are 'datalength', and 'databyte' really needed for?. Will you ever use different values for these?.
You can do something like this:
Code: |
void putcint(int x) {
putc(c);
}
#define Send_String_RS232(x,y,z) putc(0);\
putc(170);\
putcint(x);\
putc ( y ); \
if ( y ) \
putc ( z );
|
This however will output the _whole_ of the constant string (not counting four characters as you do).
Working out how it works, I leave down to you...
Best Wishes |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Thu Aug 10, 2006 6:11 am |
|
|
asmallri wrote: | Code: |
char const ADAPTER_IN_COMMANDO[] = "ADIN";
Send_String_RS232 ( ADAPTER_IN_COMMANDO , 0 ,0 ) ; |
|
I'm afraid that's not working.
This function definition is illegal:
Code: | void Send_String_RS232 (const char * s , int8 datalength, int8 databyte ); |
There seem to be problems with the const word.
Compiler gives an error: expect ( or '.
I want this:
Code: | #define COMMAND "BLAH"
char str [4];
// in program
str = COMMAND; |
And yes datalength and databyte are different :-) |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Aug 10, 2006 6:17 am |
|
|
Code: | void Send_String_RS232 (char * s , int8 datalength, int8 databyte ); |
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Thu Aug 10, 2006 6:19 am |
|
|
still not working that way.. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Aug 10, 2006 6:31 am |
|
|
Sorry for the bum steer. That's what happens when you debug some other problem and try to help someone on the side without giving it your full attention.
I used sprintf to copy the const string to a string and then pass it to printf. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 10, 2006 6:46 am |
|
|
Ttelmah wrote: | What are 'datalength', and 'databyte' really needed for?. Will you ever use different values for these?.
You can do something like this:
Code: |
void putcint(int x) {
putc(c);
}
#define Send_String_RS232(x,y,z) putc(0);\
putc(170);\
putcint(x);\
putc ( y ); \
if ( y ) \
putc ( z );
|
This however will output the _whole_ of the constant string (not counting four characters as you do).
Working out how it works, I leave down to you...
Best Wishes |
Modify line 2, to say 'putc(x)', not 'c', and get rid of the last ';' in the define (not needed). It should work.
Best Wishes |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Thu Aug 10, 2006 6:54 am |
|
|
I need to send "COMM"
4 bytes.
It don't really understand how
Code: | void putcint(int x) {
putc(c);
} |
does that job.
This I want:
0 170 C O M M datalength databyte(s)
normally I always use:
in header file:
Code: | #define WAKE_COMMANDO "WAKE"
#define SLEEP_COMMANDO "SLEE"
#define START_LADEN_COMMANDO "LDNG" |
in program
Code: | printf ("%c%c%S%c",0 ,COMMANDO_BYTE, SLEEP_COMMANDO,0); |
Now because I use this printf a lot = much the same code, I want it in a function:
Send_String_RS232 ( string, datalength, databyte )
[/code] |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 10, 2006 7:38 am |
|
|
The key, is that CCS, has a 'shortcut', for constant strings. If you have a function, that is designed to accept a single byte integer, and you call it, with a constant string, then the function gets automatically called once for every byte in the string. Unfortunately, the 'putc' ciommand as standard, accepts characters, not integers (though they are the 'same' normally in CCS C), this stops the shortcut working.
If you put the putc function into a 'wrapper', that accepts integers:
Code: |
void putcint(int x) {
putc(c);
}
|
Then this 'wrapper', will allow the shortcut to work!.
In fact your initial string can be sent as:
putcint("\000\170COMM");
Details of this shortcut, used to be in the manual, but finding it is 'fun'...
Also, a #define, can be made to behave just like a 'function', with multiple values, and lines in the resulting code, and combined with the shortcut, allows a syntax exactly as you required.
Best Wishes |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Thu Aug 10, 2006 10:01 am |
|
|
Ttelmah,
very informative replies from you.
could you elaborate on the quote below, with maybe a few examples
"Also, a #define, can be made to behave just like a 'function', with multiple values, and lines in the resulting code, and combined with the shortcut, allows a syntax exactly as you required. "
Mike |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 10, 2006 10:09 am |
|
|
I posted a multi-line define to generate the behaviour of the function he wanted.
You can get really 'smart' with defines. For instance, you can use conditional constructs, with multiple statements in the 'test' part of the construct, etc..
These can be a real 'trap for the unwary', but can also be a powerful way of achieving some results.
Best Wishes |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Thu Aug 10, 2006 10:42 am |
|
|
All I want is to pass a constant string to a function.
That define-function does not let me change parameters
Send_RS232_String ( constant string, some int, some int); |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 10, 2006 11:59 am |
|
|
Code: |
void putcint(int x) {
putc(x);
}
#define Send_String_RS232(x,y,z) putc(0);\
putc(170);\
putcint(x);\
putc ( y ); \
if ( y ) \
putc ( z )
Send_String_RS232("COMM",0,0);
Send_String_RS232("Fred",1,2);
Send_String_RS232("Dick,1,0);
|
What 'more' do you want?.
Best Wishes |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Fri Aug 11, 2006 12:42 am |
|
|
Well that is exactly what I want; just I didn't understand that syntax;
What is the meaning of '\' ?
Is that #define line put in the header file?
Can I declare that function in the .C file and put it in the C-program at the bottom?
thks for your input.. |
|
|
|
|
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
|