11#if DETWS_ENABLE_PROFIBUS
15uint8_t detws_pb_fcs(
const uint8_t *bytes,
size_t len)
18 for (
size_t i = 0; i < len; i++)
19 sum = (uint8_t)(sum + bytes[i]);
23size_t detws_pb_build_sd1(uint8_t da, uint8_t sa, uint8_t fc, uint8_t *out,
size_t cap)
27 out[0] = Profibus::PB_SD1;
31 uint8_t body[3] = {da, sa, fc};
32 out[4] = detws_pb_fcs(body, 3);
33 out[5] = Profibus::PB_ED;
37size_t detws_pb_build_sd2(uint8_t da, uint8_t sa, uint8_t fc,
const uint8_t *data,
size_t data_len, uint8_t *out,
40 if (!out || (data_len && !data) || data_len > 246)
43 size_t n = 4 + 3 + data_len + 2;
46 uint8_t le = (uint8_t)(3 + data_len);
48 out[i++] = Profibus::PB_SD2;
51 out[i++] = Profibus::PB_SD2;
57 memcpy(out + i, data, data_len);
61 out[i++] = detws_pb_fcs(out + 4, le);
62 out[i++] = Profibus::PB_ED;
66bool detws_pb_parse(
const uint8_t *frame,
size_t len, PbTelegram *out)
68 if (!frame || !out || len < 6)
71 if (frame[0] == Profibus::PB_SD1)
74 uint8_t body[3] = {frame[1], frame[2], frame[3]};
75 if (detws_pb_fcs(body, 3) != frame[4] || frame[5] != Profibus::PB_ED)
77 out->sd = Profibus::PB_SD1;
85 if (frame[0] == Profibus::PB_SD2)
90 uint8_t le = frame[1];
91 if (frame[2] != le || frame[3] != Profibus::PB_SD2)
95 size_t total = 4 + le + 2;
98 if (detws_pb_fcs(frame + 4, le) != frame[4 + le] || frame[4 + le + 1] != Profibus::PB_ED)
100 out->sd = Profibus::PB_SD2;
105 out->data = dl ? (frame + 7) : nullptr;
PROFIBUS-DP FDL telegram codec (DETWS_ENABLE_PROFIBUS).