資源簡介
Modbus是一種串行通信協議,是Modicon公司(現在的施耐德電氣 Schneider Electric)于1979年為使用可編程邏輯控制器(PLC)通信而發表。Modbus已經成為工業領域通信協議的業界標準(De facto),并且現在是工業電子設備之間常用的連接方式。libmodbus庫源文件
代碼片段和文件信息
/*
?*?Copyright???2001-2011?Stéphane?Raimbault?
?*
?*?This?library?is?free?software;?you?can?redistribute?it?and/or
?*?modify?it?under?the?terms?of?the?GNU?Lesser?General?Public
?*?License?as?published?by?the?Free?Software?Foundation;?either
?*?version?2.1?of?the?License?or?(at?your?option)?any?later?version.
?*
?*?This?library?is?distributed?in?the?hope?that?it?will?be?useful
?*?but?WITHOUT?ANY?WARRANTY;?without?even?the?implied?warranty?of
?*?MERCHANTABILITY?or?FITNESS?FOR?A?PARTICULAR?PURPOSE.??See?the?GNU
?*?Lesser?General?Public?License?for?more?details.
?*
?*?You?should?have?received?a?copy?of?the?GNU?Lesser?General?Public
?*?License?along?with?this?library;?if?not?write?to?the?Free?Software
?*?Foundation?Inc.?51?Franklin?Street?Fifth?Floor?Boston?MA?02110-1301?USA
?*
?*
?*?This?library?implements?the?Modbus?protocol.
?*?http://libmodbus.org/
?*/
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?“modbus.h“
#include?“modbus-private.h“
/*?Internal?use?*/
#define?MSG_LENGTH_UNDEFINED?-1
/*?Exported?version?*/
const?unsigned?int?libmodbus_version_major?=?LIBMODBUS_VERSION_MAJOR;
const?unsigned?int?libmodbus_version_minor?=?LIBMODBUS_VERSION_MINOR;
const?unsigned?int?libmodbus_version_micro?=?LIBMODBUS_VERSION_MICRO;
/*?Max?between?RTU?and?TCP?max?adu?length?(so?TCP)?*/
#define?MAX_MESSAGE_LENGTH?260
/*?3?steps?are?used?to?parse?the?query?*/
typedef?enum?{
????_STEP_FUNCTION
????_STEP_meta
????_STEP_DATA
}?_step_t;
const?char?*modbus_strerror(int?errnum)?{
????switch?(errnum)?{
????case?EMBXILFUN:
????????return?“Illegal?function“;
????case?EMBXILADD:
????????return?“Illegal?data?address“;
????case?EMBXILVAL:
????????return?“Illegal?data?value“;
????case?EMBXSFAIL:
????????return?“Slave?device?or?server?failure“;
????case?EMBXACK:
????????return?“Acknowledge“;
????case?EMBXSBUSY:
????????return?“Slave?device?or?server?is?busy“;
????case?EMBXNACK:
????????return?“Negative?acknowledge“;
????case?EMBXMEMPAR:
????????return?“Memory?parity?error“;
????case?EMBXGPATH:
????????return?“Gateway?path?unavailable“;
????case?EMBXGTAR:
????????return?“Target?device?failed?to?respond“;
????case?EMBBADCRC:
????????return?“Invalid?CRC“;
????case?EMBBADDATA:
????????return?“Invalid?data“;
????case?EMBBADEXC:
????????return?“Invalid?exception?code“;
????case?EMBMDATA:
????????return?“Too?many?data“;
????default:
????????return?strerror(errnum);
????}
}
void?_error_print(modbus_t?*ctx?const?char?*context)
{
????if?(ctx->debug)?{
????????fprintf(stderr?“ERROR?%s“?modbus_strerror(errno));
????????if?(context?!=?NULL)?{
????????????fprintf(stderr?“:?%s\n“?context);
????????}?else?{
????????????fprintf(stderr?“\n“);
????????}
????}
}
int?_sleep_and_flush(modbus_t?*ctx)
{
#ifdef?_WIN32
????/*?usleep?doesn‘t?exist?on?Windows?*/
????Sleep((ctx->response_timeout.tv_sec?*?1000)?+
??????????(ctx->response_timeout.tv_usec?/?10
評論
共有 條評論