null

西门子S7-200通讯协议(PPI)源代码 C语言

  • 1星
  • 日期: 2017-08-11
  • 大小: 17.3KB
  • 所需积分:2分
  • 下载次数:9
  • favicon收藏
  • rep举报
  • 分享
  • free评论
标签: S7-200

S7-200 是一种小型的可编程序控制器,适用于各行各业,各种场合中的检测、监测及控制的自动化。S7-200系列的强大功能使其无论在独立运行中,或相连成网络皆能实现复杂控制功能。

PPI

PPI

西门子S7-200的通讯协议源代,支持复合多区块读取和单区块的写入,已经验证过和S7-200的通讯,目前支持M和VB,VW,VD,需要支持其他的寄存器请参考网上的寄存器定义

文档内容节选

HANDLE PPIUARTCommunPort int PPICommunState bool ReInitFlag define READREG1 0 define WRITEREG1 1 define WRITEREG2 2 define WRITEREG3 3 define PPIMREG 1 define PPIVBREG 2 define PPIVWREG 3 define PPIVDREG 4 typedef struct BYTE regtype DWORD regaddr DWORD reglen void regvalue PPIRequestUnit define MAXPPIREQUESTNUM 8 PPIRequestUnit PPIRequestMAXPPIREQUESTNUM BYTE PPICommunBuffer256 0 BYTE PLCAddr PLC通讯地址 BYTE LocalAddr 本机通讯地址 void PPIUartClosevoid ifPPIUARTCommunPort......

HANDLE PPI_UART_CommunPort; int PPI_Commun_State; bool ReInitFlag; #define READ_REG1 0 #define WRITE_REG1 1 #define WRITE_REG2 2 #define WRITE_REG3 3 #define PPI_M_REG 1 #define PPI_VB_REG 2 #define PPI_VW_REG 3 #define PPI_VD_REG 4 typedef struct{ BYTE reg_type; DWORD reg_addr; DWORD reg_len; void *reg_value; }PPI_Request_Unit; #define MAX_PPI_REQUEST_NUM 8 PPI_Request_Unit PPI_Request[MAX_PPI_REQUEST_NUM]; BYTE PPI_Commun_Buffer[256] = {0}; BYTE PLC_Addr; //PLC通讯地址 BYTE Local_Addr; //本机通讯地址 void PPI_Uart_Close(void) { if(PPI_UART_CommunPort != INVALID_HANDLE_VALUE) { CloseHandle(PPI_UART_CommunPort); } } bool PPI_Uart_Open(void) { DCB dcb; COMMTIMEOUTS CommTimeOuts; PLC_Addr = 2; //PLC通讯地址 Local_Addr = 0; //本机通讯地址 ReInitFlag = false; PPI_UART_CommunPort = INVALID_HANDLE_VALUE; //打开串口,COM1,9600,E,8,1 PPI_UART_CommunPort = CreateFile(L"COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL) ; if(PPI_UART_CommunPort != INVALID_HANDLE_VALUE) { //串口打开成功,设置串口 CommTimeOuts.ReadIntervalTimeout = 100 ; CommTimeOuts.ReadTotalTimeoutMultiplier = 3 ; CommTimeOuts.ReadTotalTimeoutConstant = 200 ; CommTimeOuts.WriteTotalTimeoutMultiplier = 5 ; CommTimeOuts.WriteTotalTimeoutConstant = 50 ; SetCommTimeouts( PPI_UART_CommunPort, &CommTimeOuts ); dcb.DCBlength = sizeof(DCB); GetCommState(PPI_UART_CommunPort, &dcb); //取得通讯资源当前设置 dcb.BaudRate = GAS193_Machion_value.ComConfig.ComBaud; //设定波特率为9600 dcb.fBinary = TRUE ; dcb.fNull = FALSE ; dcb.fParity = TRUE ; dcb.Parity = EVENPARITY ; dcb.ByteSize = 8 ; dcb.StopBits = ONESTOPBIT; SetCommState( PPI_UART_CommunPort, &dcb ); SetupComm( PPI_UART_CommunPort, 2048, 2048 ); PurgeComm( PPI_UART_CommunPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ; return true; } return false; } //计算PPI协议通讯校验码 BYTE PPI_CheckSum(BYTE *buf, int len) { BYTE CheckSum_Value = 0; int i; for(i=0;i 8) { return false; } //SD PPI_Commun_Buffer[0] = 0x68; //LE PPI_Commun_Buffer[1] = 15 + 12 * PPI_Read_Req_Len; //LEr PPI_Commun_Buffer[2] = 15 + 12 * PPI_Read_Req_Len; //SD PPI_Commun_Buffer[3] = 0x68; //DA PPI_Commun_Buffer[4] = PLC_Addr; //SD PPI_Commun_Buffer[5] = Local_Addr; //FC,功能码 PPI_Commun_Buffer[6] = 0x6C; //协议识别 PPI_Commun_Buffer[7] = 0x32; //远程控制 PPI_Commun_Buffer[8] = 0x01; //冗余识别 PPI_Commun_Buffer[9] = 0x00; PPI_Commun_Buffer[10] = 0x00; //协议数据 PPI_Commun_Buffer[11] = 0x00; //单元参考 PPI_Commun_Buffer[12] = 0x00; //参数长度 PPI_Commun_Buffer[13] = (2 + 12 * PPI_Read_Req_Len) >> 8; PPI_Commun_Buffer[14] = 2 + 12 * PPI_Read_Req_Len; //数据长度 PPI_Commun_Buffer[15] = 0x00; PPI_Commun_Buffer[16] = 0x00; //04读 PPI_Commun_Buffer[17] = 0x04; //变量地址数 PPI_Commun_Buffer[18] = PPI_Read_Req_Len; for(i=0, PPI_Commun_Buffer_Ptr=19;i> 8; PPI_Commun_Buffer[26 + i * 12] = 0x00; PPI_Commun_Buffer[27 + i * 12] = 0x83; offset = PPI_Read_Req[i].reg_addr / 10 * 8; break; case PPI_VB_REG: PPI_Commun_Buffer[22 + i * 12] = 0x02; PPI_Commun_Buffer[23 + i * 12] = 0x00; PPI_Commun_Buffer[24 + i * 12] = PPI_Read_Req[i].reg_len; PPI_Commun_Buffer[25 + i * 12] = PPI_Read_Req[i].reg_len >> 8; PPI_Commun_Buffer[26 + i * 12] = 0x01; PPI_Commun_Buffer[27 + i * 12] = 0x84; offset = PPI_Read_Req[i].reg_addr * 8; break; case PPI_VW_REG: PPI_Commun_Buffer[22 + i * 12] = 0x04; PPI_Commun_Buffer[23 + i * 12] = 0x00; PPI_Commun_Buffer[24 + i * 12] = PPI_Read_Req[i].reg_len; PPI_Commun_Buffer[25 + i * 12] = PPI_Read_Req[i].reg_len >> 8; PPI_Commun_Buffer[26 + i * 12] = 0x01; PPI_Commun_Buffer[27 + i * 12] = 0x84; offset = PPI_Read_Req[i].reg_addr * 8; break; case PPI_VD_REG: PPI_Commun_Buffer[22 + i * 12] = 0x06; PPI_Commun_Buffer[23 + i * 12] = 0x00; PPI_Commun_Buffer[24 + i * 12] = PPI_Read_Req[i].reg_len; PPI_Commun_Buffer[25 + i * 12] = PPI_Read_Req[i].reg_len >> 8; PPI_Commun_Buffer[26 + i * 12] = 0x01; PPI_Commun_Buffer[27 + i * 12] = 0x84; offset = PPI_Read_Req[i].reg_addr * 8; break; default: return false; } //偏移量 PPI_Commun_Buffer[28 + i * 12] = offset >> 16; PPI_Commun_Buffer[29 + i * 12] = offset >> 8; PPI_Commun_Buffer[30 + i * 12] = offset; } //FCS,校验码 PPI_Commun_Buffer[PPI_Commun_Buffer_Ptr] = PPI_CheckSum(PPI_Commun_Buffer + 4, PPI_Commun_Buffer_Ptr - 4); PPI_Commun_Buffer_Ptr++; //DE,结束符 PPI_Commun_Buffer[PPI_Commun_Buffer_Ptr] = 0x16; PPI_Commun_Buffer_Ptr++; //发送读请求数据包 WriteFile(PPI_UART_CommunPort, PPI_Commun_Buffer, PPI_Commun_Buffer_Ptr, &tmp_commun_len, NULL ); //接受PLC的确认应答包 if(!ReadFile(PPI_UART_CommunPort, PPI_Commun_Buffer, 256, &tmp_commun_len, NULL )) { return false; } //应答包只有一个数据0xE5 if(tmp_commun_len != 1) { return false; } if(PPI_Commun_Buffer[0] != 0xE5) { return false; } //再次发送确认指令 PPI_Commun_Buffer[0] = 0x10; //DA PPI_Commun_Buffer[1] = PLC_Addr; //SD PPI_Commun_Buffer[2] = Local_Addr; PPI_Commun_Buffer[3] = 0x5C; PPI_Commun_Buffer[4] = PPI_CheckSum(PPI_Commun_Buffer + 1, 3); PPI_Commun_Buffer[5] = 0x16; WriteFile(PPI_UART_CommunPort, PPI_Commun_Buffer, 6, &tmp_commun_len, NULL ); //接受PLC返回的数据包 if(!ReadFile(PPI_UART_CommunPort, PPI_Commun_Buffer, 256, &tmp_commun_len, NULL )) { return false; } //低于16个字节的应答包不含有返回的寄存器数据 if(tmp_commun_len < 16) { return false; } //检查校验码 if(PPI_Commun_Buffer[tmp_commun_len - 2] != PPI_CheckSum(PPI_Commun_Buffer + 4, tmp_commun_len - 6)) { return false; } //检查地址是否匹配 if(PPI_Commun_Buffer[4] != Local_Addr) { return false; } if(PPI_Commun_Buffer[5] != PLC_Addr) { return false; } //从21字节开始就是返回的 PPI_Commun_Buffer_Ptr = 21; for(i=0;i> 16; PPI_Commun_Buffer[29] = offset >> 8; PPI_Commun_Buffer[30] = offset; //数据形式,03 - 位, 04 - 其他 switch(reg_type) { case PPI_M_REG: PPI_Commun_Buffer[31] = 0x00; PPI_Commun_Buffer[32] = 0x03; break; case PPI_VB_REG: case PPI_VW_REG: case PPI_VD_REG: PPI_Commun_Buffer[31] = 0x00; PPI_Commun_Buffer[32] = 0x04; break; default: return false; } //数据位数 switch(reg_type) { case PPI_M_REG: PPI_Commun_Buffer[33] = 0x00; PPI_Commun_Buffer[34] = 0x01; break; case PPI_VB_REG: PPI_Commun_Buffer[33] = (reg_len * 8) >> 8; PPI_Commun_Buffer[34] = reg_len * 8; break; case PPI_VW_REG: PPI_Commun_Buffer[33] = (reg_len * 16) >> 8; PPI_Commun_Buffer[34] = reg_len * 16; break; case PPI_VD_REG: PPI_Commun_Buffer[33] = (reg_len * 32) >> 8; PPI_Commun_Buffer[34] = reg_len * 32; break; default: return false; } tmp_reg_addr = 35; tmp_reg_ptr = (BYTE *)reg_value; switch(reg_type) { case PPI_M_REG: PPI_Commun_Buffer[tmp_reg_addr] = *tmp_reg_ptr; tmp_reg_addr++; break; case PPI_VB_REG: for(i=0;i
更多简介内容

推荐帖子

评论

johnsonxjq
C码完全实现了PPI协议,只是没有举例怎么调用,稍嫌遗憾
2020-01-18 23:18:29回复
登录/注册

意见反馈

求资源

回顶部

datasheet推荐 换一换

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版 版权声明

北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2021 EEWORLD.com.cn, Inc. All rights reserved
$(function(){ var appid = $(".select li a").data("channel"); $(".select li a").click(function(){ var appid = $(this).data("channel"); $('.select dt').html($(this).html()); $('#channel').val(appid); }) })