18/5/2014
CCS :: View topic - [working with hold_regs, and modbus rs485] problem
FAQ
Forum Help
Profile
Official C C S
to check your private messages
Search
C C S doe s not m onitor this forum on a re gular basis.
Ple ase do not post bug R e ports on this forum . Se nd the m to
[email protected]
[working with hold_regs, and modbus rs485] problem CCS Forum Index -> General CCS C Discussion View previous topic :: View next topic Author arthurlmiranda
Message [working with hold_regs, and modbus rs485] problem Poste d: Fri O ct 19, 2012 8:55 am
e d: 19 O ct 2012 Posts: 2
Hello guys, this is my first post here. I'm doing a simple test with modbus rs485. I have a button in a slave that I press it and the slave counts and the master should read this number... My problem is the master get the right data from slave, I don't know if i have to use holding s or input regs. The sample ex_modbus_slave.c use some holding regs but I can't compile with them for example if I use 0x8800 i get this error (n#45) subscript out of range. My ccs version IDE 4.078. I'm simulating this on proteus. My simulation with the slave address is working fine, the slave undertands that the message is for it. Probably I won't have access to internet until monday morning. Sorry for bad English, I'm from brazil.
Masters Code Code: #include <18F4550.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT #use delay(clock=20M) #define MODBUS_TYPE MODBUS_TYPE_MASTER #define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_EXT interrupcao #define MODBUS_SERIAL_BAUD 9600 #define MODBUS_SERIAL_RX_PIN PIN_c6 #define MODBUS_SERIAL_TX_PIN PIN_c7 #define MODBUS_SERIAL_ENABLE_PIN PIN_c4 #define MODBUS_SERIAL_RX_ENABLE PIN_c5
#define MODBUS_SERAIL_RX_BUFFER_SIZE 64
// MODBUS_TYPE_MASTER or MODBUS_TYPE_SLAVE //interrupcao externa mas nao estamos usando os pinos que tem //Valid baud rate for serial //Valid pin for serial receive //Valid pin for serial transmit //Valid pin for serial enable, rs485 only //Valid pin for serial rcv enable, rs485 only
//Size of the receive buffer
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA //Tipo de interrupção(assincrono) pro rs #include "modbus.c"
#define MODBUS_SLAVE_01_ADDRESS 0xF5 #define MODBUS_SLAVE_02_ADDRESS 0xF6
void read_all_holding() { if(!(modbus_read_holding_s(0xF5,0x0001,8))) // should I use 0 or 8? { int i; printf("Data: "); /*Started at 1 since 0 is quantity of coils*/ for(i=1; i < (modbus_rx.len); ++i) printf("%X ", modbus_rx.data[i]); printf("\r\n\r\n"); } else { printf("<-**Exception %X**->\r\n\r\n", modbus_rx.error); } } void read_all_input_reg() { printf("Input s:\r\n"); if(!(modbus_read_input_s(0xF5,0x0003,8))) // should I use 0 or 8? { int i; printf("Data: "); /*Started at 1 since 0 is quantity of coils*/
http://www.ccsinfo.com/forum/viewtopic.php?t=49255
1/4
18/5/2014
CCS :: View topic - [working with hold_regs, and modbus rs485] problem /*Started at 1 since 0 is quantity of coils*/ for(i=1; i < (modbus_rx.len); ++i) printf("%X ", modbus_rx.data[i]); printf("\r\n\r\n"); } else { printf("<-**Exception %X**->\r\n\r\n", modbus_rx.error); } }
void main(void) { int i; while(1) { // 2 test to receive the data. Using the sample from ex_modbus_master printf("\rreading holding"); read_all_holding(); delay_ms(1800); read_all_input_reg(); printf("\rreading input reg"); delay_ms(1800); // 2 test to receive the data. Using the samples from modbus.c modbus_read_holding_s(0xF5,0x0001,8);// should I use 0 or 8? printf("\rreading holding"); delay_ms(1800); modbus_read_input_s(0xF5,0x0003,8); // should I use 0 or 8? printf("\rreading input reg"); delay_ms(1800);
} //whie }//main
Slave Code: #include <18F4550.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT #use delay(clock=20M) #use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, stream=PIC, errors) #define MODBUS_TYPE MODBUS_TYPE_SLAVE #define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_EXT #define MODBUS_SERIAL_BAUD 9600 #define MODBUS_SERIAL_RX_PIN PIN_c6 #define MODBUS_SERIAL_TX_PIN PIN_c7 #define MODBUS_SERIAL_ENABLE_PIN PIN_c4 #define MODBUS_SERIAL_RX_ENABLE PIN_c5
// MODBUS_TYPE_MASTER or MODBUS_TYPE_SLAVE //Source of interrupts (MODBUS_INT_EXT,MODBUS_INT_RDA,MODBUS_INT_RDA2) //Valid baud rate for serial //Valid pin for serial receive //Valid pin for serial transmit //Valid pin for serial enable, rs485 only //Valid pin for serial rcv enable, rs485 only
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA //Tipo de interrupção(assincrono) pro rs #define MODBUS_SERAIL_RX_BUFFER_SIZE 64 //Size of the receive buffer
#include "modbus.c" #define MODBUS_ADDRESS 0xF5 void main(void) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int8 coils = 0b00000101; int8 inputs = 0b00001001; int16 hold_regs[] = {0x8800,0x7700,0x6600,0x5500,0x4400,0x3300,0x2200,0x0001}; int16 input_regs[] = {0x0003, 0x0004}; int16 event_count = 0; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int8 *data; int i=0; modbus_init(); printf ("slave 0xF5") ; while(TRUE) {
while(!modbus_kbhit()) { if((modbus_rx.address == MODBUS_ADDRESS)) { } if(input(pin_D4)==1){ i++;
http://www.ccsinfo.com/forum/viewtopic.php?t=49255
2/4
18/5/2014
CCS :: View topic - [working with hold_regs, and modbus rs485] problem i++; data=i; hold_regs[0x8800]=data;// test to use with read holding s input_regs[0x0003]= data;// test to use with read input s printf("\r button pressed %ld times ",data); delay_ms(1000); if(modbus_rx.func == FUNC_READ_HOLDING_S) modbus_read_holding_s_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),hold_regs+modbus_rx.data[1]); else modbus_read_input_s_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),input_regs+modbus_rx.data[1]); { }
} } } }
Thank You guys Mike Walne
Poste d: Fri O ct 19, 2012 11:00 am
Quote:
e d: 19 Fe b 2004 Posts: 1387 Location: Boston Spa UK
I'm simulating this on proteus.
This is a CCS forum not a proteus one. Proteus is bad news, it's so full of bugs you're wasting your time with it. Eventually you have to get real hardware to work in the real world . Start with real hardware. I can't (no refuse to) help with ISIS / Proteus. Mike temtronic
e d: 01 Jul 2010 Posts: 3046 Location: Gre e nsville ,O ntario
Poste d: Fri O ct 19, 2012 11:39 am
I have to 'second' Mike's response ! Please read PIC101 at the top of this forum. As Mike said Proteus is FULL of bugs, errors and faulty DRCs. You can't possibly expect anyone to 'fix' your code when your 'running' on a real bad 'simulator'. The only simulator I run is real hardware in the real world.NO simulator,that I know of, will create EMI,welding spikes, cell phone interferenc,power dips,noisey Vcc,bad solder ts, pins swapped,etc. Buy a PIC, a white breadboard and some 22ga solid wire .For less than an hour's worth of total frustration you can buy all the real hardware you need and NOT be disappointed! And... you'll get lots of help here as well. hth jay
Mike Walne
e d: 19 Fe b 2004 Posts: 1387 Location: Boston Spa UK
Poste d: Fri O ct 19, 2012 2:37 pm
Thanks jay. Nice to get some . Mike
arthurlmiranda
e d: 19 O ct 2012 Posts: 2
PCM programmer
e d: 06 Se p 2003 Posts: 17892
Poste d: W e d O ct 24, 2012 7:22 am
ok but no one who knows how to use modbus can tell me if I'm doing something really stupid? where I work here I can only simulate this, I don't know when my boss will buy PIC and etc...
Poste d: W e d O ct 24, 2012 11:35 am
Yes, there is something wrong. You should look closely at your pin assignments below: Quote: #define MODBUS_SERIAL_RX_PIN PIN_c6 #define MODBUS_SERIAL_TX_PIN PIN_c7
http://www.ccsinfo.com/forum/viewtopic.php?t=49255
3/4
18/5/2014
CCS :: View topic - [working with hold_regs, and modbus rs485] problem #define MODBUS_SERIAL_ENABLE_PIN PIN_c4 #define MODBUS_SERIAL_RX_ENABLE PIN_c5
Look at the 18F4550 data sheet and see what the functions and capabilities of those pins are, and then look what you are trying to use them for. Also, look in all the CCS modbus files and notice what pin they assign to those constants. Also you have changed the case of the pin on each pin constant. This is not the same as the All Caps case that's used in the CCS .h files. CCS is a rare compiler that by default, it ignores the case. If you change the case of compiler constants in other compilers, you will get "undefined identifier" errors. It's a bad habit to change the case. Display posts from pre vious:
All Posts
Oldest First
Go All tim e s are GMT - 6 Hours
CCS Forum Index -> General CCS C Discussion Page 1 of 1 Jum p to:
General C C S C Discussion You cannot post ne w topics You cannot re ply to topics You cannot e dit your posts You cannot de le te your posts You cannot vote in polls
in in in in in
this this this this this
forum forum forum forum forum
Powered by phpBB © 2001, 2005 phpBB Group
A ds by V iew P assw ord
http://www.ccsinfo.com/forum/viewtopic.php?t=49255
A d O ptions
4/4