|
|
View previous topic :: View next topic |
Author |
Message |
Jody
Joined: 08 Sep 2006 Posts: 182
|
Parsing string to function.. what am i doing wrong? |
Posted: Wed Dec 02, 2015 4:27 pm |
|
|
Hello,
Want to parse a string to a function..
The function is in another file and it looks like that that is the problem...
I have include all mine files... anyone a idea??
Main.c:
Code: |
#include <main.h>
#include <commands.c>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
char slave_1[3] = "323";
char slave_2[5] = "green";
char slave_3[3] = "on";
void main()
{
setup_adc_ports(sAN15);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
while(1)
{
commands(&slave_1, &slave_2, &slave_3);
}
}
|
Commands.c:
Code: | #include <string.h>
#include <stdlib.h>
char string_rs485[MAXINPUTBUF];
/* The following typedef's should be adjusted for the particular platform.
* CRC-8 x^8 + x^2 + x + 1
* prototypes */
int generate_8bit_crc(char* data, int16 length, int pattern)
{
int *current_data;
int crc_byte;
int16 byte_counter;
int bit_counter;
current_data = data;
crc_byte = *current_data++;
for(byte_counter=0; byte_counter < (length-1); byte_counter++)
{
for(bit_counter=0; bit_counter < 8; bit_counter++)
{
if(!bit_test(crc_byte,7))
{
crc_byte <<= 1;
bit_test(*current_data, 7 - bit_counter) ?
bit_set(crc_byte,0) : bit_clear(crc_byte,0);
continue;
}
crc_byte <<= 1;
bit_test(*current_data, 7 - bit_counter) ?
bit_set(crc_byte,0) : bit_clear(crc_byte,0);
crc_byte ^= pattern;
}
current_data++;
}
for(bit_counter=0; bit_counter < 8; bit_counter++)
{
if(!bit_test(crc_byte,7))
{
crc_byte <<= 1;
continue;
}
crc_byte <<= 1;
crc_byte ^= pattern;
}
return crc_byte;
}
/*
|*****|**************|********|*****|
|Addr | commando | status | CRC |
|*****|**************|********|*****|
| 001 | groen | off | 034 |
|*****|**************|********|*****| */
void commands(char *slave_address, char *slave_commando, char *slave_status)
{
char space[1], slash_n[2], crc_string[3];
int16 crc_calculated, lengte;
strcpy(space, " ");
strcpy(slash_n, "\n ");
strcat(string_rs485, *slave_address);
strcat(string_rs485, space);
strcat(string_rs485, slave_commando);
strcat(string_rs485, space);
strcat(string_rs485, slave_status);
strcat(string_rs485, space);
crc_calculated = generate_8bit_crc(string_rs485, lengte, 0x107);
ITOA(crc_calculated, 10, crc_string );
strcat(string_rs485, crc_calculated);
strcat(string_rs485, slash_n);
}
|
Main.h:
Code: |
#include <18F87J50.h>
#device adc=16
#device PASS_STRINGS=IN_RAM
#define MAXINPUTBUF 20
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=20000000)
|
Regards,
Jody |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1357
|
|
Posted: Wed Dec 02, 2015 5:54 pm |
|
|
I haven't looked through the rest of your code, but right off the bat there is some C programming 101 stuff:
Code: |
char slave_1[3] = "323";
char slave_2[5] = "green";
char slave_3[3] = "on";
|
The string "323" is 4 characters long and "green" is 6 characters long.
Possible solutions:
Code: |
char slave_1[4] = "323";
char slave_2[6] = "green";
char slave_3[3] = "on";
|
or
Code: |
char slave_1[] = "323";
char slave_2[] = "green";
char slave_3[] = "on";
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 02, 2015 5:58 pm |
|
|
In addition to that,
The first line below has a bug. A pointer is passed by name, not
by *name. Remove the * on the first line:
Quote: | strcat(string_rs485, *slave_address);
strcat(string_rs485, space);
strcat(string_rs485, slave_commando);
strcat(string_rs485, space);
strcat(string_rs485, slave_status);
strcat(string_rs485, space); |
Also, this line is wrong. Pointers to arrays are passed by name, and
not with &name. Remove all the & from the line below:
Quote: | commands(&slave_1, &slave_2, &slave_3); |
|
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Thu Dec 03, 2015 4:09 am |
|
|
Yes!!
That did it...
Now reading it it all make sense.
I was fiddling half a day with it!
Thanks for pointing me in the right direction!!!
Regards,
Jody |
|
|
|
|
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
|