This example demonstrates RS-485/RS-232 interface communication between an external device and the FlexSense Device. This also demonstrates a basic modem application. Operates as follows:
- The external device wakes up the module by setting the selected WakeupPin Low.
- Waits for the "READY\n" string from the FlexSense Device.
- Sends message string terminated by "\n".
- Sets the selected WakeupPin High again.
- The FlexSense Device acknowledges reception with string "\nOK\n".
#include <stdio.h>
#include <string.h>
#include "flex.h"
#define READY_STRING "READY\n"
#define RECEIVE_TIMEOUT_MS 2000
#define ACK_STRING "\nOK\n"
#define BAUDRATE 115200
#define USING_RS232 0
#define USING_RS485 1
#define RX_BUFFER_MAX 20
#if SERIAL_INTERFACE == USING_RS485
#define SERIAL_PROTOCOL FLEX_SERIAL_PROTOCOL_RS485
#define APPLICATION_NAME "RS485 Example"
#elif SERIAL_INTERFACE == USING_RS232
#define SERIAL_PROTOCOL FLEX_SERIAL_PROTOCOL_RS232
#define APPLICATION_NAME "RS232 Example"
#else
#error "Must supply a valid 'SERIAL_INTERFACE' to the build!"
#endif
int ReadStringWithTimeout(uint8_t *Rx, size_t MaxLength) {
size_t count = 0;
uint8_t ch;
if (ch == '\n')
return count;
Rx[count++] = ch;
if (count == MaxLength)
return -1;
}
}
return -1;
}
static void Comm() {
printf("Failed to initialise Serial interface\n");
return;
}
uint8_t Rx[RX_BUFFER_MAX] = {0};
int len = ReadStringWithTimeout(Rx, RX_BUFFER_MAX);
if (len <= 0) {
printf("Failed to receive message\n");
} else {
printf("Received message: ");
for (int i = 0; i < len; i++)
printf("%02x", Rx[i]);
printf("\n");
}
}
void FLEX_AppInit() {
printf("%s\n", APPLICATION_NAME);
}