?? modbusclient.h
字號:
#pragma once
#include "ModbusConnection.h"
// Modbus function codes for supported requests (see Modbus Protocol Specification)
#define MODBUS_READ_REGS 3 // Read Multiple Registers
#define MODBUS_WRITE_REGS 16 // Write Multiple Registers
// Modbus exception codes (see Modbus Protocol Specification)
#define ILLEGAL_FUNCTION 1
#define ILLEGAL_ADDRESS 2
#define ILLEGAL_VALUE 3
// Max allowed request quantities (see Modbus Protocol Specification)
#define MAX_READ_REGS 125 // Max for Read Multiple Registers
#define MAX_WRITE_REGS 100 // Max for Write Multiple Registers
// Modbus/TCP constants (see Modbus/TCP Protocol Specification)
#define LEN_HEADER 7 // Length (bytes) of MBAP Header
#define LEN_DESTID 1 // Length (bytes) of 'Destination ID' field of MBAP Header
#define LEN_MINPDU 1 // Length (bytes) of shortest valid PDU
#define LEN_MAXPDU 253 // Length (bytes) of longest valid PDU [see note below]
/*
* Modbus/TCP Spec states a max PDU length of 249 bytes. However,
* in order to support the well documented max request quantity
* of 125 registers for Modbus Function Code 03 (Read Multiple
* Registers), it's necessary to maintain the same max PDU
* (not ADU) as serial-line implementations, which is 253 bytes.
*/
// Arbitrary number of 'Registers' for the data area
#define N_REGISTERS 30
#define LEN_MAXADU (LEN_HEADER + LEN_MAXPDU)
// Modbus REQUEST structure used by this module
typedef struct tagREQUEST
{
BYTE bFnCode; // Modbus Function Code of request
WORD wAddr; // Address of first 'Register' to read (or write)
WORD wQty; // Quantity of 'Registers' to read (or write)
WORD awData[MAX_READ_REGS]; // The data values read (or to be written)
}REQUEST, *LPREQUEST;
class CModbusClient
{
public:
CModbusClient(void);
~CModbusClient(void);
void StartClientThread( HANDLE hConn);
static WORD ReceiveRequest( SOCKET socket, LPBYTE buffer, LPWSAOVERLAPPED lpOverlap, HANDLE hStop );
static BOOL ReceiveBytes( SOCKET socket, LPWSABUF lpBuffer, LPWSAOVERLAPPED lpOverlap, HANDLE hStop );
static WORD ParseHeader( LPBYTE buffer );
static BYTE ParseRequest( LPBYTE buffer, WORD wRecv, LPREQUEST lpRequest );
static BYTE ProcessRequest( LPREQUEST lpRequest );
static WORD MakePositiveResponse( LPREQUEST lpRequest, LPBYTE buffer );
static WORD MakeErrorResponse( BYTE bError, BYTE bFnCode, LPBYTE buffer );
static BOOL SendResponse( SOCKET socket, LPBYTE buffer, WORD nBytesToSend );
static BOOL SetRegisters( int iStart, int nQty, LPWORD lpData );
static BOOL GetRegisters( int iStart, int nQty, LPWORD lpData );
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -