modbmst.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stddef.h>
  5. #include <byteswap.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <errno.h>
  9. #include <gfa/gfaserial.h>
  10. #include "output.h"
  11. #include "error.h"
  12. #include "modbmst.h"
  13. // Table of CRC values for high-order byte
  14. static const uint8_t g_crcHi[] =
  15. {
  16. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  17. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  18. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  19. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  20. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  21. 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  22. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  23. 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  24. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  25. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  26. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  27. 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  28. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  29. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  30. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  31. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  32. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  33. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  34. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  35. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  36. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  37. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  38. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  39. 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  40. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  41. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
  42. };
  43. // Table of CRC values for low-order byte
  44. static const uint8_t g_crcLo[] =
  45. {
  46. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
  47. 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
  48. 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
  49. 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
  50. 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
  51. 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  52. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
  53. 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
  54. 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
  55. 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
  56. 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
  57. 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  58. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
  59. 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
  60. 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
  61. 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
  62. 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
  63. 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  64. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
  65. 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
  66. 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
  67. 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
  68. 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
  69. 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  70. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
  71. 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
  72. };
  73. /////////////////////////////////////////////////////////////////////////////
  74. #define _PING_SIGNATURE 0xA55A
  75. /////////////////////////////////////////////////////////////////////////////
  76. typedef struct _GFA_MB_MST
  77. {
  78. HGFAMINEMST hMiNeMst;
  79. HMINETDEV hMiNeDev;
  80. int nVerbosity;
  81. FILE *pDumpCtx;
  82. uint32_t nBaudrate;
  83. uint16_t nModbusCtrlReg;
  84. struct timespec tsStart;
  85. uint8_t parity;
  86. }GFA_MB_MST, *LPGFA_MB_MST;
  87. typedef const GFA_MB_MST *LPCGFA_MB_MST;
  88. /////////////////////////////////////////////////////////////////////////////
  89. const struct timespec* Ns2Timespec(uint64_t nsTime, struct timespec *pts);
  90. int64_t TimespecDiff(const struct timespec *pts1, const struct timespec *pts2);
  91. /////////////////////////////////////////////////////////////////////////////
  92. static uint16_t _Crc16(const void *pData, size_t nCbData)
  93. {
  94. const uint8_t *pBuffer = (const uint8_t*)pData;
  95. uint8_t crcHi = 0xFF; // high CRC byte initialized
  96. uint8_t crcLo = 0xFF; // low CRC byte initialized
  97. unsigned int i; // will index into CRC lookup
  98. while(nCbData--)
  99. {
  100. i = crcHi ^ *pBuffer++;
  101. crcHi = crcLo ^ g_crcHi[i];
  102. crcLo = g_crcLo[i];
  103. }
  104. return (((uint16_t)crcHi << 8) | (uint16_t)crcLo);
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. static void _CpyUnalignedUint16(void *pTo, const void *pFrom, size_t nCntWords)
  108. {
  109. size_t i, j;
  110. uint8_t *p1 = (uint8_t*)pTo;
  111. const uint8_t *p2 = (const uint8_t*)pFrom;
  112. for(i = 0; i < nCntWords; i++)
  113. {
  114. p2 += sizeof(uint16_t);
  115. for(j = 0; j < sizeof(uint16_t); j++)
  116. {
  117. *p1++ = *--p2;
  118. }
  119. p2 += sizeof(uint16_t);
  120. }
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. HGFAMBMST GfaMbMstOpen(HGFAMINEMST hMiNeMst, uint32_t nBaudrate, uint8_t parity, uint16_t nModbusCtrlReg)
  124. {
  125. if(hMiNeMst)
  126. {
  127. LPGFA_MB_MST pMst = (LPGFA_MB_MST)malloc(sizeof(GFA_MB_MST));
  128. memset(pMst, 0, sizeof(GFA_MB_MST));
  129. pMst->hMiNeMst = hMiNeMst;
  130. pMst->hMiNeDev = GfaMininetMasterGetDeviceHandle(hMiNeMst);
  131. pMst->nBaudrate = nBaudrate;
  132. pMst->parity = parity;
  133. pMst->nModbusCtrlReg = nModbusCtrlReg;
  134. pMst->nVerbosity = 2;
  135. pMst->pDumpCtx = stdout;
  136. GfaMininetMasterGetStartClock(hMiNeMst, &pMst->tsStart);
  137. return (HGFAMBMST)pMst;
  138. }
  139. errno = EINVAL;
  140. return NULL;
  141. }
  142. /////////////////////////////////////////////////////////////////////////////
  143. void GfaMbMstClose(HGFAMBMST hMst)
  144. {
  145. if(hMst)
  146. {
  147. LPGFA_MB_MST pMst = (LPGFA_MB_MST)hMst;
  148. free(pMst);
  149. }
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. int GfaMbMstSetVerbosity(HGFAMBMST hMst, int nVerbosity)
  153. {
  154. if(hMst)
  155. {
  156. LPGFA_MB_MST pMst = (LPGFA_MB_MST)hMst;
  157. if(nVerbosity < 0)
  158. nVerbosity = 0;
  159. else if(nVerbosity > 4)
  160. nVerbosity = 4;
  161. pMst->nVerbosity = nVerbosity;
  162. return 0;
  163. }
  164. errno = EINVAL;
  165. return -1;
  166. }
  167. /////////////////////////////////////////////////////////////////////////////
  168. ssize_t GfaMbMstCreateADU(uint8_t slvID, uint8_t func, const void *pData, size_t nCbData, LPMODBUS_RTU_ADU pAdu)
  169. {
  170. if( pAdu &&
  171. MODBUS_IS_VALID_SLAVE_ID(slvID))
  172. {
  173. uint16_t nCRC16;
  174. size_t nCbAdu = 2;
  175. pAdu->slvID = slvID;
  176. pAdu->func = func;
  177. if(pData && nCbData)
  178. {
  179. if((nCbData > MODBUS_MAX_DATA_PAYLOAD))
  180. {
  181. errno = ENOMEM;
  182. return -1;
  183. }
  184. memcpy(pAdu->b, pData, nCbData);
  185. nCbAdu += nCbData;
  186. }
  187. else
  188. {
  189. nCbData = 0;
  190. }
  191. nCRC16 = _Crc16(pAdu, nCbAdu);
  192. _CpyUnalignedUint16(&pAdu->b[nCbData], &nCRC16, 1);
  193. nCbAdu += sizeof(nCRC16);
  194. return nCbAdu;
  195. }
  196. errno = EINVAL;
  197. return -1;
  198. }
  199. /////////////////////////////////////////////////////////////////////////////
  200. bool GfaMbMstValidateADU(uint8_t slvID, uint8_t func, LPCMODBUS_RTU_ADU pAdu, size_t nCbAdu, int *pnMbErr)
  201. {
  202. bool bRet;
  203. uint16_t nCRC16Received, nCRC16Expected;
  204. if(pnMbErr)
  205. *pnMbErr = 0;
  206. if(pAdu->slvID != slvID)
  207. {
  208. errno = EPROTO;
  209. return false;
  210. }
  211. else if(pAdu->func != func)
  212. {
  213. if((pAdu->func != (func | MB_FUNC_ERROR_FLAG)) || (nCbAdu != 5))
  214. {
  215. errno = EPROTO;
  216. return false;
  217. }
  218. else if(pnMbErr)
  219. *pnMbErr = pAdu->b[0];
  220. }
  221. _CpyUnalignedUint16(&nCRC16Received, ((uint8_t*)pAdu) + nCbAdu - sizeof(uint16_t), sizeof(uint16_t));
  222. nCRC16Expected = _Crc16(pAdu, nCbAdu - sizeof(uint16_t));
  223. if(!(bRet = (nCRC16Received == nCRC16Expected)))
  224. errno = EPROTO;
  225. return bRet;
  226. }
  227. /////////////////////////////////////////////////////////////////////////////
  228. void GfaMbMstWaitFrameDelay(HGFAMBMST hMst)
  229. {
  230. if(hMst)
  231. {
  232. uint32_t nDelay, nBaudrate;
  233. LPGFA_MB_MST pMst = (LPGFA_MB_MST)hMst;
  234. if( !GfaMininetDeviceGetBaudrate(pMst->hMiNeDev, &nBaudrate) &&
  235. nBaudrate <= 19200)
  236. nDelay = (350000000 / nBaudrate + 5) / 10;
  237. else
  238. nDelay = 1750;
  239. usleep(nDelay);
  240. }
  241. }
  242. /////////////////////////////////////////////////////////////////////////////
  243. ssize_t GfaMbMstSendADU(HGFAMBMST hMst, LPCMODBUS_RTU_ADU pAdu, size_t nCbAdu)
  244. {
  245. if(hMst && pAdu && nCbAdu >= 4)
  246. {
  247. LPGFA_MB_MST pMst = (LPGFA_MB_MST)hMst;
  248. GfaMbMstDumpADU(hMst, pAdu, nCbAdu, true, NULL);
  249. return GfaMininetDeviceTransmit(pMst->hMiNeDev, pAdu, nCbAdu);
  250. }
  251. errno = EINVAL;
  252. return -1;
  253. }
  254. /////////////////////////////////////////////////////////////////////////////
  255. ssize_t GfaMbMstRecvADU(HGFAMBMST hMst, LPMODBUS_RTU_ADU pAdu, size_t nCbAdu)
  256. {
  257. if(hMst && pAdu && nCbAdu >= 4)
  258. {
  259. ssize_t nRet;
  260. LPGFA_MB_MST pMst = (LPGFA_MB_MST)hMst;
  261. if((nRet = GfaMininetDeviceReceive(pMst->hMiNeDev, pAdu, nCbAdu)) > 0)
  262. GfaMbMstDumpADU(hMst, pAdu, nCbAdu, false, NULL);
  263. return nRet;
  264. }
  265. errno = EINVAL;
  266. return -1;
  267. }
  268. /////////////////////////////////////////////////////////////////////////////
  269. void GfaMbMstDumpADU(HGFAMBMST hMst, LPCMODBUS_RTU_ADU pAdu, size_t nCbAdu, bool bTX, const char *pszAnnotation)
  270. {
  271. if(hMst && pAdu && nCbAdu)
  272. {
  273. LPGFA_MB_MST pMst = (LPGFA_MB_MST)hMst;
  274. FILE *pf = pMst->pDumpCtx;
  275. if(pf && (pMst->nVerbosity >= 4))
  276. {
  277. size_t i, nCbData = nCbAdu - 4;
  278. uint16_t nCRC16;
  279. struct timespec tsCur, tsIntv;
  280. clock_gettime(CLOCK_MONOTONIC, &tsCur);
  281. uint64_t nInterval = TimespecDiff(&tsCur, &pMst->tsStart);
  282. Ns2Timespec(nInterval, &tsIntv);
  283. if(nCbAdu >= 4)
  284. {
  285. fprintf(pf, "\n///////////////////////////////////////\n");
  286. fprintf(pf, "// Modbus - %ld.%03ld\n", tsIntv.tv_sec, tsIntv.tv_nsec / 1000000);
  287. fprintf(pf, "// %s:\n", bTX ? "TX" : "RX");
  288. fprintf(pf, "SlvID: %02hhX (%hhu)\n", pAdu->slvID, pAdu->slvID);
  289. fprintf(pf, "Func: %02hhX (%hhu)\n", pAdu->func, pAdu->func);
  290. fprintf(pf, "Data: ");
  291. for(i = 0; i < nCbData; i++)
  292. {
  293. fprintf(pf, "[%02hhX]", (char)pAdu->b[i]);
  294. }
  295. _CpyUnalignedUint16(&nCRC16, &pAdu->b[i], 1);
  296. fprintf(pf, "\nCRC16: %02hX (%hu)\n", nCRC16, nCRC16);
  297. if(pszAnnotation)
  298. fprintf(pf, "Annot.: %s\n", pszAnnotation);
  299. }
  300. else
  301. {
  302. fprintf(pf, "\n///////////////////////////////////////\n");
  303. fprintf(pf, "// Modbus - %ld:%03ld\n", tsIntv.tv_sec, tsIntv.tv_nsec / 1000000);
  304. fprintf(pf, "// %s:\n", bTX ? "TX" : "RX");
  305. fprintf(pf, "Invalid Modbus-Frame!\n");
  306. }
  307. fprintf(pf, "\n");
  308. fflush(pf);
  309. }
  310. }
  311. }
  312. /////////////////////////////////////////////////////////////////////////////
  313. ssize_t GfaMbMstReadHoldingRegisters(HGFAMBMST hMst, uint8_t slvID, uint16_t nRegStart, uint16_t nRegCount, void *pRegs)
  314. {
  315. if(hMst && pRegs && nRegCount > 0)
  316. {
  317. int nMBErr;
  318. ssize_t nRet;
  319. uint8_t func = MB_FUNC_READ_HOLDING_REGISTERS;
  320. MODBUS_RTU_ADU adu;
  321. size_t nCbExpt = 5 + (nRegCount * sizeof(uint16_t));
  322. uint8_t ri[4];
  323. _CpyUnalignedUint16(&ri[0], &nRegStart, 1);
  324. _CpyUnalignedUint16(&ri[2], &nRegCount, 1);
  325. if((nRet = GfaMbMstCreateADU(slvID, func, &ri, sizeof(ri), &adu)) < 0)
  326. return nRet;
  327. if((nRet = GfaMbMstSendADU(hMst, &adu, nRet)) < 0)
  328. return nRet;
  329. if((nRet = GfaMbMstRecvADU(hMst, &adu, nCbExpt)) <= 0)
  330. return -1;
  331. if(!GfaMbMstValidateADU(slvID, func, &adu, nRet, &nMBErr))
  332. return -1;
  333. if(nMBErr)
  334. {
  335. errno = MAKE_GFA_MODBUS_ERROR(nMBErr);
  336. return -1;
  337. }
  338. else if(nRet != (int)(5 + nRegCount * sizeof(uint16_t)))
  339. {
  340. errno = EPROTO;
  341. return -1;
  342. }
  343. else if(adu.b[0] != (nRegCount * sizeof(uint16_t)))
  344. {
  345. errno = EPROTO;
  346. return -1;
  347. }
  348. _CpyUnalignedUint16(pRegs, &adu.b[1], nRegCount);
  349. return 0;
  350. }
  351. errno = EINVAL;
  352. return -1;
  353. }
  354. /////////////////////////////////////////////////////////////////////////////
  355. ssize_t GfaMbMstWriteMultipleRegisters(HGFAMBMST hMst, uint8_t slvID, uint16_t nRegStart, uint16_t nRegCount, const void *pRegs)
  356. {
  357. if(hMst && pRegs && nRegCount > 0)
  358. {
  359. int nMBErr;
  360. ssize_t nRet;
  361. MODBUS_RTU_ADU adu;
  362. uint8_t func = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
  363. uint8_t data[MODBUS_MAX_DATA_PAYLOAD];
  364. _CpyUnalignedUint16(&data[0], &nRegStart, 1);
  365. _CpyUnalignedUint16(&data[2], &nRegCount, 1);
  366. data[4] = (uint8_t)(nRegCount * sizeof(uint16_t));
  367. _CpyUnalignedUint16(&data[5], pRegs, nRegCount);
  368. if((nRet = GfaMbMstCreateADU(slvID, func, &data, 5 + nRegCount * sizeof(uint16_t), &adu)) < 0)
  369. return -1;
  370. if((nRet = GfaMbMstSendADU(hMst, &adu, nRet)) < 0)
  371. return -1;
  372. if((nRet = GfaMbMstRecvADU(hMst, &adu, 8)) <= 0)
  373. return -1;
  374. if(!GfaMbMstValidateADU(slvID, func, &adu, nRet, &nMBErr))
  375. return -1;
  376. if(nMBErr)
  377. {
  378. errno = MAKE_GFA_MODBUS_ERROR(nMBErr);
  379. return -1;
  380. }
  381. else if(nRet != 8)
  382. {
  383. errno = EPROTO;
  384. return -1;
  385. }
  386. else if((bswap_16(adu.w[0]) != nRegStart) || (bswap_16(adu.w[1]) != nRegCount))
  387. {
  388. errno = EPROTO;
  389. return -1;
  390. }
  391. return 0;
  392. }
  393. errno = EINVAL;
  394. return -1;
  395. }
  396. /////////////////////////////////////////////////////////////////////////////
  397. int GfaMbMstPing(HGFAMBMST hMst, uint8_t slvID)
  398. {
  399. if(hMst)
  400. {
  401. static uint8_t b = 0;
  402. int nMBErr;
  403. ssize_t nRet;
  404. MODBUS_RTU_ADU adu;
  405. uint8_t func = MB_FUNC_DIAGNOSTIC;
  406. uint16_t nEcho = ++b;
  407. nEcho |= (~b << 8);
  408. uint16_t data[2] = {bswap_16(MB_SUBFUNC_RETURN_QUERY_DATA), nEcho};
  409. if((nRet = GfaMbMstCreateADU(slvID, func, &data, sizeof(data), &adu)) < 0)
  410. return -1;
  411. if((nRet = GfaMbMstSendADU(hMst, &adu, nRet)) < 0)
  412. return -1;
  413. adu.w[0] = ~MB_SUBFUNC_RETURN_QUERY_DATA;
  414. adu.w[1] = ~adu.w[1];
  415. adu.w[2] = 0;
  416. if((nRet = GfaMbMstRecvADU(hMst, &adu, 8)) <= 0)
  417. return -1;
  418. if(!GfaMbMstValidateADU(slvID, func, &adu, nRet, &nMBErr))
  419. return -1;
  420. if(nMBErr)
  421. {
  422. errno = MAKE_GFA_MODBUS_ERROR(nMBErr);
  423. return -1;
  424. }
  425. else if(nRet != 8)
  426. {
  427. errno = EPROTO;
  428. return -1;
  429. }
  430. else if((adu.w[0] != data[0]) || (adu.w[1] != data[1]))
  431. {
  432. errno = EPROTO;
  433. return -1;
  434. }
  435. return 0;
  436. }
  437. errno = EINVAL;
  438. return -1;
  439. }
  440. /////////////////////////////////////////////////////////////////////////////
  441. int GfaMbMstBootloaderExecute(HGFAMBMST hMst, uint8_t slvID)
  442. {
  443. if(hMst)
  444. {
  445. int nRet, nErr;
  446. GFA_SER_CFG_PARAMS scp, scpSave;
  447. struct timeval tvRX = {0, 500000};
  448. uint16_t regs[2];
  449. LPGFA_MB_MST pMst = (LPGFA_MB_MST)hMst;
  450. if(GfaMininetDeviceGetConfigParams(pMst->hMiNeDev, &scp, sizeof(scp)) != sizeof(scp))
  451. return -1;
  452. memcpy(&scpSave, &scp, sizeof(scpSave));
  453. scp.parity = pMst->parity;
  454. scp.baud = pMst->nBaudrate;
  455. if(GfaMininetDeviceSetConfigParams(pMst->hMiNeDev, &scp, sizeof(scp)) != 0)
  456. return -1;
  457. GfaMininetMasterSaveTimeouts(pMst->hMiNeMst);
  458. GfaMininetMasterSetTimeouts(pMst->hMiNeMst, &tvRX, NULL);
  459. TRACE4("Reading modbus registers %hu and %hu.\n", pMst->nModbusCtrlReg, pMst->nModbusCtrlReg + 1);
  460. if((nRet = GfaMbMstReadHoldingRegisters(hMst, slvID, pMst->nModbusCtrlReg, 2, regs)) == 0)
  461. {
  462. GfaMbMstWaitFrameDelay(hMst);
  463. TRACE4("Writing modbus registers %hu and %hu - values: 0x%04hX and 0x%04hX\n", pMst->nModbusCtrlReg + 2, pMst->nModbusCtrlReg + 3, regs[0], regs[1]);
  464. if((nRet = GfaMbMstWriteMultipleRegisters(hMst, slvID, pMst->nModbusCtrlReg + 2, 2, regs)) == 0)
  465. usleep(GFA_BOOTLOADER_EXEC_WAIT_TIME * 1000);
  466. }
  467. nErr = errno;
  468. GfaMininetMasterRestoreTimeouts(pMst->hMiNeMst);
  469. if(GfaMininetDeviceSetConfigParams(pMst->hMiNeDev, &scpSave, sizeof(scpSave)) != 0)
  470. return -1;
  471. errno = nErr;
  472. return nRet;
  473. }
  474. errno = EINVAL;
  475. return -1;
  476. }