| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /*
- * NbusBridge.cpp
- *
- * Created on: Mar 7, 2025
- * Author: juraj
- */
- #include "NbusBridge.h"
- #include "dataframe.h"
- NbusBridge::NbusBridge(NbusCommunicator *nc) {
- if(nc == NULL)
- {
- while(1){
- __NOP();
- }
- }
- _communicator = nc;
- _num_slaves = 0;
- for(uint32_t i = 0 ; i<MAX_SLAVES ; i++){
- _slaves[i] = NULL;
- }
- }
- NbusBridge::~NbusBridge() {
- // TODO Auto-generated destructor stub
- }
- bool NbusBridge::addSlave(NbusSlave *slave){
- if(_num_slaves >= MAX_SLAVES){
- return false;
- }
- _slaves[_num_slaves++] = slave;
- return true;
- }
- void NbusBridge::scan(){
- _num_slaves = 0;
- DataFrame *frame;
- uint8_t *response;
- Nbus_pdu pdu;
- pdu.fc = FC_ECHO;
- pdu.sa = SLAVE_ADDRESS_MODULE;
- uint8_t data[4] = {110, 66, 117, 115}; // nBus
- uint8_t detected_slaves[MAX_SLAVES];
- const uint8_t data_offset = 4;
- for(uint32_t i = 1 ; i < MAX_SLAVES; i++){
- pdu.ma = i;
- frame = _communicator->send(&pdu, data, 4);
- if (!frame->IsEmpty()){
- response = frame->GetFrame();
- if(response[0+data_offset] == 110 && response[1+data_offset] == 66 && response[2+data_offset] == 117 && response[3+data_offset] == 115) {
- detected_slaves[_num_slaves] = i;
- _num_slaves++;
- }
- }
- }
- NbusSlave *slave;
- for(uint32_t i = 0 ; i < _num_slaves; i++){
- slave = new NbusSlave(detected_slaves[i], _communicator);
- if (slave != NULL){
- _slaves[i] = slave;
- }
- }
- }
- NbusSlave * NbusBridge::getSlave(uint8_t index){
- if(index >= 0 && index<_num_slaves){
- return _slaves[index];
- }
- return NULL;
- }
- uint8_t NbusBridge::getNumSlaves(){
- return _num_slaves;
- }
- bool NbusBridge::call_echo(uint8_t slave){
- if(slave >= _num_slaves){
- return false;
- }
- if(slave == 0){
- for(uint32_t i = 0 ; i<_num_slaves ; i++){
- _slaves[i]->nbus_echo();
- }
- return true;
- }
- _slaves[slave]->nbus_echo();
- return true;
- }
- void NbusBridge::sendResponseToMaster(DataFrame *response_frame){
- if(response_frame !=NULL && response_frame->IsEmpty() == false) {
- _communicator->sendToMaster(response_frame);
- }
- }
|