Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback function implementation for Coil/Hreg writes #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TomaTLAB
Copy link
Contributor

Hi, Andre. It maybe a powerful mechanism for range checking, storing a values in EEPROM, etc.

word callback_hr1 (word val, byte src)
{
  print("Callback HR1 rised from ");
  print(src);
  print(" with value ");
  println(val);
  return val;
}

word callback_coil1 (word val, byte src)
{
  if (mb.Coil(1) == (bool)val) return val; // nothing todo
  if ((bool)val) digitalWrite(LED_PIN, LOW); else digitalWrite(LED_PIN, HIGH); // change pin state
  return val;
}

void setup() 
{
//......
  mb.addHreg(1, 12345,  callback_hr1);
  mb.addHreg(2);
  mb.addHreg(3);
  mb.addHreg(4);
  mb.addCoil(1, false, callback_coil1);

//...........
// somewhere in the code
  mb.Hreg(1, 321); //     printout: "Callback HR1 rised from 0 with value 321"
  mb.Hreg(1, 123, 100); // printout: "Callback HR1 rised from 100 with value 123"
// writing 456 via modbus printout: "Callback HR1 rised from 6 with value 456"
//.....

Callback function implementation for Coil/Hreg writes
@wvengen
Copy link

wvengen commented Sep 8, 2016

I like the idea!
It could also bring a performance benefit since the linked list only needs to be traversed once.

@@ -65,8 +66,8 @@ class Modbus {

TRegister* searchRegister(word addr);

void addReg(word address, word value = 0);
bool Reg(word address, word value);
void addReg(word address, word value = 0, word (*cb)(word, byte) = 0);
Copy link

@wvengen wvengen Sep 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<nitpick>wouldn't = NULL be slightly more expressive?</nitpick>?

@wvengen
Copy link

wvengen commented Sep 8, 2016

I would have expected the second argument to the callback to be the from value, but reading the code I see it is a function code that initiated the change. Do you have a specific use-case for that?

@TomaTLAB
Copy link
Contributor Author

TomaTLAB commented Sep 8, 2016

Hi! I add this argument for tracking from where callback occur - inside the lib or in my own code, and make a decision on this info. I need to look in my code to remember for what I made it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants