12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012 |
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #ifdef HAVE_INTTYPES_H
- #include <inttypes.h>
- #endif
- #ifdef HAVE_STDINT_H
- #include <stdint.h>
- #endif
- #include <termios.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <errno.h>
- #include <limits.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #if defined(__FreeBSD__ ) && __FreeBSD__ < 5
- #include <netinet/in_systm.h>
- #endif
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #include <arpa/inet.h>
- #if !defined(UINT16_MAX)
- #define UINT16_MAX 0xFFFF
- #endif
- #include <config.h>
- #include <modbus/modbus.h>
- #define UNKNOWN_ERROR_MSG "Not defined in modbus specification"
- const unsigned int mb_version_major = MB_VERSION_MAJOR;
- const unsigned int mb_version_minor = MB_VERSION_MINOR;
- const unsigned int mb_version_micro = MB_VERSION_MICRO;
- typedef struct {
- int slave;
- int function;
- int t_id;
- } sft_t;
- static const uint8_t NB_TAB_ERROR_MSG = 12;
- static const char *TAB_ERROR_MSG[] = {
- UNKNOWN_ERROR_MSG,
- "Illegal function code",
- "Illegal data address",
- "Illegal data value",
- "Slave device or server failure",
- "Acknowledge",
- "Slave device or server busy",
- "Negative acknowledge",
- "Memory parity error",
- UNKNOWN_ERROR_MSG,
- "Gateway path unavailable",
- "Target device failed to respond"
- };
- static uint8_t table_crc_hi[] = {
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
- };
- static uint8_t table_crc_lo[] = {
- 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
- 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
- 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
- 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
- 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
- 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
- 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
- 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
- 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
- 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
- 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
- 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
- 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
- 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
- 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
- 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
- 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
- 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
- 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
- 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
- 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
- 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
- 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
- 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
- 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
- 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
- };
- static const int TAB_HEADER_LENGTH[2] = {
- HEADER_LENGTH_RTU,
- HEADER_LENGTH_TCP
- };
- static const int TAB_CHECKSUM_LENGTH[2] = {
- CHECKSUM_LENGTH_RTU,
- CHECKSUM_LENGTH_TCP
- };
- static const int TAB_MAX_ADU_LENGTH[2] = {
- MAX_ADU_LENGTH_RTU,
- MAX_ADU_LENGTH_TCP,
- };
- static void error_treat(modbus_param_t *mb_param, int code, const char *string)
- {
- printf("\nERROR %s (%0X)\n", string, -code);
- if (mb_param->error_handling == FLUSH_OR_CONNECT_ON_ERROR) {
- switch (code) {
- case INVALID_DATA:
- case INVALID_CRC:
- case INVALID_EXCEPTION_CODE:
- modbus_flush(mb_param);
- break;
- case SELECT_FAILURE:
- case SOCKET_FAILURE:
- case CONNECTION_CLOSED:
- modbus_close(mb_param);
- modbus_connect(mb_param);
- break;
- default:
-
- break;
- }
- }
- }
- void modbus_flush(modbus_param_t *mb_param)
- {
- if (mb_param->type_com == RTU) {
- tcflush(mb_param->fd, TCIOFLUSH);
- } else {
- int ret;
- do {
-
- char devnull[MAX_ADU_LENGTH_TCP];
- #if (!HAVE_DECL___CYGWIN__)
- ret = recv(mb_param->fd, devnull, MAX_ADU_LENGTH_TCP, MSG_DONTWAIT);
- #else
-
- fd_set rfds;
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- FD_ZERO(&rfds);
- FD_SET(mb_param->fd, &rfds);
- ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv);
- if (ret > 0) {
- ret = recv(mb_param->fd, devnull, MAX_ADU_LENGTH_TCP, 0);
- } else if (ret == -1) {
-
- error_treat(mb_param, SELECT_FAILURE, "Select failure");
- }
- #endif
- if (mb_param->debug && ret > 0) {
- printf("%d bytes flushed\n", ret);
- }
- } while (ret > 0);
- }
- }
- static unsigned int compute_response_length(modbus_param_t *mb_param,
- uint8_t *query)
- {
- int length;
- int offset;
- offset = TAB_HEADER_LENGTH[mb_param->type_com];
- switch (query[offset]) {
- case FC_READ_COIL_STATUS:
- case FC_READ_INPUT_STATUS: {
-
- int nb = (query[offset + 3] << 8) | query[offset + 4];
- length = 2 + (nb / 8) + ((nb % 8) ? 1 : 0);
- }
- break;
- case FC_READ_HOLDING_REGISTERS:
- case FC_READ_INPUT_REGISTERS:
-
- length = 2 + 2 * (query[offset + 3] << 8 |
- query[offset + 4]);
- break;
- case FC_READ_EXCEPTION_STATUS:
- length = 3;
- break;
- case FC_REPORT_SLAVE_ID:
-
- return MSG_LENGTH_UNDEFINED;
- default:
- length = 5;
- }
- return length + offset + TAB_CHECKSUM_LENGTH[mb_param->type_com];
- }
- static int build_query_basis_rtu(int slave, int function,
- int start_addr, int nb,
- uint8_t *query)
- {
- query[0] = slave;
- query[1] = function;
- query[2] = start_addr >> 8;
- query[3] = start_addr & 0x00ff;
- query[4] = nb >> 8;
- query[5] = nb & 0x00ff;
- return PRESET_QUERY_LENGTH_RTU;
- }
- static int build_query_basis_tcp(int slave, int function,
- int start_addr, int nb,
- uint8_t *query)
- {
-
- static uint16_t t_id = 0;
-
- if (t_id < UINT16_MAX)
- t_id++;
- else
- t_id = 0;
- query[0] = t_id >> 8;
- query[1] = t_id & 0x00ff;
-
- query[2] = 0;
- query[3] = 0;
-
- query[6] = slave;
- query[7] = function;
- query[8] = start_addr >> 8;
- query[9] = start_addr & 0x00ff;
- query[10] = nb >> 8;
- query[11] = nb & 0x00ff;
- return PRESET_QUERY_LENGTH_TCP;
- }
- static int build_query_basis(modbus_param_t *mb_param,
- int function, int start_addr,
- int nb, uint8_t *query)
- {
- if (mb_param->type_com == RTU)
- return build_query_basis_rtu(mb_param->slave, function,
- start_addr, nb, query);
- else
- return build_query_basis_tcp(mb_param->slave, function,
- start_addr, nb, query);
- }
- static int build_response_basis_rtu(sft_t *sft, uint8_t *response)
- {
- response[0] = sft->slave;
- response[1] = sft->function;
- return PRESET_RESPONSE_LENGTH_RTU;
- }
- static int build_response_basis_tcp(sft_t *sft, uint8_t *response)
- {
-
- response[0] = sft->t_id >> 8;
- response[1] = sft->t_id & 0x00ff;
-
- response[2] = 0;
- response[3] = 0;
-
- response[6] = sft->slave;
- response[7] = sft->function;
- return PRESET_RESPONSE_LENGTH_TCP;
- }
- static int build_response_basis(modbus_param_t *mb_param, sft_t *sft,
- uint8_t *response)
- {
- if (mb_param->type_com == RTU)
- return build_response_basis_rtu(sft, response);
- else
- return build_response_basis_tcp(sft, response);
- }
- void set_message_length_tcp(uint8_t *msg, int msg_length)
- {
-
- int mbap_length = msg_length - 6;
- msg[4] = mbap_length >> 8;
- msg[5] = mbap_length & 0x00FF;
- }
- static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length)
- {
- uint8_t crc_hi = 0xFF;
- uint8_t crc_lo = 0xFF;
- unsigned int i;
-
- while (buffer_length--) {
- i = crc_hi ^ *buffer++;
- crc_hi = crc_lo ^ table_crc_hi[i];
- crc_lo = table_crc_lo[i];
- }
- return (crc_hi << 8 | crc_lo);
- }
- static int check_crc16(modbus_param_t *mb_param,
- uint8_t *msg,
- const int msg_length)
- {
- int ret;
- uint16_t crc_calc;
- uint16_t crc_received;
- crc_calc = crc16(msg, msg_length - 2);
- crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1];
-
- if (crc_calc == crc_received) {
- ret = msg_length;
- } else {
- char s_error[64];
- sprintf(s_error,
- "invalid crc received %0X - crc_calc %0X",
- crc_received, crc_calc);
- ret = INVALID_CRC;
- error_treat(mb_param, ret, s_error);
- }
- return ret;
- }
- static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
- int query_length)
- {
- int ret;
- uint16_t s_crc;
- int i;
- if (mb_param->type_com == RTU) {
- s_crc = crc16(query, query_length);
- query[query_length++] = s_crc >> 8;
- query[query_length++] = s_crc & 0x00FF;
- } else {
- set_message_length_tcp(query, query_length);
- }
- if (mb_param->debug) {
- for (i = 0; i < query_length; i++)
- printf("[%.2X]", query[i]);
- printf("\n");
- }
- if (mb_param->type_com == RTU)
- ret = write(mb_param->fd, query, query_length);
- else
- ret = send(mb_param->fd, query, query_length, MSG_NOSIGNAL);
-
- if ((ret == -1) || (ret != query_length)) {
- ret = SOCKET_FAILURE;
- error_treat(mb_param, ret, "Write socket failure");
- }
- return ret;
- }
- static uint8_t compute_query_length_header(int function)
- {
- int length;
- if (function <= FC_FORCE_SINGLE_COIL ||
- function == FC_PRESET_SINGLE_REGISTER)
-
- length = 4;
- else if (function == FC_FORCE_MULTIPLE_COILS ||
- function == FC_PRESET_MULTIPLE_REGISTERS)
-
- length = 5;
- else if (function == FC_REPORT_SLAVE_ID)
- length = 1;
- else
- length = 0;
- return length;
- }
- static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg)
- {
- int function = msg[TAB_HEADER_LENGTH[mb_param->type_com]];
- int length;
- if (function == FC_FORCE_MULTIPLE_COILS ||
- function == FC_PRESET_MULTIPLE_REGISTERS)
- length = msg[TAB_HEADER_LENGTH[mb_param->type_com] + 5];
- else if (function == FC_REPORT_SLAVE_ID)
- length = msg[TAB_HEADER_LENGTH[mb_param->type_com] + 1];
- else
- length = 0;
- length += TAB_CHECKSUM_LENGTH[mb_param->type_com];
- return length;
- }
- #define WAIT_DATA() \
- { \
- while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \
- if (errno == EINTR) { \
- printf("A non blocked signal was caught\n"); \
- \
- FD_ZERO(&rfds); \
- FD_SET(mb_param->fd, &rfds); \
- } else { \
- error_treat(mb_param, SELECT_FAILURE, "Select failure"); \
- return SELECT_FAILURE; \
- } \
- } \
- \
- if (select_ret == 0) { \
- \
- if (msg_length == (TAB_HEADER_LENGTH[mb_param->type_com] + 2 + \
- TAB_CHECKSUM_LENGTH[mb_param->type_com])) { \
-
- \
- return MB_EXCEPTION; \
- } else { \
- \
- return SELECT_TIMEOUT; \
- } \
- } \
- }
- static int receive_msg(modbus_param_t *mb_param,
- int msg_length_computed,
- uint8_t *msg)
- {
- int select_ret;
- int read_ret;
- fd_set rfds;
- struct timeval tv;
- int length_to_read;
- uint8_t *p_msg;
- enum { FUNCTION, BYTE, COMPLETE };
- int state;
- int msg_length = 0;
- if (mb_param->debug) {
- if (msg_length_computed == MSG_LENGTH_UNDEFINED)
- printf("Waiting for a message...\n");
- else
- printf("Waiting for a message (%d bytes)...\n",
- msg_length_computed);
- }
-
- FD_ZERO(&rfds);
- FD_SET(mb_param->fd, &rfds);
- if (msg_length_computed == MSG_LENGTH_UNDEFINED) {
-
- tv.tv_sec = 60;
- tv.tv_usec = 0;
-
- state = FUNCTION;
- msg_length_computed = TAB_HEADER_LENGTH[mb_param->type_com] + 1;
- } else {
- tv.tv_sec = 0;
- tv.tv_usec = TIME_OUT_BEGIN_OF_TRAME;
- state = COMPLETE;
- }
- length_to_read = msg_length_computed;
- select_ret = 0;
- WAIT_DATA();
- p_msg = msg;
- while (select_ret) {
- if (mb_param->type_com == RTU)
- read_ret = read(mb_param->fd, p_msg, length_to_read);
- else
- read_ret = recv(mb_param->fd, p_msg, length_to_read, 0);
- if (read_ret == 0) {
- return CONNECTION_CLOSED;
- } else if (read_ret < 0) {
-
- error_treat(mb_param, SOCKET_FAILURE,
- "Read socket failure");
- return SOCKET_FAILURE;
- }
-
- msg_length += read_ret;
-
- if (mb_param->debug) {
- int i;
- for (i=0; i < read_ret; i++)
- printf("<%.2X>", p_msg[i]);
- }
- if (msg_length < msg_length_computed) {
-
- length_to_read = msg_length_computed - msg_length;
- } else {
- switch (state) {
- case FUNCTION:
-
- length_to_read = compute_query_length_header(
- msg[TAB_HEADER_LENGTH[mb_param->type_com]]);
- msg_length_computed += length_to_read;
-
- state = BYTE;
- break;
- case BYTE:
- length_to_read = compute_query_length_data(mb_param, msg);
- msg_length_computed += length_to_read;
- if (msg_length_computed > TAB_MAX_ADU_LENGTH[mb_param->type_com]) {
- error_treat(mb_param, INVALID_DATA, "Too many data");
- return INVALID_DATA;
- }
- state = COMPLETE;
- break;
- case COMPLETE:
- length_to_read = 0;
- break;
- }
- }
-
- p_msg = &(p_msg[read_ret]);
- if (length_to_read > 0) {
-
- tv.tv_sec = 0;
- tv.tv_usec = TIME_OUT_END_OF_TRAME;
- WAIT_DATA();
- } else {
-
- select_ret = FALSE;
- }
- }
- if (mb_param->debug)
- printf("\n");
- if (mb_param->type_com == RTU) {
-
- return check_crc16(mb_param, msg, msg_length);
- } else {
-
- return msg_length;
- }
- }
- int modbus_slave_receive(modbus_param_t *mb_param, int sockfd, uint8_t *query)
- {
- if (sockfd != -1) {
- mb_param->fd = sockfd;
- }
-
- return receive_msg(mb_param, MSG_LENGTH_UNDEFINED, query);
- }
- static int modbus_receive(modbus_param_t *mb_param,
- uint8_t *query,
- uint8_t *response)
- {
- int ret;
- int response_length_computed;
- int offset = TAB_HEADER_LENGTH[mb_param->type_com];
- response_length_computed = compute_response_length(mb_param, query);
- ret = receive_msg(mb_param, response_length_computed, response);
- if (ret >= 0) {
-
- int query_nb_value;
- int response_nb_value;
-
- switch (response[offset]) {
- case FC_READ_COIL_STATUS:
- case FC_READ_INPUT_STATUS:
-
- query_nb_value = (query[offset + 3] << 8) + query[offset + 4];
- query_nb_value = (query_nb_value / 8) + ((query_nb_value % 8) ? 1 : 0);
- response_nb_value = response[offset + 1];
- break;
- case FC_READ_HOLDING_REGISTERS:
- case FC_READ_INPUT_REGISTERS:
-
- query_nb_value = (query[offset + 3] << 8) + query[offset + 4];
- response_nb_value = (response[offset + 1] / 2);
- break;
- case FC_FORCE_MULTIPLE_COILS:
- case FC_PRESET_MULTIPLE_REGISTERS:
-
- query_nb_value = (query[offset + 3] << 8) + query[offset + 4];
- response_nb_value = (response[offset + 3] << 8) | response[offset + 4];
- break;
- case FC_REPORT_SLAVE_ID:
-
- query_nb_value = response_nb_value = ret;
- break;
- default:
-
- query_nb_value = response_nb_value = 1;
- }
- if (query_nb_value == response_nb_value) {
- ret = response_nb_value;
- } else {
- char *s_error = malloc(64 * sizeof(char));
- sprintf(s_error, "Quantity not corresponding to the query (%d != %d)",
- response_nb_value, query_nb_value);
- ret = INVALID_DATA;
- error_treat(mb_param, ret, s_error);
- free(s_error);
- }
- } else if (ret == MB_EXCEPTION) {
-
-
- if (mb_param->type_com == RTU) {
- ret = check_crc16(mb_param, response, EXCEPTION_RESPONSE_LENGTH_RTU);
- if (ret < 0)
- return ret;
- }
-
- if (0x80 + query[offset] == response[offset]) {
- int exception_code = response[offset + 1];
-
- if (exception_code < NB_TAB_ERROR_MSG) {
- error_treat(mb_param, -exception_code,
- TAB_ERROR_MSG[response[offset + 1]]);
-
-
- return -exception_code;
- } else {
-
- char *s_error = malloc(64 * sizeof(char));
- sprintf(s_error,
- "Invalid exception code %d",
- response[offset + 1]);
- error_treat(mb_param, INVALID_EXCEPTION_CODE,
- s_error);
- free(s_error);
- return INVALID_EXCEPTION_CODE;
- }
- }
- } else {
-
- error_treat(mb_param, ret, "receive_msg");
- }
- return ret;
- }
- static int response_io_status(int address, int nb,
- uint8_t *tab_io_status,
- uint8_t *response, int offset)
- {
- int shift = 0;
- int byte = 0;
- int i;
- for (i = address; i < address+nb; i++) {
- byte |= tab_io_status[i] << shift;
- if (shift == 7) {
-
- response[offset++] = byte;
- byte = shift = 0;
- } else {
- shift++;
- }
- }
- if (shift != 0)
- response[offset++] = byte;
- return offset;
- }
- static int response_exception(modbus_param_t *mb_param, sft_t *sft,
- int exception_code, uint8_t *response)
- {
- int response_length;
- sft->function = sft->function + 0x80;
- response_length = build_response_basis(mb_param, sft, response);
-
- response[response_length++] = -exception_code;
- return response_length;
- }
- void modbus_slave_manage(modbus_param_t *mb_param, const uint8_t *query,
- int query_length, modbus_mapping_t *mb_mapping)
- {
- int offset = TAB_HEADER_LENGTH[mb_param->type_com];
- int slave = query[offset - 1];
- int function = query[offset];
- uint16_t address = (query[offset + 1] << 8) + query[offset + 2];
- uint8_t response[MAX_MESSAGE_LENGTH];
- int resp_length = 0;
- sft_t sft;
- if (slave != mb_param->slave && slave != MODBUS_BROADCAST_ADDRESS) {
-
- if (mb_param->debug) {
- printf("Request for slave %d ignored (not %d)\n",
- slave, mb_param->slave);
- }
- return;
- }
- sft.slave = slave;
- sft.function = function;
- if (mb_param->type_com == TCP) {
- sft.t_id = (query[0] << 8) + query[1];
- } else {
- sft.t_id = 0;
- query_length -= CHECKSUM_LENGTH_RTU;
- }
- switch (function) {
- case FC_READ_COIL_STATUS: {
- int nb = (query[offset + 3] << 8) + query[offset + 4];
- if ((address + nb) > mb_mapping->nb_coil_status) {
- printf("Illegal data address %0X in read_coil_status\n",
- address + nb);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
- resp_length = build_response_basis(mb_param, &sft, response);
- response[resp_length++] = (nb / 8) + ((nb % 8) ? 1 : 0);
- resp_length = response_io_status(address, nb,
- mb_mapping->tab_coil_status,
- response, resp_length);
- }
- }
- break;
- case FC_READ_INPUT_STATUS: {
-
- int nb = (query[offset + 3] << 8) + query[offset + 4];
- if ((address + nb) > mb_mapping->nb_input_status) {
- printf("Illegal data address %0X in read_input_status\n",
- address + nb);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
- resp_length = build_response_basis(mb_param, &sft, response);
- response[resp_length++] = (nb / 8) + ((nb % 8) ? 1 : 0);
- resp_length = response_io_status(address, nb,
- mb_mapping->tab_input_status,
- response, resp_length);
- }
- }
- break;
- case FC_READ_HOLDING_REGISTERS: {
- int nb = (query[offset + 3] << 8) + query[offset + 4];
- if ((address + nb) > mb_mapping->nb_holding_registers) {
- printf("Illegal data address %0X in read_holding_registers\n",
- address + nb);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
- int i;
- resp_length = build_response_basis(mb_param, &sft, response);
- response[resp_length++] = nb << 1;
- for (i = address; i < address + nb; i++) {
- response[resp_length++] = mb_mapping->tab_holding_registers[i] >> 8;
- response[resp_length++] = mb_mapping->tab_holding_registers[i] & 0xFF;
- }
- }
- }
- break;
- case FC_READ_INPUT_REGISTERS: {
-
- int nb = (query[offset + 3] << 8) + query[offset + 4];
- if ((address + nb) > mb_mapping->nb_input_registers) {
- printf("Illegal data address %0X in read_input_registers\n",
- address + nb);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
- int i;
- resp_length = build_response_basis(mb_param, &sft, response);
- response[resp_length++] = nb << 1;
- for (i = address; i < address + nb; i++) {
- response[resp_length++] = mb_mapping->tab_input_registers[i] >> 8;
- response[resp_length++] = mb_mapping->tab_input_registers[i] & 0xFF;
- }
- }
- }
- break;
- case FC_FORCE_SINGLE_COIL:
- if (address >= mb_mapping->nb_coil_status) {
- printf("Illegal data address %0X in force_singe_coil\n", address);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
- int data = (query[offset + 3] << 8) + query[offset + 4];
- if (data == 0xFF00 || data == 0x0) {
- mb_mapping->tab_coil_status[address] = (data) ? ON : OFF;
-
- memcpy(response, query, query_length);
- resp_length = query_length;
- } else {
- printf("Illegal data value %0X in force_single_coil request at address %0X\n",
- data, address);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_VALUE, response);
- }
- }
- break;
- case FC_PRESET_SINGLE_REGISTER:
- if (address >= mb_mapping->nb_holding_registers) {
- printf("Illegal data address %0X in preset_holding_register\n", address);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
- int data = (query[offset + 3] << 8) + query[offset + 4];
- mb_mapping->tab_holding_registers[address] = data;
- memcpy(response, query, query_length);
- resp_length = query_length;
- }
- break;
- case FC_FORCE_MULTIPLE_COILS: {
- int nb = (query[offset + 3] << 8) + query[offset + 4];
- if ((address + nb) > mb_mapping->nb_coil_status) {
- printf("Illegal data address %0X in force_multiple_coils\n",
- address + nb);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
-
- set_bits_from_bytes(mb_mapping->tab_coil_status, address, nb, &query[offset + 6]);
- resp_length = build_response_basis(mb_param, &sft, response);
-
- memcpy(response + resp_length, query + resp_length, 4);
- resp_length += 4;
- }
- }
- break;
- case FC_PRESET_MULTIPLE_REGISTERS: {
- int nb = (query[offset + 3] << 8) + query[offset + 4];
- if ((address + nb) > mb_mapping->nb_holding_registers) {
- printf("Illegal data address %0X in preset_multiple_registers\n",
- address + nb);
- resp_length = response_exception(mb_param, &sft,
- ILLEGAL_DATA_ADDRESS, response);
- } else {
- int i, j;
- for (i = address, j = 6; i < address + nb; i++, j += 2) {
-
- mb_mapping->tab_holding_registers[i] =
- (query[offset + j] << 8) + query[offset + j + 1];
- }
- resp_length = build_response_basis(mb_param, &sft, response);
-
- memcpy(response + resp_length, query + resp_length, 4);
- resp_length += 4;
- }
- }
- break;
- case FC_READ_EXCEPTION_STATUS:
- case FC_REPORT_SLAVE_ID:
- printf("Not implemented\n");
- break;
- }
- modbus_send(mb_param, response, resp_length);
- }
- static int read_io_status(modbus_param_t *mb_param, int function,
- int start_addr, int nb, uint8_t *data_dest)
- {
- int ret;
- int query_length;
- uint8_t query[MIN_QUERY_LENGTH];
- uint8_t response[MAX_MESSAGE_LENGTH];
- query_length = build_query_basis(mb_param, function,
- start_addr, nb, query);
- ret = modbus_send(mb_param, query, query_length);
- if (ret > 0) {
- int i, temp, bit;
- int pos = 0;
- int offset;
- int offset_end;
- ret = modbus_receive(mb_param, query, response);
- if (ret < 0)
- return ret;
- offset = TAB_HEADER_LENGTH[mb_param->type_com];
- offset_end = offset + ret;
- for (i = offset; i < offset_end; i++) {
-
- temp = response[i + 2];
- for (bit = 0x01; (bit & 0xff) && (pos < nb);) {
- data_dest[pos++] = (temp & bit) ? TRUE : FALSE;
- bit = bit << 1;
- }
- }
- }
- return ret;
- }
- int read_coil_status(modbus_param_t *mb_param, int start_addr,
- int nb, uint8_t *data_dest)
- {
- int status;
- if (nb > MAX_STATUS) {
- printf("ERROR Too many coils status requested (%d > %d)\n",
- nb, MAX_STATUS);
- return INVALID_DATA;
- }
- status = read_io_status(mb_param, FC_READ_COIL_STATUS,
- start_addr, nb, data_dest);
- if (status > 0)
- status = nb;
- return status;
- }
- int read_input_status(modbus_param_t *mb_param, int start_addr,
- int nb, uint8_t *data_dest)
- {
- int status;
- if (nb > MAX_STATUS) {
- printf("ERROR Too many input status requested (%d > %d)\n",
- nb, MAX_STATUS);
- return INVALID_DATA;
- }
- status = read_io_status(mb_param, FC_READ_INPUT_STATUS,
- start_addr, nb, data_dest);
- if (status > 0)
- status = nb;
- return status;
- }
- static int read_registers(modbus_param_t *mb_param, int function,
- int start_addr, int nb, uint16_t *data_dest)
- {
- int ret;
- int query_length;
- uint8_t query[MIN_QUERY_LENGTH];
- uint8_t response[MAX_MESSAGE_LENGTH];
- if (nb > MAX_REGISTERS) {
- printf("ERROR Too many holding registers requested (%d > %d)\n",
- nb, MAX_REGISTERS);
- return INVALID_DATA;
- }
- query_length = build_query_basis(mb_param, function,
- start_addr, nb, query);
- ret = modbus_send(mb_param, query, query_length);
- if (ret > 0) {
- int offset;
- int i;
- ret = modbus_receive(mb_param, query, response);
- offset = TAB_HEADER_LENGTH[mb_param->type_com];
-
- for (i = 0; i < ret; i++) {
-
- data_dest[i] = (response[offset + 2 + (i << 1)] << 8) |
- response[offset + 3 + (i << 1)];
- }
- }
- return ret;
- }
- int read_holding_registers(modbus_param_t *mb_param,
- int start_addr, int nb, uint16_t *data_dest)
- {
- int status;
- if (nb > MAX_REGISTERS) {
- printf("ERROR Too many holding registers requested (%d > %d)\n",
- nb, MAX_REGISTERS);
- return INVALID_DATA;
- }
- status = read_registers(mb_param, FC_READ_HOLDING_REGISTERS,
- start_addr, nb, data_dest);
- return status;
- }
- int read_input_registers(modbus_param_t *mb_param, int start_addr, int nb,
- uint16_t *data_dest)
- {
- int status;
- if (nb > MAX_REGISTERS) {
- printf("ERROR Too many input registers requested (%d > %d)\n",
- nb, MAX_REGISTERS);
- return INVALID_DATA;
- }
- status = read_registers(mb_param, FC_READ_INPUT_REGISTERS,
- start_addr, nb, data_dest);
- return status;
- }
- static int set_single(modbus_param_t *mb_param, int function,
- int addr, int value)
- {
- int ret;
- int query_length;
- uint8_t query[MIN_QUERY_LENGTH];
- query_length = build_query_basis(mb_param, function,
- addr, value, query);
- ret = modbus_send(mb_param, query, query_length);
- if (ret > 0) {
-
- uint8_t response[MIN_QUERY_LENGTH];
- ret = modbus_receive(mb_param, query, response);
- }
- return ret;
- }
- int force_single_coil(modbus_param_t *mb_param, int coil_addr, int state)
- {
- int status;
- if (state)
- state = 0xFF00;
- status = set_single(mb_param, FC_FORCE_SINGLE_COIL,
- coil_addr, state);
- return status;
- }
- int preset_single_register(modbus_param_t *mb_param, int reg_addr, int value)
- {
- int status;
- status = set_single(mb_param, FC_PRESET_SINGLE_REGISTER,
- reg_addr, value);
- return status;
- }
- int force_multiple_coils(modbus_param_t *mb_param, int start_addr, int nb,
- const uint8_t *data_src)
- {
- int ret;
- int i;
- int byte_count;
- int query_length;
- int coil_check = 0;
- int pos = 0;
- uint8_t query[MAX_MESSAGE_LENGTH];
- if (nb > MAX_STATUS) {
- printf("ERROR Writing to too many coils (%d > %d)\n",
- nb, MAX_STATUS);
- return INVALID_DATA;
- }
- query_length = build_query_basis(mb_param, FC_FORCE_MULTIPLE_COILS,
- start_addr, nb, query);
- byte_count = (nb / 8) + ((nb % 8) ? 1 : 0);
- query[query_length++] = byte_count;
- for (i = 0; i < byte_count; i++) {
- int bit;
- bit = 0x01;
- query[query_length] = 0;
- while ((bit & 0xFF) && (coil_check++ < nb)) {
- if (data_src[pos++])
- query[query_length] |= bit;
- else
- query[query_length] &=~ bit;
- bit = bit << 1;
- }
- query_length++;
- }
- ret = modbus_send(mb_param, query, query_length);
- if (ret > 0) {
- uint8_t response[MAX_MESSAGE_LENGTH];
- ret = modbus_receive(mb_param, query, response);
- }
- return ret;
- }
- int preset_multiple_registers(modbus_param_t *mb_param, int start_addr, int nb,
- const uint16_t *data_src)
- {
- int ret;
- int i;
- int query_length;
- int byte_count;
- uint8_t query[MAX_MESSAGE_LENGTH];
- if (nb > MAX_REGISTERS) {
- printf("ERROR Trying to write to too many registers (%d > %d)\n",
- nb, MAX_REGISTERS);
- return INVALID_DATA;
- }
- query_length = build_query_basis(mb_param, FC_PRESET_MULTIPLE_REGISTERS,
- start_addr, nb, query);
- byte_count = nb * 2;
- query[query_length++] = byte_count;
- for (i = 0; i < nb; i++) {
- query[query_length++] = data_src[i] >> 8;
- query[query_length++] = data_src[i] & 0x00FF;
- }
- ret = modbus_send(mb_param, query, query_length);
- if (ret > 0) {
- uint8_t response[MAX_MESSAGE_LENGTH];
- ret = modbus_receive(mb_param, query, response);
- }
- return ret;
- }
- int report_slave_id(modbus_param_t *mb_param, uint8_t *data_dest)
- {
- int ret;
- int query_length;
- uint8_t query[MIN_QUERY_LENGTH];
- query_length = build_query_basis(mb_param, FC_REPORT_SLAVE_ID, 0, 0, query);
-
- query_length -= 4;
- ret = modbus_send(mb_param, query, query_length);
- if (ret > 0) {
- int i;
- int offset;
- int offset_end;
- uint8_t response[MAX_MESSAGE_LENGTH];
-
- ret = modbus_receive(mb_param, query, response);
- if (ret < 0)
- return ret;
- offset = TAB_HEADER_LENGTH[mb_param->type_com] - 1;
- offset_end = offset + ret;
- for (i = offset; i < offset_end; i++)
- data_dest[i] = response[i];
- }
- return ret;
- }
- void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
- int baud, const char *parity, int data_bit,
- int stop_bit, int slave)
- {
- memset(mb_param, 0, sizeof(modbus_param_t));
- strcpy(mb_param->device, device);
- mb_param->baud = baud;
- strcpy(mb_param->parity, parity);
- mb_param->debug = FALSE;
- mb_param->data_bit = data_bit;
- mb_param->stop_bit = stop_bit;
- mb_param->type_com = RTU;
- mb_param->error_handling = FLUSH_OR_CONNECT_ON_ERROR;
- mb_param->slave = slave;
- }
- void modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port, int slave)
- {
- memset(mb_param, 0, sizeof(modbus_param_t));
- strncpy(mb_param->ip, ip, sizeof(char)*16);
- mb_param->port = port;
- mb_param->type_com = TCP;
- mb_param->error_handling = FLUSH_OR_CONNECT_ON_ERROR;
- mb_param->slave = slave;
- }
- void modbus_set_slave(modbus_param_t *mb_param, int slave)
- {
- mb_param->slave = slave;
- }
- void modbus_set_error_handling(modbus_param_t *mb_param,
- error_handling_t error_handling)
- {
- if (error_handling == FLUSH_OR_CONNECT_ON_ERROR ||
- error_handling == NOP_ON_ERROR) {
- mb_param->error_handling = error_handling;
- } else {
- printf("Invalid setting for error handling (not changed)\n");
- }
- }
- static int modbus_connect_rtu(modbus_param_t *mb_param)
- {
- struct termios tios;
- speed_t speed;
- if (mb_param->debug) {
- printf("Opening %s at %d bauds (%s)\n",
- mb_param->device, mb_param->baud, mb_param->parity);
- }
-
- mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
- if (mb_param->fd < 0) {
- perror("open");
- printf("ERROR Can't open the device %s (%s)\n",
- mb_param->device, strerror(errno));
- return -1;
- }
-
- tcgetattr(mb_param->fd, &(mb_param->old_tios));
- memset(&tios, 0, sizeof(struct termios));
-
- switch (mb_param->baud) {
- case 110:
- speed = B110;
- break;
- case 300:
- speed = B300;
- break;
- case 600:
- speed = B600;
- break;
- case 1200:
- speed = B1200;
- break;
- case 2400:
- speed = B2400;
- break;
- case 4800:
- speed = B4800;
- break;
- case 9600:
- speed = B9600;
- break;
- case 19200:
- speed = B19200;
- break;
- case 38400:
- speed = B38400;
- break;
- case 57600:
- speed = B57600;
- break;
- case 115200:
- speed = B115200;
- break;
- default:
- speed = B9600;
- printf("WARNING Unknown baud rate %d for %s (B9600 used)\n",
- mb_param->baud, mb_param->device);
- }
-
- if ((cfsetispeed(&tios, speed) < 0) ||
- (cfsetospeed(&tios, speed) < 0)) {
- perror("cfsetispeed/cfsetospeed\n");
- return -1;
- }
-
- tios.c_cflag |= (CREAD | CLOCAL);
-
-
- tios.c_cflag &= ~CSIZE;
- switch (mb_param->data_bit) {
- case 5:
- tios.c_cflag |= CS5;
- break;
- case 6:
- tios.c_cflag |= CS6;
- break;
- case 7:
- tios.c_cflag |= CS7;
- break;
- case 8:
- default:
- tios.c_cflag |= CS8;
- break;
- }
-
- if (mb_param->stop_bit == 1)
- tios.c_cflag &=~ CSTOPB;
- else
- tios.c_cflag |= CSTOPB;
-
- if (strncmp(mb_param->parity, "none", 4) == 0) {
- tios.c_cflag &=~ PARENB;
- } else if (strncmp(mb_param->parity, "even", 4) == 0) {
- tios.c_cflag |= PARENB;
- tios.c_cflag &=~ PARODD;
- } else {
-
- tios.c_cflag |= PARENB;
- tios.c_cflag |= PARODD;
- }
-
-
-
-
- tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
-
- if (strncmp(mb_param->parity, "none", 4) == 0) {
- tios.c_iflag &= ~INPCK;
- } else {
- tios.c_iflag |= INPCK;
- }
-
- tios.c_iflag &= ~(IXON | IXOFF | IXANY);
-
-
- tios.c_oflag &=~ OPOST;
-
-
- tios.c_cc[VMIN] = 0;
- tios.c_cc[VTIME] = 0;
- if (tcsetattr(mb_param->fd, TCSANOW, &tios) < 0) {
- perror("tcsetattr\n");
- return -1;
- }
- return 0;
- }
- static int modbus_connect_tcp(modbus_param_t *mb_param)
- {
- int ret;
- int option;
- struct sockaddr_in addr;
- mb_param->fd = socket(PF_INET, SOCK_STREAM, 0);
- if (mb_param->fd < 0) {
- return mb_param->fd;
- }
-
-
- option = 1;
- ret = setsockopt(mb_param->fd, IPPROTO_TCP, TCP_NODELAY,
- (const void *)&option, sizeof(int));
- if (ret < 0) {
- perror("setsockopt TCP_NODELAY");
- close(mb_param->fd);
- return ret;
- }
- #if (!HAVE_DECL___CYGWIN__)
-
-
- option = IPTOS_LOWDELAY;
- ret = setsockopt(mb_param->fd, IPPROTO_IP, IP_TOS,
- (const void *)&option, sizeof(int));
- if (ret < 0) {
- perror("setsockopt IP_TOS");
- close(mb_param->fd);
- return ret;
- }
- #endif
- if (mb_param->debug) {
- printf("Connecting to %s\n", mb_param->ip);
- }
- addr.sin_family = AF_INET;
- addr.sin_port = htons(mb_param->port);
- addr.sin_addr.s_addr = inet_addr(mb_param->ip);
- ret = connect(mb_param->fd, (struct sockaddr *)&addr,
- sizeof(struct sockaddr_in));
- if (ret < 0) {
- perror("connect");
- close(mb_param->fd);
- return ret;
- }
- return 0;
- }
- int modbus_connect(modbus_param_t *mb_param)
- {
- int ret;
- if (mb_param->type_com == RTU)
- ret = modbus_connect_rtu(mb_param);
- else
- ret = modbus_connect_tcp(mb_param);
- return ret;
- }
- static void modbus_close_rtu(modbus_param_t *mb_param)
- {
- if (tcsetattr(mb_param->fd, TCSANOW, &(mb_param->old_tios)) < 0)
- perror("tcsetattr");
- close(mb_param->fd);
- }
- static void modbus_close_tcp(modbus_param_t *mb_param)
- {
- shutdown(mb_param->fd, SHUT_RDWR);
- close(mb_param->fd);
- }
- void modbus_close(modbus_param_t *mb_param)
- {
- if (mb_param->type_com == RTU)
- modbus_close_rtu(mb_param);
- else
- modbus_close_tcp(mb_param);
- }
- void modbus_set_debug(modbus_param_t *mb_param, int boolean)
- {
- mb_param->debug = boolean;
- }
- int modbus_mapping_new(modbus_mapping_t *mb_mapping,
- int nb_coil_status, int nb_input_status,
- int nb_holding_registers, int nb_input_registers)
- {
-
- mb_mapping->nb_coil_status = nb_coil_status;
- mb_mapping->tab_coil_status =
- (uint8_t *) malloc(nb_coil_status * sizeof(uint8_t));
- memset(mb_mapping->tab_coil_status, 0,
- nb_coil_status * sizeof(uint8_t));
- if (mb_mapping->tab_coil_status == NULL)
- return -1;
-
- mb_mapping->nb_input_status = nb_input_status;
- mb_mapping->tab_input_status =
- (uint8_t *) malloc(nb_input_status * sizeof(uint8_t));
- memset(mb_mapping->tab_input_status, 0,
- nb_input_status * sizeof(uint8_t));
- if (mb_mapping->tab_input_status == NULL) {
- free(mb_mapping->tab_coil_status);
- return -1;
- }
-
- mb_mapping->nb_holding_registers = nb_holding_registers;
- mb_mapping->tab_holding_registers =
- (uint16_t *) malloc(nb_holding_registers * sizeof(uint16_t));
- memset(mb_mapping->tab_holding_registers, 0,
- nb_holding_registers * sizeof(uint16_t));
- if (mb_mapping->tab_holding_registers == NULL) {
- free(mb_mapping->tab_coil_status);
- free(mb_mapping->tab_input_status);
- return -1;
- }
-
- mb_mapping->nb_input_registers = nb_input_registers;
- mb_mapping->tab_input_registers =
- (uint16_t *) malloc(nb_input_registers * sizeof(uint16_t));
- memset(mb_mapping->tab_input_registers, 0,
- nb_input_registers * sizeof(uint16_t));
- if (mb_mapping->tab_input_registers == NULL) {
- free(mb_mapping->tab_coil_status);
- free(mb_mapping->tab_input_status);
- free(mb_mapping->tab_holding_registers);
- return -1;
- }
- return 0;
- }
- void modbus_mapping_free(modbus_mapping_t *mb_mapping)
- {
- free(mb_mapping->tab_coil_status);
- free(mb_mapping->tab_input_status);
- free(mb_mapping->tab_holding_registers);
- free(mb_mapping->tab_input_registers);
- }
- int modbus_slave_listen_tcp(modbus_param_t *mb_param, int nb_connection)
- {
- int new_socket;
- int yes;
- struct sockaddr_in addr;
- new_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (new_socket < 0) {
- perror("socket");
- return -1;
- }
- yes = 1;
- if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEADDR,
- (char *) &yes, sizeof(yes)) < 0) {
- perror("setsockopt");
- close(new_socket);
- return -1;
- }
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
-
- addr.sin_port = htons(mb_param->port);
- addr.sin_addr.s_addr = INADDR_ANY;
- if (bind(new_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
- perror("bind");
- close(new_socket);
- return -1;
- }
- if (listen(new_socket, nb_connection) < 0) {
- perror("listen");
- close(new_socket);
- return -1;
- }
- return new_socket;
- }
- int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket)
- {
- struct sockaddr_in addr;
- socklen_t addrlen;
- addrlen = sizeof(struct sockaddr_in);
- mb_param->fd = accept(*socket, (struct sockaddr *)&addr, &addrlen);
- if (mb_param->fd < 0) {
- perror("accept");
- close(*socket);
- *socket = 0;
- } else {
- printf("The client %s is connected\n",
- inet_ntoa(addr.sin_addr));
- }
- return mb_param->fd;
- }
- void modbus_slave_close_tcp(int socket)
- {
- shutdown(socket, SHUT_RDWR);
- close(socket);
- }
- void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value)
- {
- int i;
- for (i=0; i<8; i++) {
- dest[address+i] = (value & (1 << i)) ? ON : OFF;
- }
- }
- void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
- const uint8_t tab_byte[])
- {
- int i;
- int shift = 0;
- for (i = address; i < address + nb_bits; i++) {
- dest[i] = tab_byte[(i - address) / 8] & (1 << shift) ? ON : OFF;
-
- shift++;
- shift %= 8;
- }
- }
- uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits)
- {
- int i;
- uint8_t value = 0;
- if (nb_bits > 8) {
- printf("Error: nb_bits is too big\n");
- nb_bits = 8;
- }
- for (i=0; i < nb_bits; i++) {
- value |= (src[address+i] << i);
- }
- return value;
- }
- float modbus_read_float(const uint16_t *src)
- {
- float r = 0.0f;
- uint32_t i;
- i = (((uint32_t)src[1]) << 16) + src[0];
- memcpy(&r, &i, sizeof (r));
- return r;
- }
- void modbus_write_float(float real, uint16_t *dest)
- {
- uint32_t i = 0;
- memcpy(&i, &real, sizeof (i));
- dest[0] = (uint16_t)i;
- dest[1] = (uint16_t)(i >> 16);
- }
|