NbusBridge.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const uint8_t data_offset = 4;
  42. for(uint32_t i = 1 ; i < MAX_SLAVES; i++){
  43. pdu.ma = i;
  44. frame = _communicator->send(&pdu, data, 4);
  45. if (!frame->IsEmpty()){
  46. response = frame->GetFrame();
  47. if(response[0+data_offset] == 110 && response[1+data_offset] == 66 && response[2+data_offset] == 117 && response[3+data_offset] == 115) {
  48. detected_slaves[_num_slaves] = i;
  49. _num_slaves++;
  50. }
  51. }
  52. }
  53. NbusSlave *slave;
  54. for(uint32_t i = 0 ; i < _num_slaves; i++){
  55. slave = new NbusSlave(detected_slaves[i], _communicator);
  56. if (slave != NULL){
  57. _slaves[i] = slave;
  58. }
  59. }
  60. }
  61. NbusSlave * NbusBridge::getSlave(uint8_t index){
  62. if(index >= 0 && index<_num_slaves){
  63. return _slaves[index];
  64. }
  65. return NULL;
  66. }
  67. uint8_t NbusBridge::getNumSlaves(){
  68. return _num_slaves;
  69. }
  70. bool NbusBridge::call_echo(uint8_t slave){
  71. if(slave >= _num_slaves){
  72. return false;
  73. }
  74. if(slave == 0){
  75. for(uint32_t i = 0 ; i<_num_slaves ; i++){
  76. _slaves[i]->nbus_echo();
  77. }
  78. return true;
  79. }
  80. _slaves[slave]->nbus_echo();
  81. return true;
  82. }
  83. void NbusBridge::sendResponseToMaster(DataFrame *response_frame){
  84. if(response_frame !=NULL && response_frame->IsEmpty() == false) {
  85. _communicator->sendToMaster(response_frame);
  86. }
  87. }