#include "pch.h"
#include "modbus_init.h"
//#include "class.h"
using namespace std;
#pragma comment(lib,"ws2_32.lib")
int e = 0;
long int coil_ADR[COIL_ALL] = { 0 , 0, 0,0}; //线圈地址位的数据,二进制表示
unsigned char coil[COIL_ALL] = { 0 }; //转化后要发给客户端的数据,十六进制数
unsigned char relay[2] = { 0x01,0x04 }; //读离散量输入值(ON/OFF) 02 bit 16路DI:0000 0110 0000 1111(高到低)
//unsigned char holding_reg[32] = { 0x01,0x01,0x01,0x02,0x00,0x03,0x00,0x04,0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08,
// 0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08, };
int holding_reg[HOLDING_ALL] = { 0 };
//读保持寄存器值 03 word (响应报文按字节来的,这里不用word定义了)
unsigned char input_reg[32] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
//读输入寄存器值 04 word 全为0x0101,即257
void solve_01(SOCKET clntSock, unsigned char request[], unsigned char coil[]) {
int a = request[11] - request[9];
int b = request[11];
int c = request[9];
int d = a;
do
{
d = d - 8;
if (d >=0)
{
int q = c + e * 8;
huan(coil_ADR, 8, q);
e++;
}
else
{
int q = c + e * 8;
int f = d + 8;
huan(coil_ADR, f, q);
e++;
}
} while (d>0);
e = 0;
int len, n;
len = request[11]; //需要读取的点的个数
printf("****%d*****", len);
n = (len % 8) == 0 ? (len / 8) : (len / 8 + 1);
printf("****%d*****", n);//对应的的字节数
unsigned char send_buff[8 + 50] = { 0x00,0x01,0x00,0x00,0x00,0x06,0x00,0x01 }; //返回数据缓冲区,前八个字节不变(除了长度位),后面最多3个字节
send_buff[6] = request[6];
send_buff[0] = request[0];
send_buff[1] = request[1];
send_buff[5] = 2 + 1 + n; //确定响应报文第六位,即后续报文长度:01+02+n+n bytes data
send_buff[8] = n;
for (int i = 0; i < n; i++) {
send_buff[i + 9] = coil[i];
}
printf("\n该指令为读线圈/离散量状态输出即DO,返回内容应该为:\n");
for (int i = 0; i < 8 + 1 + n; i++)printf("0x0%x ", send_buff[i]);
printf("\n**************************************************************************************************");
send(clntSock, (const char*)send_buff, 8 + 1 + n, 0); //响应请求
}
void solve_02(SOCKET clntSock, unsigned char request[], unsigned char relay[]) {
int len, n;
len = request[11]; //需要读取的点的个数
n = (len % 8) == 0 ? (len / 8) : (len / 8 + 1); //对应的的字节数
unsigned char send_buff[8 + 3] = { 0x00,0x01,0x00,0x00,0x00,0x06,0x01,0x02 }; //返回数据缓冲区,前八个字节不变(除了长度位),后面最多3个字节
send_buff[5] = 2 + 1 + n; //确定响应报文第六位,即后续报文长度:01+02+n+n bytes data
send_buff[8] = n;
for (int i = 0; i < n; i++) {
send_buff[i + 9] = relay[i];
}
printf("\n该指令为读离散量输入即DI,返回内容应该为:\n");
for (int i = 0; i < 8 + 1 + n; i++)printf("0x0%x ", send_buff[i]);
printf("\n**************************************************************************************************");
send(clntSock, (const char*)send_buff, 8 + 1 + n, 0); //响应请求
}
void solve_03(SOCKET clntSock, unsigned char request[], int holding_reg[]) {
int len, n;
len = request[11]; //需要读取的寄存器的个数
n = 2 * len; //对应的的字节数
unsigned char send_buff[8 + 500] = { 0x00,0x01,0x00,0x00,0x00,0x06,0x01,0x03 }; //返回数据缓冲区,前八个字节不变(除了长度位),后面最多100个字节
send_buff[6] = request[6];
send_buff[0] = request[0];
send_buff[1] = request[1];
send_buff[5] = 2 + 1 + n; //确定响应报文第六位,即后续报文长度:01+02+n+n bytes data
send_buff[8] = n;
for (int i = 0; i < n; i++) {
send_buff[i + 9] = holding_reg[i];
send_buff[i + 10] = holding_reg[i+1];
i++;
TRACE("%d\n", holding_reg[i]);
}
/*printf("\n该指令为读保持寄存器,返回内容应该为:\n");
for (int i = 0; i < 8 + 1 + n; i++)printf("0x0%x ", send_buff[i]);
printf("\n**************************************************************************************************");*/
send(clntSock, (const char*)send_buff, 8 + 1 + n, 0); //响应请求
}
void solve_04(SOCKET clntSock, unsigned char request[], unsigned char input_reg[]) {
int len, n;
len = request[11]; //需要读取的寄存器的个数
n = 2 * len; //对应的的字节数
unsigned char send_buff[8 + 33] = { 0x00,0x01,0x00,0x00,0x00,0x06,0x01,0x04 }; //返回数据缓冲区,前八个字节不变(除了长度位),后面最多33个字节
send_buff[5] = 2 + 1 + n; //确定响应报文第六位,即后续报文长度:01+02+n+n bytes data
send_buff[8] = n;
for (int i = 0; i < n; i++) {
send_buff[i + 9] = input_reg[i];
}
printf("\n该指令为读输入寄存器,返回内容应该为:\n");
for (int i = 0; i < 8 + 1 + n; i++)printf("0x0%x ", send_buff[i]);
printf("\n**************************************************************************************************");
send(clntSock, (const char*)send_buff, 8 + 1 + n, 0); //响应请求
}
void solve_05(SOCKET clntSock, unsigned char request[])
{ //对应的的字节数
unsigned char send_buff[8 + 33] = { 0x00,0x01,0x00,0x00,0x00,0x06,0x01,0x05 }; //返回数据缓冲区,前八个字节不变(除了长度位),后面最多33个字节
send_buff[6] = request[6];
send_buff[0] = request[0];
send_buff[1] = request[1];
for (int i = 0; i < 3; i++) {
send_buff[i + 9] = request[i + 9];
}
printf("\n写入成功,返回内容应该为:\n");
for (int i = 0; i < 12; i++)printf("0x0%x ", send_buff[i]);
printf("\n**************************************************************************************************\n");
if (send_buff[10] == COIL_ON)
{
int a = request[9];
coil_ADR[a] = 1;
}
else if (send_buff[10] == COIL_OFF)
{
int a = request[9];
coil_ADR[a] = 0;
}
send(clntSock, (const char*)send_buff, 12, 0); //响应请求
}
void solve_all(SOCKET clnSock, unsigned char request[]) {
switch (request[7]) {
case 1:solve_01(clnSock, request, coil); break;
case 2:solve_02(clnSock, request, relay); break; //DI 读离散量输入
case 3:solve_03(clnSock, request, holding_reg); break;
case 4:solve_04(clnSock, request, input_reg); break;
case 5:solve_05(clnSock, request); break;
}
}
void huan(long int ss[], int n, int j)
{
long int a = 1, b = 0;
for (int i=0; i < n; i++)
{
TRACE("%d\n", coil_ADR[j]);
if (coil_ADR[j++] > 0)
{
a = 1;
a = a << i;
b += a;
}
else
{
a = 0;
a = a << i;
b += a;
}
}
TRACE("%ld\n", b);
coil[e] = b;
}
评论0