41 if (device == NULL) {
return -1; }
65 static size_t index = 0;
91 ti = read(handle, &(buf[(*hw)]),
LPMS_BUFF - (*hw));
95 if (errno != EAGAIN) {
96 fprintf(stderr,
"Unexpected error while reading from serial port (handle ID: 0x%02x)\n",
98 fprintf(stderr,
"read returned \"%s\" in readMessage\n", strerror(errno));
105 if (((*hw) ==
LPMS_BUFF) && (*index) > 0 && (*index) > ((*hw) - 25)) {
108 memmove(buf, &(buf[(*index)]),
LPMS_BUFF - (*index));
112 if (ti == 0) { out->
id = 0xFD; }
121 if (ti == 0 && ((*hw) - (*index)) < 10) {
125 if ((*index) < (*hw)) { (*index)++; }
130 if ((*hw) > 0 && ((*hw) >= (*index))) {
132 memmove(buf, &(buf[(*index)]),
LPMS_BUFF - (*index));
135 memset(&(buf[(*hw)]), 0,
LPMS_BUFF - (*hw));
158 uint8_t buf[
LPMS_BUFF],
size_t *index,
size_t *hw) {
159 time_t start = time(NULL);
160 while (time(NULL) <= (start + timeout)) {
164 fprintf(stdout,
"%02x: Command 0x%02x received while waiting\n", t.
id, t.
command);
165 for (
unsigned int c = 0; c < numtypes; c++) {
167 if (out) { (*out) = t; }
186 if (!rs) {
return false; }
187 rs = (write(handle, arr, len) == (ssize_t) len);
bool lpms_readMessage_buf(int handle, lpms_message *out, uint8_t buf[LPMS_BUFF], size_t *index, size_t *hw)
Read data from handle, and parse message if able.
bool lpms_send_command(const int handle, lpms_message *m)
Write command defined by structure to handle.
bool lpms_find_messages(int handle, size_t numtypes, const uint8_t types[], int timeout, lpms_message *out, uint8_t buf[LPMS_BUFF], size_t *index, size_t *hw)
Read data from handle until first of specified message types is found.
#define LPMS_BUFF
Default serial buffer allocation size.
bool lpms_readMessage(int handle, lpms_message *out)
Static wrapper around lpms_readMessage_buf.
bool lpms_send_get_config(const int handle)
Shortcut: Send LPMS_MSG_GET_OUTPUTS.
void lpms_closeConnection(int handle)
Close LPMS serial connection.
int lpms_openConnection(const char *device, const int baud)
Open connection to an LPMS serial device.
bool lpms_send_stream_mode(const int handle)
Shortcut: Send LPMS_MSG_MODE_STREAM.
bool lpms_send_command_mode(const int handle)
Shortcut: Send LPMS_MSG_MODE_CMD.
bool lpms_checksum(const lpms_message *msg, uint16_t *csum)
Calculate checksum for LPMS message packet.
bool lpms_to_bytes(const lpms_message *msg, uint8_t **out, size_t *len)
Convert message structure to flat array.
bool lpms_from_bytes(const uint8_t *in, const size_t len, lpms_message *msg, size_t *pos)
Read bytes and populate message structure.
int openSerialConnection(const char *port, const int baudRate)
Open a serial connection at a given baud rate.
uint16_t checksum
Sum of all preceding message bytes.
uint8_t * data
Pointer to data array.
uint16_t id
Source/Destination Sensor ID.
uint16_t command
Message type.