Hello,
I am writing here with the hope of some clarification to a question that has been struggling me. I want to configure my arduino Uno as a modbus slave and i want to make it count some pulses divide them to a smaller value (example 100 pulses / 10 = 10 pulses) before the master reads the data. So basically i want to make the master read the data after i have run some code on the arduino. So my question is if it is even possible to divide the data before the master makes a request to read it from the arduino? And if it is possible can someone give me a link to a working and tested modbus slave library for the arduino? Thanks in advance.
Yes, it is possible to manipulate (to almost any degree) the sensor readings before presenting them to the ModBus library.
There are several ModBus library available for the Arduino, most of them support (only) the slave mode. All library I tested have some limitations but for almost any situation you'll find a library that fits that need. Please describe your needs and maybe the needs of your master.
I use a slightly modified version of this library.
If you master complies completely with the standard a UNO might not be the best choice as you'll need the hardware serial for the ModBus connection (the software emulations don't support parity bits), so you don't have a debugging channel. For development a Mega2560 or Leonardo is a much better choice.
I've found this library developed specially for Mach3 and Arduino modbus communication. Mach3 is a CNC software. Mach3 is configured as Modbus master and the arduino as Modbus slave. The library has a pdf documentation made for the purpose.
here is a link - https://www.machsupport.com/forum/index.php?topic=21105.0
I can't simply understand where in between the code for the arduino to add a code to manipulate the data from an input and then the master to read it.
What hardware do you use on the PC side? What hardware do you use on the Arduino side (other than the Arduino itself)? The ModBus standard defines RS-485 as the physical layer.
The library has a pdf documentation made for the purpose.
Do you have a link to that library (might be in the thread you linked to but I don't read 100+ messages just to find a link you should provide).
I can't simply understand where in between the code for the arduino to add a code to manipulate the data from an input and then the master to read it.
Post that Arduino code you're experimenting with!
The library is on the bottom of the first post in the link i posted. It is a direct download link. As i said the author has written the code on the arduino side. I just change the values in his code so i can configure the pins to be inputs/outputs and analog/digital/pwm pins, everything else is done by the author. I need help to understand where in his code i can put a few lines of my own before the master reads this data. I have uploaded pdfs with the authors code on the arduino. The pc and arduino communicate via USB.
ModBusSlave.pdf (23.6 KB)
Pin_Manipulator.pdf (26.9 KB)
Never post code as PDFs! It's completely unreadable.
I had a short look at the Modbus_Slave library available using the link in the forum. Although the post is done in 2012 the code is written for a pre-1.0 version of the IDE, so it has to be change, at least a little bit, to make it run with a current IDE.
The sketch does not include any pulse counting, it simply shows the state of the digital pins and the readings of the analog inputs in ModBus registers and allows digital outputs to be set as well as PWMs based on the registers.
So post the code you use to get your pulse counts and the manipulation and I will try to tell you where to insert it into that "framework".
Yes i know it needs to be changed. The #include Wire.h has to be changed with Arduino.h or it was the other way around. I know what the current code does, i just want to add a simple pulse counter to a pin.
So the code i want to implement should be something like this (don't judge me too hard, i don't have much experience in arduino programming).
void loop()
{
for(int i=0; digitalRead(SomePin)==HIGH; i=i+1)
{
//Here the modbusmaster reads the value of i
}
}
Please tell us what you're trying to achieve, not how you want to achieve it. What kind of pulses do you try to count. What time frame should they be in? How often do you want to count? What is this value used for?
The code you provided does not count pulses but does a very rough estimation of the duration of one pulse.
I want to count the pulses from a incremental encoder, the encoder ppr will be any value ranging from 100 ppr - 1000 ppr. The pulse count should be constant on every on pulse until an input is activated which will reset the pulse count. This value is then read by the master and compared with another value which also counts pulses.
That means you don't need the rest of the code of the example you linked to? So Pin_Manipulator.pde is not used at all?
That code looks like someone designed the register addresses to follow an exact scheme probably defined by some mach3 code part. Are you free to use any register address for your own values?
I want to count the pulses from a incremental encoder, the encoder ppr will be any value ranging from 100 ppr - 1000 ppr.
I guess "ppr" means pulses per round. What speed do you expect, so how many pulses per second are to be expected?
To update the code: Remove Update_Pin_States() and Update_AN_States() calls in the loop() routine and insert your own code there. How that code has to look depends on your answers to above questions.