send-cmd

gabbapeople/sim7020/send-cmd

Sends an AT command to sim7020 module.
send-cmd
@/send-cmd
Sends an AT command to sim7020 module.
UARTxod/uart/uart
An UART interface to which SIM7020 module is wired.
Tnumber
Timeout in seconds to receive a response from module.
CAPnumber
The capacity of the response string buffer. Defines the maximum length. Must be a constant value.
CMDstring
String that contains an AT command to send.
SENDpulse
Triggers writting of the AT command.
send-cmd
RESP
DONE
OK
ERR
TOUT
UART
T
CAP
CMD
SEND
TOUTpulse
Pulses when no response is received after a timeout.
ERRpulse
Pulses when the response is received and the response string contains the "ERROR" char sequence.
OKpulse
Pulses when the response is received and the response string contains the "OK" char sequence.
DONEpulse
Pulses when the response is received.
RESPstring
A string that contains the AT command response. May contain CR and LF terminal sequences.
To use the node in your project you should have the gabbapeople/sim7020 library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

// {{#global}}
// #ifdef SAMD_SERIES
// void* operator new(size_t, void* ptr) {
//     return ptr;
// }
// #endif
// {{/global}}

struct State {
  char* buf;
  size_t cap;
  CStringView view;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
  auto state = getState(ctx);

  if (isSettingUp()) {
    state->cap = getValue<input_CAP>(ctx);
    state->buf = new char[state->cap + 1];
    memset(state->buf, '\0', state->cap + 1);
    state->view = CStringView(state->buf);
  }

  if (isInputDirty<input_SEND>(ctx)) {
    memset(state->buf, '\0', state->cap + 1);

    auto uartCtrl = getValue<input_UART>(ctx);
    auto timeOut = getValue<input_T>(ctx);

    auto cmd = getValue<input_CMD>(ctx);
    uint8_t len = length(cmd);

//       XOD_DEBUG_SERIAL.print(len);
//       XOD_DEBUG_SERIAL.print('\r');
//       XOD_DEBUG_SERIAL.print('\n');

    char *_cmd = (char*)malloc(sizeof(char) * (len + 2));
    memset(_cmd, '\0', len + 2);
    _cmd[len] = '\r';
    _cmd[len + 1] = '\n';
    dump(cmd, _cmd);

    uartCtrl->flush();
    for (auto it = 0; it < len + 2; it++) {
//         XOD_DEBUG_SERIAL.print((uint8_t)_cmd[it]);
//         XOD_DEBUG_SERIAL.print(' ');
        uartCtrl->writeByte((uint8_t)_cmd[it]);
    }

//       XOD_DEBUG_SERIAL.print('\r');
//       XOD_DEBUG_SERIAL.print('\n');

    free(_cmd);

    uint16_t n = 0;
    uint16_t t = 0;
    uint8_t byte = 0x00;

    char* seqOK = "OK";
    char* seqERROR = "ERROR";

    while (n <= timeOut) {
      if (uartCtrl->available()) {
        if (uartCtrl->readByte(&byte)) {
          state->buf[t] = byte;
          state->buf[++t] = 0;
        }
      } else {
        n++;
        delay(100);
      }
      char* ptr;
      ptr = strstr(state->buf, seqOK);
      if (ptr != NULL) {
        emitValue<output_OK>(ctx, 1);
        break;
      }
      ptr = strstr(state->buf, seqERROR);
      if (ptr != NULL) {
        emitValue<output_ERR>(ctx, 1);
        break;
      }
    }

    if (n >= timeOut){
        emitValue<output_TOUT>(ctx, 1);
    }
    emitValue<output_RESP>(ctx, XString(&state->view));
    emitValue<output_DONE>(ctx, 1);
  }
}