NbusBridge.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * NbusBridge.cpp
  3. *
  4. * Created on: Mar 7, 2025
  5. * Author: juraj
  6. */
  7. #include "NbusBridge.h"
  8. #include "dataframe.h"
  9. NbusBridge::NbusBridge(NbusCommunicator *nc) {
  10. if(nc == NULL)
  11. {
  12. while(1){
  13. __NOP();
  14. }
  15. }
  16. _communicator = nc;
  17. _num_slaves = 0;
  18. for(uint32_t i = 0 ; i<MAX_SLAVES ; i++){
  19. _slaves[i] = NULL;
  20. }
  21. }
  22. NbusBridge::~NbusBridge() {
  23. // TODO Auto-generated destructor stub
  24. }
  25. bool NbusBridge::addSlave(NbusSlave *slave){
  26. if(_num_slaves >= MAX_SLAVES){
  27. return false;
  28. }
  29. _slaves[_num_slaves++] = slave;
  30. return true;
  31. }
  32. void NbusBridge::scan(){
  33. _num_slaves = 0;
  34. DataFrame *frame;
  35. uint8_t *response;
  36. Nbus_pdu pdu;
  37. pdu.fc = FC_ECHO;
  38. pdu.sa = SLAVE_ADDRESS_MODULE;
  39. uint8_t data[4] = {110, 66, 117, 115}; // nBus
  40. uint8_t detected_slaves[MAX_SLAVES];
  41. for(uint32_t i = 1 ; i < MAX_SLAVES; i++){
  42. pdu.ma = i;
  43. frame = _communicator->send(&pdu, data, 4);
  44. if (!frame->IsEmpty()){
  45. response = frame->GetFrame();
  46. if(response[5] == 110 && response[6] == 66 && response[7] == 117 && response[8] == 115) {
  47. detected_slaves[_num_slaves] = i;
  48. _num_slaves++;
  49. }
  50. }
  51. }
  52. NbusSlave *slave;
  53. for(uint32_t i = 0 ; i < _num_slaves; i++){
  54. slave = new NbusSlave(detected_slaves[i], _communicator);
  55. if (slave != NULL){
  56. _slaves[i] = slave;
  57. }
  58. }
  59. }
  60. NbusSlave * NbusBridge::getSlave(uint8_t index){
  61. if(index >= 0 && index<_num_slaves){
  62. return _slaves[index];
  63. }
  64. return NULL;
  65. }
  66. uint8_t NbusBridge::getNumSlaves(){
  67. return _num_slaves;
  68. }
  69. bool NbusBridge::call_echo(uint8_t slave){
  70. if(slave >= _num_slaves){
  71. return false;
  72. }
  73. if(slave == 0){
  74. for(uint32_t i = 0 ; i<_num_slaves ; i++){
  75. _slaves[i]->nbus_echo();
  76. }
  77. return true;
  78. }
  79. _slaves[slave]->nbus_echo();
  80. return true;
  81. }