/*
* Copyright © 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
The library is designed to send and receive data from a device that
communicate via the Modbus protocol.
The function names used are inspired by the Modicon Modbus Protocol
Reference Guide which can be obtained from Schneider at
www.schneiderautomation.com.
Documentation:
http://www.easysw.com/~mike/serial/serial.html
http://copyleft.free.fr/wordpress/index.php/libmodbus/
*/
#include "modbus.h"
#ifndef NATIVE_WIN32
#include <termios.h>
#endif /* NATIVE_WIN32 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
/* TCP */
#ifndef NATIVE_WIN32
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#endif /* NATIVE_WIN32 */
#include <sys/types.h>
#define UNKNOWN_ERROR_MSG "Not defined in modbus specification"
/* This structure reduces the number of params in functions and so
* optimizes the speed of execution (~ 37%). */
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[] = {
/* 0x00 */ UNKNOWN_ERROR_MSG,
/* 0x01 */ "Illegal function code",
/* 0x02 */ "Illegal data address",
/* 0x03 */ "Illegal data value",
/* 0x04 */ "Slave device or server failure",
/* 0x05 */ "Acknowledge",
/* 0x06 */ "Slave device or server busy",
/* 0x07 */ "Negative acknowledge",
/* 0x08 */ "Memory parity error",
/* 0x09 */ UNKNOWN_ERROR_MSG,
/* 0x0A */ "Gateway path unavailable",
/* 0x0B */ "Target device failed to respond"
};
/* Table of CRC values for high-order byte */
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
};
/* Table of CRC values for low-order byte */
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
};
extern void busMonitorAddItem( uint8_t isOut,
uint8_t slave,
uint8_t func,
uint16_t addr,
uint16_t nb,
uint16_t crc );
extern void busMonitorRawData( uint8_t * data,
uint8_t dataLen );
/* Treats errors and flush or close connection if necessary */
static void error_treat(modbus_param_t *mb_param, int code, const char *string)
{
printf("\nERROR %s (%d)\n", string, code);
if (mb_param->error_handling == FLUSH_OR_RECONNECT_ON_ERROR) {
switch (code) {
case ILLEGAL_DATA_VALUE:
case ILLEGAL_DATA_ADDRESS:
case ILLEGAL_FUNCTION:
break;
default:
if (mb_param->type_com == RTU) {
#ifdef NATIVE_WIN32
FlushFileBuffers(mb_param->w_ser.fd);
mb_param->w_ser.n_bytes = 0;
#else /* NATIVE_WIN32 */
tcflush(mb_param->fd, TCIOFLUSH);
#endif /* NATIVE_WIN32 */
} else {
modbus_close(mb_param);
modbus_connect(mb_param);
}
}
}
}
/* Computes the length of the expected response */
static unsigned int compute_response_length(modbus_param_t *mb_param,
uint8_t *query)
{
int length;
int offset;
offset = mb_param->header_length;
switch (query[offset + 1]) {
case FC_READ_COIL_STATUS:
case FC_READ_INPUT_STATUS: {
/* Header + nb values (code from force_multiple_coils) */
int nb = (query[offset + 4] << 8) | query[offset + 5];
length = 3 + (nb / 8) + ((nb % 8) ? 1 : 0);
}
break;
case FC_READ_HOLDING_REGISTERS:
case FC_READ_INPUT_REGISTERS:
/* Header + 2 * nb values */
length = 3 + 2 * (query[offset + 4] << 8 |
query[offset + 5]);
break;
case FC_READ_EXCEPTION_STATUS:
length = 4;
break;
default:
length = 6;
}
return length + offset + mb_param->checksum_length;
}
/* Builds a RTU query header */
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;
}
/* Builds a TCP query header */
static int build_query_basis_tcp(int slave, int function,
int start_addr, int nb,
uint8_t *query)
{
/* Extract from MODBUS Messaging on TCP/IP Implementation
Guide V1.0b (page 23/46):
The transaction identifier is used to associate the future
resp
没有合适的资源?快使用搜索试试~ 我知道了~
QT4.6实践编写串口调试助手(原创全源码)
共74个文件
svn-base:23个
cpp:11个
o:10个
5星 · 超过95%的资源 需积分: 48 418 下载量 114 浏览量
2009-12-29
09:32:15
上传
评论 14
收藏 1.6MB RAR 举报
温馨提示
QT4.6实践编写串口调试助手(原创全源码), 包括使用qextserial,包括可在linux和windows两种环境下识别通讯方式,和硬件接口识别。非常适合初学者学习,建议下载QT_Create最新版本
资源推荐
资源详情
资源评论
收起资源包目录
TestProtocol.rar (74个子文件)
TestProtocol
main.cpp 338B
TestProtocol.pro 1KB
mainwindow.h 776B
3rdparty
.svn
props
all-wcprops 84B
entries 240B
text-base
tmp
props
text-base
prop-base
prop-base
libmodbus
modbus.c 62KB
.svn
props
all-wcprops 440B
entries 649B
text-base
stdint-msvc.h.svn-base 7KB
modbus.h.svn-base 11KB
modbus.c.svn-base 62KB
tmp
props
text-base
prop-base
prop-base
modbus.h 11KB
stdint-msvc.h 7KB
qextserialport
qextserialport.h 894B
qextserialenumerator.cpp 5KB
posix_qextserialport.cpp 36KB
win_qextserialport.cpp 33KB
.svn
props
all-wcprops 1KB
entries 2KB
text-base
qextserialenumerator.h.svn-base 2KB
posix_qextserialport.cpp.svn-base 36KB
qextserialbase.h.svn-base 6KB
qextserialport.cpp.svn-base 3KB
qextserialport.h.svn-base 894B
qextserialenumerator.cpp.svn-base 5KB
win_qextserialport.h.svn-base 4KB
qextserialbase.cpp.svn-base 6KB
win_qextserialport.cpp.svn-base 33KB
posix_qextserialport.h.svn-base 2KB
tmp
props
text-base
prop-base
prop-base
qextserialport.cpp 3KB
posix_qextserialport.h 2KB
qextserialbase.cpp 6KB
qextserialenumerator.h 2KB
win_qextserialport.h 4KB
qextserialbase.h 6KB
ui_mainwindow.h 11KB
.svn
props
all-wcprops 1KB
entries 2KB
text-base
main.cpp.svn-base 338B
mainwindow.cpp.svn-base 3KB
TestProtocol.pro.user.svn-base 19KB
Makefile.svn-base 6KB
Makefile.Debug.svn-base 9KB
Makefile.Release.svn-base 9KB
TestProtocol.pro.svn-base 1KB
ui_mainwindow.h.svn-base 11KB
mainwindow.ui.svn-base 8KB
mainwindow.h.svn-base 707B
dir-prop-base 31B
tmp
props
text-base
prop-base
prop-base
object_script.TestProtocol.Release 304B
Makefile.Debug 9KB
release
.svn
props
all-wcprops 83B
entries 202B
text-base
tmp
props
text-base
prop-base
prop-base
mainwindow.cpp 4KB
mainwindow.ui 8KB
object_script.TestProtocol.Debug 284B
Makefile.Release 9KB
Makefile 6KB
TestProtocol.pro.user 19KB
debug
qextserialport.o 105KB
moc_mainwindow.o 127KB
moc_qextserialport.o 110KB
moc_qextserialbase.o 114KB
TestProtocol.exe 4.89MB
moc_win_qextserialport.cpp 2KB
qextserialenumerator.o 139KB
moc_qextserialport.cpp 2KB
main.o 112KB
moc_mainwindow.cpp 3KB
qextserialbase.o 117KB
moc_win_qextserialport.o 115KB
moc_qextserialbase.cpp 3KB
win_qextserialport.o 148KB
mainwindow.o 312KB
共 74 条
- 1
kenter1643
- 粉丝: 2
- 资源: 41
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页