#include “MCGS.h”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdbool.h>
#include “/home/zlgmcu/vscode/EMS_SNV/source/BSW/Protocal/DataTransfer.h”
/*
*************************** Product Code Begin ************************************
*/
int serv_sock = -1;
pthread_t recv_thread;
modbus_tcp_frame_t tcp_frame;
uint16_t inputRegisterZone3[10000];
uint16_t holdingRegisterZone4[10000];
bool coilRegisteZone0[10000];
bool inputRegisteZone1[10000];
uint16_t startAddr=0;
uint16_t registerNum=0;
uint8_t cmd=0;
void MCGS_Init()
{
for (size_t i = 0; i < 10000; i++)
{
holdingRegisterZone4[i]=40;
}
for (size_t i = 0; i < 10000; i++)
{
inputRegisterZone3[i]=30;
}
int ret;
struct ifreq if_request;
//创建套接字
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
{
perror("server->socket");
return ;
}
// 指定网络接口,例如 "eth0"
strcpy(if_request.ifr_name, "eth1");
/* // 获取接口信息
if (ioctl(serv_sock, SIOCGIFADDR, &if_request) < 0) {
perror("ioctl failed");
close(serv_sock);
exit(EXIT_FAILURE);
}*/
//将套接字和IP、端口绑定
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr =inet_addr(MCGS_SERVER_IP);//htonl(INADDR_ANY);/ //具体的IP地址
serv_addr.sin_port = htons(MCGS_SERVER_PORT); //端口
ret=bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(-1 == ret)
{
perror("server->bind");
return;
}
else
{
printf("bind ok %s,%d \n",MCGS_SERVER_IP,MCGS_SERVER_PORT);
}
//进入监听状态,等待用户发起请求
ret=listen(serv_sock, 5);
if(-1 == ret)
{
perror("server->listen");
return;
}
printf("等待连接......\n");
}
void MCGS_Deal_CMD01(int sock)
{
char send_buf[1024];
int p=0;
uint16_t temp=0;
uint8_t tempByte=0;
char recv_buf[1024];
char *recv_arr;
if(tcp_frame.func_code==0x01)
{
send_buf[p++]=(uint8_t)(tcp_frame.trans_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.trans_id);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id);
if((tcp_frame.reg_count%8)==0)
{
temp=tcp_frame.reg_count/8;
}
else
{
temp=tcp_frame.reg_count/8+1;
}
send_buf[p++]=(uint8_t)((temp+3)>>8);
send_buf[p++]=(uint8_t)(temp+3);
send_buf[p++]=tcp_frame.unit_id;
send_buf[p++]=tcp_frame.func_code;
send_buf[p++]=temp;
for (size_t j = 0; j < temp; j++)
{
for (size_t i = 0; i < 8; i++)
{
tempByte+=coilRegisteZone0[j*8+i]<<i;
}
send_buf[p++]=tempByte;
}
if (send(sock, send_buf, p, 0) < 0)
{
perror("send");
}
else
{
recv_arr=HexToAsciiStr(send_buf,p);
printf("MCGS_SendData:%s \n",recv_arr);
}
}
}
void MCGS_Deal_CMD02(int sock)
{
char send_buf[1024];
int p=0;
uint16_t temp=0;
uint8_t tempByte=0;
char recv_buf[1024];
char *recv_arr;
if(tcp_frame.func_code==0x02)
{
send_buf[p++]=(uint8_t)(tcp_frame.trans_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.trans_id);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id);
if((tcp_frame.reg_count%8)==0)
{
temp=tcp_frame.reg_count/8;
}
else
{
temp=tcp_frame.reg_count/8+1;
}
send_buf[p++]=(uint8_t)((temp+3)>>8);
send_buf[p++]=(uint8_t)(temp+3);
send_buf[p++]=tcp_frame.unit_id;
send_buf[p++]=tcp_frame.func_code;
send_buf[p++]=temp;
for (size_t j = 0; j < temp; j++)
{
for (size_t i = 0; i < 8; i++)
{
tempByte+=inputRegisteZone1[j*8+i]<<i;
}
send_buf[p++]=tempByte;
}
if (send(sock, send_buf, p, 0) < 0)
{
perror("send");
}
else
{
recv_arr=HexToAsciiStr(send_buf,p);
printf("MCGS_SendData:%s \n",recv_arr);
}
}
}
void MCGS_Deal_CMD03(int sock)
{
char send_buf[1024];
int p=0;
uint16_t temp=0;
char recv_buf[1024];
char recv_arr;
if(tcp_frame.func_code==0x03)
{
send_buf[p++]=(uint8_t)(tcp_frame.trans_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.trans_id);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id);
temp=3+tcp_frame.reg_count2;
send_buf[p++]=(uint8_t)(temp>>8);
send_buf[p++]=(uint8_t)(temp);
send_buf[p++]=tcp_frame.unit_id;
send_buf[p++]=tcp_frame.func_code;
send_buf[p++]=tcp_frame.reg_count*2;
for (size_t i = 0; i < tcp_frame.reg_count; i++)
{
send_buf[p++]=(uint8_t)(holdingRegisterZone4[i]>>8);
send_buf[p++]=(uint8_t)holdingRegisterZone4[i];
}
if (send(sock, send_buf, p, 0) < 0)
{
perror("send");
}
else
{
recv_arr=HexToAsciiStr(send_buf,p);
printf("MCGS_SendData:%s \n",recv_arr);
}
}
}
void MCGS_Deal_CMD04(int sock)
{
char send_buf[1024];
int p=0;
uint16_t temp=0;
char recv_buf[1024];
char recv_arr;
if(tcp_frame.func_code==0x04)
{
send_buf[p++]=(uint8_t)(tcp_frame.trans_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.trans_id);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id>>8);
send_buf[p++]=(uint8_t)(tcp_frame.proto_id);
temp=3+tcp_frame.reg_count2;
send_buf[p++]=(uint8_t)(temp>>8);
send_buf[p++]=(uint8_t)(temp);
send_buf[p++]=tcp_frame.unit_id;
send_buf[p++]=tcp_frame.func_code;
send_buf[p++]=tcp_frame.reg_count*2;
for (size_t i = 0; i < tcp_frame.reg_count; i++)
{
send_buf[p++]=(uint8_t)(inputRegisterZone3[i]>>8);
send_buf[p++]=(uint8_t)inputRegisterZone3[i];
}
if (send(sock, send_buf, p, 0) < 0)
{
perror("send");
}
else
{
recv_arr=HexToAsciiStr(send_buf,p);
printf("MCGS_SendData:%s \n",recv_arr);
}
}
}
void *MCGS_RecvFunction(void *arg)
{
int sockfd = (int)(long)arg;
char recv_buf[1024];
char send_buf[1024];
int recv_result;
int modbusTcp_len=0;
int modbus_len=0;
int slaveId=0;
int protocal=0;
char *recv_arr;
while(1)
{
// 接收数据
// 接收Modbus TCP帧
recv_result = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
if (recv_result > 0)
{
tcp_frame.trans_id=recv_buf[0]*0x100+recv_buf[1];
tcp_frame.proto_id=recv_buf[2]*0x100+recv_buf[3];
tcp_frame.len=recv_buf[4]*0x100+recv_buf[5];
tcp_frame.unit_id=recv_buf[6];
tcp_frame.func_code=recv_buf[7];
tcp_frame.reg_addr=recv_buf[8]*0x100+recv_buf[9];
tcp_frame.reg_count=recv_buf[10]*0x100+recv_buf[11];
recv_arr=HexToAsciiStr(recv_buf,recv_result);
printf("MCGS_RecvData:%s \n",recv_arr);
if((tcp_frame.proto_id==00)&&(tcp_frame.len==06)&&(tcp_frame.unit_id==1))
{
if(tcp_frame.func_code==0x01)//read holding register 40000~49999
{
MCGS_Deal_CMD01(sockfd);
}
if(tcp_frame.func_code==0x02)//read holding register 40000~49999
{
MCGS_Deal_CMD02(sockfd);
}
else if(tcp_frame.func_code==0x03)//read holding register 40000~49999
{
MCGS_Deal_CMD03(sockfd);
}
else if(tcp_frame.func_code==0x04)//read holding register 40000~49999
{
MCGS_Deal_CMD04(sockfd);
}
}
}
else if (recv_result==0)
{
printf("recv error:connection closed by peer \n");
close(sockfd);
pthread_exit(0);
}
}
}
void *MCGS_ThreadFunction(void *arg)
{
uint8_t *recv_arr=“”;
char buffer[1024] = {0};
ssize_t size = 0;
int result=0;
//接收客户端请求
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size = sizeof(clnt_addr);
while (1)
{
/* code */
usleep(500000);//50 ms
int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
if (clnt_sock < 0 )
{
perror("accept error"); // 打印错误信息及对应的errno值
printf("连接失败1\n");
//exit(1);
continue;
}
else {
printf("连接成功\n");
//printf("客户端IP:%s\t端口:%d\n", inet_ntop(AF_INET, &clnt_addr.sin_addr.s_addr, clie_IP, sizeof(clie_IP)), ntohs(clnt_addr.sin_port));
//向客户端发送和接收数据,使用线程方式
int send_result, recv_result;
recv_result = pthread_create(&recv_thread, NULL, MCGS_RecvFunction, (void*)(long)clnt_sock);
}
}
}
void MCGS_CreateThread()
{
pthread_t threadID;
pthread_create(&threadID,NULL,MCGS_ThreadFunction,NULL);
}