|
|
View previous topic :: View next topic |
Author |
Message |
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
modbus slave |
Posted: Wed Mar 07, 2012 3:14 am |
|
|
Hi, I use standard slave implemetation from CCS example ex_modbus_slave.c
Just raw copy with PIC specific adjustments. As scada software I use Free Scada with RTU support over serial port.
This is what scada sends to pic: 0201000700014C38
This is what it gets back from my pic: 02020100A1CC
This is what scada sends to pic: 0201000000083DFF
This is what it gets back from my pic: 0202010561CF
This is what scada sends to pic: A70100000001E50C
This is what it gets back from my pic: A702010142C0
Response from PIC looks invalid - as far I understand it should be 020100A1CC not 02020100A1CC or A702010142C0 if I change an address to 0xA7 form 0x02
Any suggestions? Anyone knows about any compatibility issues?
Thank you.
front part of the program
Code: |
#include <18F4520.h> //libs from compiler
#device ADC=10 //analogue to digital converter init specs
//#DEVICE HIGH_INTS=TRUE
#include <ctype.h> //libs from compiler
#include <stddef.h> //libs from compiler
#include <string.h> //libs from compiler
//=========DEFINE====================
#define MODBUS_SERIAL_RX_PIN PIN_C7 // serial data receive pin
#define MODBUS_SERIAL_TX_PIN PIN_C6 // serial data transmit pin
#define MODBUS_SERIAL_ENABLE_PIN PIN_B1 // serial TX on
#define STATUSL PIN_B0 // status led
#define GOUT1 PIN_D7
#define GOUT2 PIN_D6
#define GOUT3 PIN_D5
#define GOUT4 PIN_D4
#define GOUT5 PIN_C5
#define GOUT6 PIN_C4
#define BUZZ PIN_D2
#define POWER PIN_D0
#define BATTERY PIN_D1
#define MAINS_TEST PIN_AN5
#define POWER_TEST PIN_AN6
#define BATTERY_TEST PIN_AN7
#define INTMP PIN_AN0
#define TMP1 PIN_AN1
#define A1 PIN_AN2
#define A2 PIN_AN3
#define A3 PIN_AN4
#define BATTERY_TEST_ON PIN_B5
#define IN1 PIN_B2
#define IN2 PIN_A4
#define IN3 PIN_C3
#define IN4 PIN_C2
#define IN5 PIN_C1
#define IN6 PIN_C0
#define IN7 PIN_B4
#define IN8 PIN_B3
//===================================
#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_SERIAL_TYPE MODBUS_RTU //use MODBUS_ASCII for ASCII mode
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600
//#define MODBUS_SERIAL_RX_ENABLE 0 // Controls RE pin for RS485
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#FUSES H4
#FUSES MCLR
#FUSES BROWNOUT
#FUSES NODEBUG
#FUSES NOPROTECT
#FUSES NOWDT
#FUSES PUT
#FUSES NOCPD
#FUSES NOXINST // Config: ext reset, no code protect, no watchdog, high speed clock
#use delay (clock=40M, oscillator=10M)
#use fixed_io(D_outputs=GOUT1,GOUT2,GOUT3,GOUT4,BUZZ,POWER,BATTERY)
#use fixed_io(A_outputs=)
#use fixed_io(B_outputs=STATUSL,MODBUS_SERIAL_ENABLE_PIN,BATTERY_TEST_ON)
#use fixed_io(C_outputs=MODBUS_SERIAL_TX_PIN,GOUT5,GOUT6)
#use fixed_io(E_outputs=)
#include <modbus.c>
#define MODBUS_ADDRESS 0x02
int timer = 0;
int timerstep = 0;
int timerstep2 = 0;
int timerstep3 = 0;
#int_RTCC
void timer0_isr(void) {
timer++;
if (timer == 0) {
output_high(STATUSL);
}
if (timer == 0xFF) {
timerstep++;
}
if (timerstep == 128) {
output_low(STATUSL);
}
if (timerstep == 0xFF) {
timerstep2++;
}
if (timerstep2 == 0xFF) {
timerstep3++;
}
if (timerstep3 == 0x05) {
}
}
int8 swap_bits(int8 c)
{
return ((c&1)?128:0)|((c&2)?64:0)|((c&4)?32:0)|((c&8)?16:0)|((c&16)?8:0)
|((c&32)?4:0)|((c&64)?2:0)|((c&128)?1:0);
}
//=========MAIN PROGRAM==============
void main() { // main program
int i;
int8 coils = 0b00000101;
int8 inputs = 0b00001001;
int16 hold_regs[] = {0x8800,0x7700,0x6600,0x5500,0x4400,0x3300,0x2200,0x1100};
int16 input_regs[] = {0x1100,0x2200,0x3300,0x4400,0x5500,0x6600,0x7700,0x8800};
int16 event_count = 0;
setup_wdt(WDT_OFF); //set watch dog timer
disable_interrupts(GLOBAL); //clear interrupts
set_timer0(0); //clear timers
set_rtcc(0); //clear timers
setup_timer_0(RTCC_INTERNAL | RTCC_8_BIT | T0_DIV_1); //set timer 0 parameters
setup_adc_ports(AN0_TO_AN7 | VSS_VDD); //set AD ports
setup_adc(ADC_CLOCK_DIV_16 | ADC_TAD_MUL_16); //ADC_CLOCK_INTERNAL |
setup_comparator(NC_NC_NC_NC); //turn off comparators
enable_interrupts(GLOBAL); //clear interrupts
//enable_interrupts(int_rda); //enable serial port
enable_interrupts(INT_RTCC); //enable TIMERs
//=========BOOT UP===================
output_low(STATUSL);
//output_low(TX_ON);
output_low(GOUT1);
output_low(GOUT2);
output_low(GOUT3);
output_low(GOUT4);
output_low(GOUT5);
output_low(GOUT6);
output_low(BUZZ);
output_low(POWER);
output_low(BATTERY);
output_high(BUZZ);
delay_ms(150);
output_low(BUZZ);
delay_ms(150);
output_high(BUZZ);
delay_ms(150);
output_low(BUZZ);
delay_ms(150);
output_high(BUZZ);
delay_ms(150);
output_low(BUZZ);
output_high(STATUSL);
modbus_init();
|
then rest follows as per while loop from example file
CCS V4.114 in use _________________ Help "d" others and then you shell receive some help from "d" others. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Mar 07, 2012 1:04 pm |
|
|
It's a trivial copy and past error in ex_modbus_slave.c
Change the below code
Code: | if(modbus_rx.func == FUNC_READ_COILS)
modbus_read_discrete_input_rsp(MODBUS_ADDRESS, 0x01, &data);
else
modbus_read_discrete_input_rsp(MODBUS_ADDRESS, 0x01, &data); |
to
Code: | if(modbus_rx.func == FUNC_READ_COILS)
modbus_read_coils_rsp(MODBUS_ADDRESS, 0x01, &data);
else
modbus_read_discrete_input_rsp(MODBUS_ADDRESS, 0x01, &data); |
|
|
|
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
|
Posted: Wed Mar 07, 2012 11:41 pm |
|
|
working, thank you. _________________ Help "d" others and then you shell receive some help from "d" others. |
|
|
bbnnbb
Joined: 09 Mar 2012 Posts: 3
|
HELP |
Posted: Sun Mar 11, 2012 4:15 am |
|
|
HELLO SIR I HAVE THE SAME PROJECT AS YOU SO I NEED HELP ESPECIALY WICH SCADA SOFTWARE YOU USED AND IF POSSIBLE SEND ME THE INTERFACE.
++++++++++++++++++++
No more ALL CAPS in post.
No ALL CAPS in sigline.
- Forum Moderator
++++++++++++++++++++ _________________ HELLO EVERY BODY!,AM WORKING IN MY FINAL PROJECT IN IMPLEMENTATION OF THE MODBUS AND INTERFACING WITH HMI SO I NEED HELP IN MY PROJECT (I USE LIBRARY MODBUS OF CCS) |
|
|
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
|
Posted: Sun Mar 11, 2012 12:49 pm |
|
|
You can expect help but not entire solution. _________________ Help "d" others and then you shell receive some help from "d" others. |
|
|
|
|
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
|