View previous topic :: View next topic |
Author |
Message |
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
using ex_modbus_slave.c |
Posted: Sun Apr 27, 2008 10:57 pm |
|
|
I am trying to implement a modbus type comms between a PIC and a PC program that communicate using modbus. I have been staring at the examples all day but still am not sure how to setup so i can use pins on the micro as inputs or outputs with respect to the modbus. I need on/off and analog inputs and on/off outputs.
any help appreciated |
|
|
Yalis Guest
|
Using Modbus example |
Posted: Sat May 03, 2008 11:17 am |
|
|
Make sure you read the modbus protocol specifications
Get a 'free' modbus client like modbus poll
Check bitrate and modbus Address #define MODBUS_ADDRESS 123
Get communication going between modbus-poll and your PIC.
Below you will see the PIC data that is send to the modbus client.
Simply use these variable to send you own data .
Code: | int8 coils = 0b00000101;
int8 inputs = 0b00001001;
int16 hold_regs[] = {0x8800,0x7700,0x6600, SNIP
int16 input_regs[] = {0x1100,0x2200,0x3300,0x4400, SNIP |
Note the bug in ex_modbus_slave.c
Code: | if(modbus_rx.func == FUNC_READ_COILS)
//Bugfix modbus_read_discrete_input_rsp(MODBUS_ADDRESS, 0x01, &data);
modbus_read_coils_rsp(MODBUS_ADDRESS, 0x01, &data);
|
|
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Sat May 03, 2008 1:55 pm |
|
|
thanks yalis
have had a look at the specs and sort of understood them but was unsure how to set them on the pic.
is all i have to do is have an if (input(pin_a1) set inputs = something if i want to have an input button? what if i want more than 8 inouts or outputs?
is the input regs for storing adc values and the likes? and the hold regs for recieving 16bit values?
once again thanks for the help |
|
|
magnoedu Guest
|
about modbus example |
Posted: Wed May 27, 2009 8:21 pm |
|
|
I got my pc communicate with the Modbus slave example under rs232, I reply with ok, but I can not turn-on an output (function 5), read the condition of an input (function 2), read an analog input (function 4) and send a value for PWM (function 6) and etc. I think you have to assign a pin to a register is that?
please help-me
+++++++++++++++++++
Do not make duplicate posts to five threads.
See forum rule #8:
8. No spamming
Four posts deleted.
-- Forum Moderator
+++++++++++++++++++ |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu May 28, 2009 1:41 am |
|
|
Quote: | I think you have to assign a pin to a register is that? |
Quite right. The example code doesn't interface real inputs and outputs.
Obviously, it has been written under the assumption, that you are familar with the basic operation of a microprocessor and can supplement the missing link easily.
Somethink like calling the below code in the main loop:
Code: | inputs = input_a():
output_b(coils); |
|
|
|
magnoedu
Joined: 29 May 2009 Posts: 11
|
sorry |
Posted: Fri May 29, 2009 3:15 pm |
|
|
I'm newbie.
But I sure do not have to use a register of the type
Code: |
leds =coils;
portb =leds;
trisb(0);
|
besides
Code: | #use fast_io (b)
#byte portb=8 |
Please help me I'm getting nowhere. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri May 29, 2009 4:09 pm |
|
|
Unfortunately I'm not aware of a PIC device, for that this assignment would be valid. Where did you get it? |
|
|
magnoedu
Joined: 29 May 2009 Posts: 11
|
donĀ“t give up to me help-me please |
Posted: Fri May 29, 2009 9:12 pm |
|
|
I am studying very hard to try to learn, but I live here in Brazil. Things are difficult so everyone thinks about money and not help. I'm driving in what I think on the internet.
I put your code but it just did this
Code: | int8 coils = 0b00000101; |
lighting LEDs that are on the respective pins rb_2, example rb_0 and even if I send the command 5 to turn them off, there is no response to command.
please help me |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat May 30, 2009 3:15 am |
|
|
I understand, that your LEDs are reflecting the initialized value of the coils variable but can't be changed through MODBUS. If so, you apparently managed to connect the LEDs to the variable successful, but the MODBUS write to coils seems not work. Then there wouldn't be a problem of connecting to outputs rather than general MODBUS slave operation. |
|
|
magnoedu
Joined: 29 May 2009 Posts: 11
|
modbus is work fine |
Posted: Sat May 30, 2009 10:04 am |
|
|
no communication is ok and so learn how to connect the Modbus and pin.
I am sending a command modbus response correct communication
speaks in code set for the bit of the port.
Code: |
else
{
// coils are stored msb->lsb so we must use 7-address
if(modbus_rx.data[2] == 0xFF)
bit_set(coils, 7-modbus_rx.data[1]);
else
bit_clear(coils, 7-modbus_rx.data[1]);
|
The code says to set bit 7, or set bit number with charged in modbus_rx.data[1] |
|
|
magnoedu
Joined: 29 May 2009 Posts: 11
|
I put the instructions |
Posted: Wed Jun 03, 2009 11:59 am |
|
|
I put the instructions in the code
Code: | while(TRUE)
{
inputs = input_a();
output_b(coils);
|
Now I have control for this pins in port_b but have no control for inputs.
Please help me. |
|
|
hugo_br
Joined: 01 Aug 2011 Posts: 26 Location: BRAZIL
|
|
Posted: Mon Sep 09, 2013 7:47 am |
|
|
I'm using Modbus slave routine example for testing, but it does not work. I'm using the PC Modbus Poll. The PC transmits and not get anything back. The PC program only sends and PIC returns nothing.
Would have any suggestions to make this example work?
Actually I need to send 2 analog channels for a device master. Getting run the sample program already help.
Thank you all. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Sep 09, 2013 8:06 am |
|
|
I'd say the most likely problem is with the RS485 control.
Try some simple 'non modbus' tests, using the same hardware. Can the PC send data to the PIC?. Can the PIC send data to the PC?. What does each receive when nothing is sending?. Get the comm's working, before trying to use the example. It does work (or it did several months ago, when I last tried it....).
Best Wishes |
|
|
hugo_br
Joined: 01 Aug 2011 Posts: 26 Location: BRAZIL
|
|
Posted: Mon Sep 09, 2013 8:27 am |
|
|
Good morning Ttelmah
I did a routine with the same hardware running as Master. And when sending commands it returns normally. I used the slave Modbus Poll to see if the data was coming and everything is normal.
I do not understand why when I record the slave routine does not work anything.
I do not know what else to do, I've done everything and nothing .....
I really need help.
Thank you. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Sep 09, 2013 8:48 am |
|
|
What compiler version are you on?.
Best Wishes |
|
|
|