|
|
View previous topic :: View next topic |
Author |
Message |
emil86
Joined: 14 Nov 2006 Posts: 6 Location: Austria, Vienna
|
RS485 - problem with pointer |
Posted: Fri Jan 05, 2007 8:20 am |
|
|
hy guys,
first i want to thank you for thisgreat forum you helped me a much till now.
please take a look at my source code and tell me if the type definitons are ok, because this is my first time i have to do with pointers in C. Is the parameter in the function call ok ? and are there any errors in the rs485.c from CCS ?? i have absolutly no idea, because sometimes it works, and sometimes it doesn't. Is it ok to let the RX_ENABLE pin the whole time low ?
thx in advance,
emil
Code: |
#include <18f2580.h>
#fuses INTRC_IO, NOWDT, NOMCLR, NOLVP, NOPROTECT
#use delay(clock=8000000)
#define RS485_ID 1 // The device's RS485 address or ID
#define RS485_USE_EXT_INT FALSE // Select between external interrupt
#define RS485_RX_PIN PIN_C7 // Data receive pin
#define RS485_TX_PIN PIN_C6 // Data transmit pin
#define RS485_ENABLE_PIN PIN_C5 // Controls DE pin. RX low, TX high. Pin 1 von unten
#include <rs485.c>
#include <lcd_HD44780_4x16.c>
typedef struct
{
int8 from_id; //
int8 length; //
int8 data; //
}receive_buffer;
typedef struct
{
int8 to_id; //
int8 length; //
int8 data; //
}send_buffer;
/** H A U P T P R O G R A M M *************************************************/
void main(void)
{
receive_buffer rb;
send_buffer sb;
char text[15];
setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_OFF); //stellt die Frequ. auf 8MHz
lcd_init(); //initialisiert das LCD
rs485_init();
lcd_clearlcd();
lcd_gotoxy(1,1);
printf(lcd_putc,"MASTER ID1 init.");
delay_ms(500);
sb.to_id=2;
sb.length=8;
sb.data=1; //Initialisiert den Slave
rs485_send_message(sb.to_id,sb.length,&sb.data); //Paket an Slave mit ID2 und Data1
delay_ms(400);
rs485_get_message(&rb,TRUE); //Warte auf Antwort
lcd_gotoxy(1,2);
sprintf(text,"ID%i erreichbar",rb.from_id);
printf(lcd_putc,text);
/* sb.to_id=3;
sb.length=8;
sb.data=1; //Initialisiert den Slave
rs485_send_message(sb.to_id,sb.length,&sb.data); //Paket an Slave mit ID3 und Data1
rs485_get_message(&rb,TRUE); //Warte auf Antwort
lcd_gotoxy(1,3);
sprintf(text,"ID%i erreichbar",rb.from_id);
printf(lcd_putc,text);
*/
while (TRUE)
{
sb.to_id=2;
sb.length=8;
sb.data=1; //Fortlaufendes Pakete senden
rs485_send_message(sb.to_id,sb.length,&sb.data); //Paket an Slave mit ID2 und Data1
delay_ms(50);
rs485_get_message(&rb,TRUE); //Warte auf Antwort
lcd_gotoxy(1,2);
sprintf(text,"I:%i B:%i D:%i",rb.from_id,rb.length,rb.data);
printf(lcd_putc,text);
/* sb.to_id=3;
sb.length=8;
sb.data=1; //Fortlaufendes Pakete senden
rs485_send_message(sb.to_id,sb.length,&sb.data); //Paket an Slave mit ID3 und Data1
rs485_get_message(&rb,TRUE); //Warte auf Antwort
lcd_gotoxy(1,2);
sprintf(text,"I:%i B:%i D:%i",rb.from_id,rb.length,rb.data);
printf(lcd_putc,text);
*/
}
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 05, 2007 8:43 am |
|
|
You have 'data' in each structure defined as a single int8 character. Then you start to use it as if it was an array of characters. The only array you have present, is the 'text' array. You need an array inside the buffers....
Best Wishes |
|
|
emil86
Joined: 14 Nov 2006 Posts: 6 Location: Austria, Vienna
|
|
Posted: Fri Jan 05, 2007 10:43 am |
|
|
.... |
|
|
emil86
Joined: 14 Nov 2006 Posts: 6 Location: Austria, Vienna
|
|
Posted: Sat Jan 06, 2007 11:54 am |
|
|
Sorry i can't find the line where i am using the 'data' variable like an array.
And how should i fix my source code so it will be okay?
Thanks in advance. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Jan 06, 2007 12:41 pm |
|
|
Code: |
sb.length=8;
sb.data=1; //Initialisiert den Slave
rs485_send_message(sb.to_id,sb.length,&sb.data); //Paket an Slave mit ID2 und Data1
|
Length is in bytes not bits. You data is only 1 byte long but you tell it to send 8 bytes. |
|
|
emil86
Joined: 14 Nov 2006 Posts: 6 Location: Austria, Vienna
|
|
Posted: Sat Jan 06, 2007 12:46 pm |
|
|
GE thanks a lot
You guys saved me again!! |
|
|
Ttelmah Guest
|
|
Posted: Sat Jan 06, 2007 1:45 pm |
|
|
emil86 wrote: | Sorry i can't find the line where i am using the 'data' variable like an array.
And how should i fix my source code so it will be okay?
Thanks in advance. |
In the 'send message' call, you have a length, and the address of data. What happens if the length is more than one...
Best Wishes |
|
|
|
|
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
|