11#if DETWS_ENABLE_INTERBUS
13uint16_t detws_interbus_fcs(
const uint8_t *bytes,
size_t len)
16 uint16_t crc = 0xFFFF;
17 for (
size_t i = 0; i < len; i++)
19 crc ^= (uint16_t)bytes[i] << 8;
20 for (
int b = 0; b < 8; b++)
21 crc = (crc & 0x8000) ? (uint16_t)((crc << 1) ^ 0x1021) : (uint16_t)(crc << 1);
26size_t detws_interbus_build(
const uint16_t *words,
size_t word_count, uint8_t *out,
size_t cap)
28 if (!out || (word_count && !words))
30 size_t n = 2 + word_count * 2 + 2;
34 out[i++] = (uint8_t)(INTERBUS_LOOPBACK >> 8);
35 out[i++] = (uint8_t)INTERBUS_LOOPBACK;
36 for (
size_t w = 0; w < word_count; w++)
38 out[i++] = (uint8_t)(words[w] >> 8);
39 out[i++] = (uint8_t)words[w];
41 uint16_t crc = detws_interbus_fcs(out, i);
42 out[i++] = (uint8_t)(crc >> 8);
43 out[i++] = (uint8_t)crc;
47bool detws_interbus_parse(
const uint8_t *frame,
size_t len, uint16_t *out_words,
size_t max_words,
size_t *out_count)
49 if (!frame || !out_words || !out_count || len < 4)
51 if (((frame[0] << 8) | frame[1]) != INTERBUS_LOOPBACK)
53 if ((len - 4) % 2 != 0)
55 size_t word_count = (len - 4) / 2;
56 if (word_count > max_words)
58 uint16_t want = detws_interbus_fcs(frame, len - 2);
59 uint16_t got = (uint16_t)((frame[len - 2] << 8) | frame[len - 1]);
62 for (
size_t w = 0; w < word_count; w++)
63 out_words[w] = (uint16_t)((frame[2 + w * 2] << 8) | frame[2 + w * 2 + 1]);
64 *out_count = word_count;
INTERBUS summation-frame fieldbus codec (DETWS_ENABLE_INTERBUS).