View previous topic :: View next topic |
Author |
Message |
agross
Joined: 15 Apr 2012 Posts: 5
|
Modbus driver shenanigans |
Posted: Sun May 06, 2012 11:12 am |
|
|
My apologies to the moderators of the Library forum I posted this in the wrong spot.
Hello, hopefully there are some other friends of modbus out there who might be able to shed some light on to what I might be doing wrong. I have been working for the last week to try and get the CCS modbus.c driver working with very poor results. I am currently just trying to get the basics working but having my board blink if it has received the command to read holding registers. I have the board indicating it has initialized and it is in fact driving the 485 control pins low but that's as far as I seem to get. Whenever I send the packet from the master the slave just sits as if nothing has happened. I know the hardware is working correctly as I scoped the control pins and they are responding to my commands so I think my error is somewhere in the driver specifically the modbus_serial_receive function but I really am not sure what it is I might be doing wrong. It's so frustrating as I know what I want to do and I think I am on the right track and it seems so simple but I've got something goofed up and I just don't know were. Any input on the subject would be appreciated. Current code is below. FYI I am using simply Modbus 7.0 to test pack sending. Also if more information is required let me know. Thanks.
****************MAIN PROGRAM*********************
Code: |
#include "C:\Documents and Settings\Desktop\CCS FULL MODBUS DRIVER\Full driver.h"
#int_RDA
//RDA_isr()
//{
//}
//#int_TBE
//TBE_isr()
//{
//}
#define MODBUS_BUS SERIAL
#define MODBUS_TYPE SLAVE
#define MODBUS_SERIAL_RX_BUFFER_SIZE 8
#define MODBUS_SERIAL_RX_PIN PIN_C7
#define MODBUS_SERIAL_TX_PIN PIN_C6
#define MODBUS_SERIAL_ENABLE_PIN PIN_D0
#define MODBUS_SERIAL_RX_ENABLE PIN_D1
#define MODBUS_SERIAL_BAUD 19200
#define MODBUS_OUR_ADDRESS 3
#define RX_BUFFER_SIZE 8
#include "modbus.c"
int respond;
void main()
{
modbus_init();
output_high(PIN_J7);
delay_ms(1000);
output_low(PIN_J7);
delay_ms(1000);
while(TRUE)
{
while(!modbus_serial_receive(FALSE));
output_high(PIN_J7);
delay_ms(1000);
output_low(PIN_J7);
delay_ms(1000);
if(modbus_rx.address == MODBUS_OUR_ADDRESS){
respond = 1;
}
if(respond)
{
switch(modbus_rx.func)
{
case 0x01:
if(respond)
{
//read the coils
}
break;
case 0x02:
if(respond)
{
//read discrete inputs
}
break;
case 0x03:
if(respond)
{
//read holding registers
output_high(PIN_J5);
delay_ms(1000);
output_low(PIN_J5);
delay_ms(1000);
respond = 0;
}
break;
case 0x04:
if(respond)
{
//read input registers
}
break;
case 0x05:
break;
case 0x06:
break;
case 0x07:
break;
case 0x08:
break;
}
}
}
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_spi2(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
}
|
***********CURRENT DRIVER CONFIG FILE snippets**************
Code: |
//#define SERIAL 0
//#define TCP 1
#define MASTER 0
#define SLAVE 1
/*#ifndef MODBUS_BUS
#define MODBUS_BUS SERIAL
#endif*/
#ifndef MODBUS_TYPE
#define MODBUS_TYPE SLAVE
#endif
//#if (MODBUS_BUS==SERIAL)
//#include "modbus_serial.c"
//#else
//#include "modbus_tcp.c" //not implemented
//#endif
#ifndef MODBUS_SERIAL_USE_EXT_INT
#define MODBUS_SERIAL_USE_EXT_INT FALSE // Select between external interrupt
#endif // or asynchronous serial interrupt
#ifndef MODBUS_SERIAL_BAUD
#define MODBUS_SERIAL_BAUD 19200
#endif
#if( MODBUS_SERIAL_USE_EXT_INT == FALSE )
#ifndef MODBUS_SERIAL_RX_PIN
#define MODBUS_SERIAL_RX_PIN PIN_C6 // Data receive pin
#endif
#ifndef MODBUS_SERIAL_TX_PIN
#define MODBUS_SERIAL_TX_PIN PIN_C7 // Data transmit pin
#endif
#ifndef MODBUS_SERIAL_ENABLE_PIN
#define MODBUS_SERIAL_ENABLE_PIN PIN_D0 // Controls DE pin. RX low, TX high.
#endif
#ifndef MODBUS_SERIAL_RX_ENABLE
#define MODBUS_SERIAL_RX_ENABLE PIN_D1 // Controls RE pin. Should keep low.
#endif
#use rs232(baud=MODBUS_SERIAL_BAUD, xmit=MODBUS_SERIAL_TX_PIN, rcv=MODBUS_SERIAL_RX_PIN, parity=N, force_sw, stream=MODBUS_SERIAL)
#endif
#ifndef MODBUS_SERIAL_RX_BUFFER_SIZE
#define MODBUS_SERIAL_RX_BUFFER_SIZE 48 //size of send/rcv buffer
#endif
// Purpose: Get a message from the RS485 bus and store it in a buffer
// Inputs: 1) TRUE - wait for a message
// FALSE - only check if a message is available
// Outputs: TRUE if a message was received
// FALSE if wait is FALSE and no message is available
// Note: Data will be filled in at the modbus_rx struct:
int1 modbus_serial_receive(int1 wait)
{
//int8 i;
while(wait && (!modbus_serial_new));
if(!modbus_serial_new)
return FALSE;
else if(modbus_rx.func & 0x80)
{
modbus_rx.error = modbus_rx.data[0];
modbus_rx.len = 1;
}
return TRUE;
}
|
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun May 06, 2012 2:06 pm |
|
|
To start with a simple thing, can you tell which exact MODBUS driver version you are using? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Sun May 06, 2012 2:14 pm |
|
|
You must _never_ enable an interrupt without a driver present. Doing so, _will_ crash the chip. Remove the enable lines.
TBE is even worse. Look at how serial works. The transmit buffer will _always_ be empty, unless the interrupt handler loads something into the transmit register. The interrupt will happen for ever. This is why in the EX_STISR code the interrupt is _only_ enabled, when there _is_ data to send, and is disabled in the handler when there is no more data. You won't be running the code at all, but sitting looping to the non existent handler, continually restarting the chip.....
Best Wishes |
|
|
agross
Joined: 15 Apr 2012 Posts: 5
|
|
Posted: Tue May 08, 2012 11:57 am |
|
|
>Ttelmah
Not sure what you mean by version, have there been updates to their library? I'm looking over the modbus.c driver and didn't see anything indicating a version number. Also in regards to the TBE and ISR I didn't copy all the code correctly they they are commented out as I'm still learning how to use interrupts. I'm trying to work this problem on two ends one is to work directly using the modbus.c driver and the other is to bang the packet info into a USART using MPlabs and their C compiler. Appreciate the input and I'll update if I uncover anything new. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue May 08, 2012 1:47 pm |
|
|
Quote: | Not sure what you mean by version, have there been updates to their library? |
Yes.
As it's shipped with CCS C, referring to a CCS version would be informative. Newer versions have a kind of revision history. |
|
|
|