CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Modbus kbhit() not triggering
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 17221

View user's profile Send private message

PostPosted: Mon Feb 22, 2021 11:42 am     Reply with quote

Change your clock line, to:

#use delay(INTERNAL=8000000)

Currently there is no guarantee that this will be the selected clock.

The default is not 8MHz, so it may well not be running at this rate.
webgiorgio



Joined: 02 Oct 2009
Posts: 111
Location: Denmark

View user's profile Send private message

PostPosted: Mon Feb 22, 2021 11:50 am     Reply with quote

Defining the RX TX pin is not mandatory, however I tried and it did not fix the issue.
Code:
#define MODBUS_SERIAL_TX_PIN PIN_C6
#define MODBUS_SERIAL_RX_PIN PIN_C7
webgiorgio



Joined: 02 Oct 2009
Posts: 111
Location: Denmark

View user's profile Send private message

PostPosted: Mon Feb 22, 2021 2:02 pm     Reply with quote

No improvement, it seems that both "internal" or "clock" works equally.

I toggle a LED in the while loop and get the following frequency on the output pin:
4M: 17.8 kHz
8M: 35.6 kHz
16M: 71 kHz
temtronic



Joined: 01 Jul 2010
Posts: 8095
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 22, 2021 2:43 pm     Reply with quote

Ok, How about trying the ex_sisr.c example program that CCS supplies ?
It does work and be a good test to confirm/deny whether it's a 'hardware' or a 'software' problem.
Any PC terminal prgram will work with it, just need a TTL<>USB adapter.
webgiorgio



Joined: 02 Oct 2009
Posts: 111
Location: Denmark

View user's profile Send private message

PostPosted: Tue Feb 23, 2021 5:19 am     Reply with quote

That example works!
I only changed the top defines to
Code:
#include <16F1788.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


Without changing any connection I tried again the modbus example,
Code:

#include <16F1788.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8M)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#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_INT_SOURCE MODBUS_INT_RDA
#define MODBUS_ADDRESS 0x02
#include <modbus.c>


and it doesn't work.
So the problem is in the modbus library. Sad
temtronic



Joined: 01 Jul 2010
Posts: 8095
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 23, 2021 5:32 am     Reply with quote

You should ( well need actually) to add 'errors' to the #USE RS232 ( ...options....). This will prevent the UART from 'locking up' after 3 characters arrive and not processed.

I can't see the MODBUS library being the problem as the exact same code worked on another PIC.
webgiorgio



Joined: 02 Oct 2009
Posts: 111
Location: Denmark

View user's profile Send private message

PostPosted: Tue Feb 23, 2021 5:58 pm     Reply with quote

We have a solution, thanks to a hint from CCS support.
The parity!!! Very Happy

My PC was set to None, but the default for the modbus library is to Odd.
So, setting it to Odd was enough to make it work. So damn simple solution, as it often happens. Confused

Code:
 //the parity used by the Modbus driver can be change by making one of the defined before the driver is included:

#define MODBUS_PARITY   "NONE"
#define MODBUS_PARITY   "EVEN"
#define MODBUS_PARITY   "ODD"


I wish this was mentioned in the modbus example instructions.
Now I am using a TTL-USB serial converter, hence full duplex. I will test tomorrow with the MAX485.
temtronic



Joined: 01 Jul 2010
Posts: 8095
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 23, 2021 6:03 pm     Reply with quote

grrrr....well at least you found it !!!!

The odds are that '8 bit, NO parity' is probably the default for 99.44% of all programs since the hardware UART was invented.....

Have no idea why MODbus would be different than 'industry standard'.
Ttelmah



Joined: 11 Mar 2010
Posts: 17221

View user's profile Send private message

PostPosted: Wed Feb 24, 2021 12:36 am     Reply with quote

This actually depends on what mode you are using.
Modbus for most modes uses no parity, but Modbus in V1.02, specifically
says that parity must be 'even' in RTU mode.
However a lot of MODBUS stuff ignores this, and instead used a second
stop bit.
webgiorgio



Joined: 02 Oct 2009
Posts: 111
Location: Denmark

View user's profile Send private message

PostPosted: Thu Feb 25, 2021 3:46 pm     Reply with quote

It works also with the MAX485.
Thanks for the help!
One more thing I can do with PIC rather than Arduino compatible chips. Wink

To only use one pin to control the direction:
Code:
//#define MODBUS_SERIAL_RX_ENABLE     PIN_B4   // Controls RE pin for RS485. Comment out if RE-DE are connected together.
#define MODBUS_SERIAL_ENABLE_PIN    PIN_B5   // Controls DE pin for RS485
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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