1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #include "NodeLinkCommon.h"
- #include "MathHelper.h"
- /**
- * 数据编码
- *
- * @author lxz (041820 14:21:27)
- *
- * @param slave
- * @param dst
- * @param src
- * @param len
- *
- * @return int
- */
- int nodelink_decode(unsigned char *id, unsigned char *dst, unsigned char *src, int len) {
- int res = 0;
- if (len >= 4) {
- *id = src[0] & 0xf0;
- while (len > 1) {
- dst[res] = ((src[res * 2] & 0x0f) << 4) + ((src[res * 2 + 1] & 0x0f));
- res++;
- len -= 2;
- }
- if (mh_crc8_calc(dst, res) == 0) {
- return res - 1;
- }
- }
- return 0;
- }
- /**
- * 数据解码
- *
- * @author lxz (041820 14:22:05)
- *
- * @param id
- * @param dst
- * @param src
- * @param len
- *
- * @return int
- */
- int nodelink_encode(char id, unsigned char *dst, unsigned char *src, int len) {
- int res = 0;
- unsigned char crc = 0;
- crc = mh_crc8_calc(src, len);
- res = (len + 1) * 2;
- dst[len * 2] = ((crc >> 4) & 0x0f) | id;
- dst[len * 2 + 1] = (crc & 0x0f) | id;
- while (len--) {
- dst[len * 2 + 1] = (src[len] & 0x0f) | id;
- dst[len * 2] = ((src[len] >> 4) & 0x0f) | id;
- }
- return res;
- }
|