具体从PLC读取函数查看官方手册,在此主要写一下获取数据后存储到对应结构体

结构体头文件

#ifndef MSTC_H
#define MSTC_H//! 与beckhoff交互信息结构体
//! 与beckhoff端结构体定义一致
/*******************************************************************************************
* 文件名:mSTc.h
* 编制日期:2018.11.13
* 作者:Kare
* 功能:
*   bk_ctlStatus 定义与BECKHOFF控制器通信的结构体,与在BECKHOFF PLC内的结构体一致
********************************************************************************************/#include "mMacro.h"
#include <vector>using namespace std;
//! 按照单字节对齐,需要与PLC端定义一致。不需要优化。
#pragma pack(push)
#pragma pack(1) //! beckhoff结构体 NCTOPLC_AXLESTRUCT
struct Nc2Plc
{BK_DWORD nStateDWord; //Status double word BK_DWORD nErrorCode; // Axis error code BK_DWORD nAxisState; // Axis moving status BK_DWORD nAxisModeCon; // Axis mode confirmation (feedback from NC) BK_DWORD nCalibrationState; // State of axis calibration (homing) BK_DWORD nCoupleState; // Axis coupling state BK_DWORD nSvbEntries; // SVB entries/orders (SVB = Set preparation task) BK_DWORD nSafEntries; // SAF entries/orders (SAF = Set execution task BK_DWORD nAxisId    ; // Axis ID BK_DWORD nOpModeDWord; // Current operation mode BK_DWORD nReserved2_HIDDEN; // reserved BK_LREAL fPosIst; // Actual position (absolut value from NC) BK_LREAL fModuloPosIst; // Actual position as modulo value (e.g. in degrees) BK_DINT nModuloTurns;   // Actual modulo turns BK_LREAL fVeloIst; // Actual velocity (optional) BK_LREAL fPosDiff; // Position difference (lag distance) BK_LREAL fPosSoll; // Setpoint position BK_LREAL fVeloSoll; // Setpoint velocity BK_LREAL fAccSoll; // Setpoint acceleration, OLD: "fReserve1_HIDDEN" BK_LREAL fReserve2_HIDDEN; // reserved BK_LREAL fReserve3_HIDDEN; // reserved BK_LREAL fReserve4_HIDDEN; // reserved };//! NCTOPLC_AXLESTRUCT nStateDWord变量
struct NCTOPLC_AXLESTRUCT_nStateDWord
{/* NCTOPLC_AXLESTRUCTByte Bit  Def. range Variable Name Description 0 0/1 Operational Axis is ready for operation 1 0/1 Homed Axis has been referenced/ homed ("Axis calibrated") 2 0/1 NotMoving Axis is logically stationary ("Axis not moving") 3 0/1 InPositionArea Axis is in position window (physical feedback) 4 0/1 InTargetPosition Axis is at target position (PEH) (physical feedback) 5 0/1 Protected Axis is in a protected operating mode (e.g. as a slave axis, or under axis interpolation) 6 0/1 ErrorPrewarning NEW: Axis signals an error pre warning (from TC 2.11) 7 0/1 HasBeenStopped Axis has been stopped or is presently executing a stop 8 0/1 HasJob Axis has instructions, is carrying instructions out 9 0/1 PositiveDirection Axis moving to logically larger values 10 0/1 NegativeDirection Axis moving to logically smaller values 11 0/1 HomingBusy Axis referenced ("Axis being calibrated") 12 0/1 ConstantVelocity Axis has reached its constant velocity or rotary speed 13 0/1 Compensating Section compensation passive[0]/active[1] (s. "MC_MoveSuperImposed") 14 0/1 ExtSetPointGenEnabled External setpoint generator enabled 15 0/1   Operating mode not yet executed (Busy). Not implemented yet! 16 0/1 ExternalLatchValid External latch value or sensing switch has become valid 17 0/1 NewTargetPos Axis has a new target position or a new velocity 18 0/1  Axis is not at target position or cannot reach the target position (e.g. stop). Not implemented yet! 19 0/1 ContinuousMotion Axis has target position (±) endless 20 0/1 ControlLoopClosed Axis is ready for operation and axis control loop is closed (e.g. position control) 21 0/1 CamTableQueued CAM table is queued for  "Online Change" and waiting for activation 22 0/1 CamDataQueued CAM data (only MF) are queued for  "Online Change" and waiting for activation 23 0/1 CamScalingPending CAM scaling are queued for  "Online Change" and waiting for activation 24 0/1 CmdBuffered NEW: Following command is queued in then command buffer (s. Buffer Mode)(from TwinCAT V2.10 Build 1311) 25 0/1 PTPmode NEW: Axis in PTP mode (no slave, no NCI axis) (from TC 2.10 Build 1326) 26 0/1 SoftLimitMinExceeded NEW: Position software limit switch minimum is exceeded (from TC 2.10 Build 1327) 27 0/1 SoftLimitMaxExceeded NEW: Position software limit switch maximum is exceeded (from TC 2.10 Build 1327) 28 0/1 DriveDeviceError NEW: Hardware drive device error (no warning), interpretation only possible when drive is data exchanging, e.g. EtherCAT "OP"-state (from TC 2.10 Build 1326) 29 0/1 MotionCommandsLocked NEW: Axis is locked for motion commands (TcMc2) 30 0/1 IoDataInvalid NEW: IO data invalid (e.g. 'WcState' or 'CdlState') 31 0/1 Error Axis is in a fault state */bool Operational; // Axis is ready for operation bool Homed; // Axis has been referenced/ homed ("Axis calibrated") bool NotMoving; // Axis is logically stationary ("Axis not moving") bool InPositionArea; // Axis is in position window (physical feedback) bool InTargetPosition; // Axis is at target position (PEH) (physical feedback) bool Protected; // Axis is in a protected operating mode (e.g. as a slave axis, or under axis interpolation) bool ErrorPrewarning; // NEW: Axis signals an error pre warning (from TC 2.11) bool HasBeenStopped; // Axis has been stopped or is presently executing a stop bool HasJob; // Axis has instructions, is carrying instructions out bool PositiveDirection; // Axis moving to logically larger values bool NegativeDirection; // Axis moving to logically smaller values bool HomingBusy; // Axis referenced ("Axis being calibrated") bool ConstantVelocity; // Axis has reached its constant velocity or rotary speed bool Compensating; // Section compensation passive[0]/active[1] (s. "MC_MoveSuperImposed") bool ExtSetPointGenEnabled; // External setpoint generator enabled // Operating mode not yet executed (Busy). Not implemented yet! bool ExternalLatchValid; // External latch value or sensing switch has become valid bool NewTargetPos; // Axis has a new target position or a new velocity //  18 0/1  Axis is not at target position or cannot reach the target position (e.g. stop). Not implemented yet! bool ContinuousMotion; // Axis has target position (±) endless bool ControlLoopClosed; // Axis is ready for operation and axis control loop is closed (e.g. position control) bool CamTableQueued; // CAM table is queued for  "Online Change" and waiting for activation bool CamDataQueued; // CAM data (only MF) are queued for  "Online Change" and waiting for activation bool CamScalingPending; // CAM scaling are queued for  "Online Change" and waiting for activation bool CmdBuffered; // NEW: Following command is queued in then command buffer (s. Buffer Mode)//(from TwinCAT V2.10 Build 1311) bool PTPmode; // NEW: Axis in PTP mode (no slave, no NCI axis) (from TC 2.10 Build 1326) bool SoftLimitMinExceeded; // NEW: Position software limit switch minimum is exceeded (from TC 2.10 Build 1327) bool SoftLimitMaxExceeded; // NEW: Position software limit switch maximum is exceeded (from TC 2.10 Build 1327) bool DriveDeviceError; // NEW: Hardware drive device error (no warning), interpretation only possible when drive is data exchanging, e.g. EtherCAT "OP"-state (from TC 2.10 Build 1326) bool MotionCommandsLocked; // NEW: Axis is locked for motion commands (TcMc2) bool IoDataInvalid; // NEW: IO data invalid (e.g. 'WcState' or 'CdlState') bool Error; // Axis is in a fault state };#pragma pack(pop) //恢复默认对齐
//#pragma pack() #endif

结构体转换函数

//相关宏定义//定义对应beckhoff数据类型
#define BK_LREAL double //8 bytes
#define BK_USINT unsigned char //1 byte
#define BK_UINT unsigned short int //2 byte
#define BK_INT short int //2byte
#define BK_BOOL unsigned char //1 bit
#define BK_UDINT unsigned  int//4 bytes
#define BK_DINT int //4 bytes
#define BK_DWORD unsigned int //4 bytes//处理函数,将BK_DWORD转换为对应的结构体
//c++的bool是一个字节(我一直以为只占一个内存bit)
NCTOPLC_AXLESTRUCT_nStateDWord mg_uint2NCTOPLC_AXLESTRUCT_nStateDWord( unsigned int fml_val )
{NCTOPLC_AXLESTRUCT_nStateDWord nState;int siz = sizeof(NCTOPLC_AXLESTRUCT_nStateDWord);
//  memcpy(&nState, &fml_val, sizeof(nState));nState.Operational = (fml_val & (0x01)) != 0;nState.Homed = (fml_val & (0x01<<1)) != 0;nState.NotMoving = (fml_val & (0x01<<2)) != 0;nState.InTargetPosition = (fml_val & (0x01<<4)) != 0;nState.HasJob = (fml_val & (0x01<<8)) != 0;nState.Error = (fml_val & (0x01>>31)) != 0;return nState;
}

c++读取倍福PLC中轴状态相关推荐

  1. 上位机使用C++通过ADS协议与倍福PLC通信例程-字符串变量读取

    前言 建议初学者先看这一章节内容,里面包括一些基础的环境配置和项目建立流程,以后开发项目这些流程是通用的,务必掌握并熟练. 链接: 上位机使用C++通过ADS协议与倍福PLC通信例程-布尔变量的读取 ...

  2. 倍福PLC和C#通过ADS通信传输Bool数组变量

    在倍福PLC和C#通信,采用ADS通信,本文讲解C#如何读取和写入bool类型数组变量. 操作流程 1.1. PLC程序设计 首先定义相关的变量,如下所示,激活配置: 1.2. C#程序设计 关于C# ...

  3. 倍福PLC和C#通过ADS通信传输int类型变量

    在倍福PLC和C#通信,采用ADS通信,本文讲解C#如何读取和写入int类型变量. 操作流程 1.1. PLC程序设计 首先定义相关的变量,如下所示,激活配置: 1.2. C#程序设计 关于C#和倍福 ...

  4. 倍福PLC的C++ ADS通讯定义数据类型时注意事项

    在C++程序与倍福PLC通过ADS通讯时,如果C++程序中定义的变量与PLC程序中相应变量定义的数据类型不对应时,可能会出现数据读取或者写入错误,以下为调试过程中容易出错的实践总结记录. 1.向PLC ...

  5. 倍福PLC和C#通过ADS通信传输bool类型变量

    在倍福PLC和C#通信,采用ADS通信,本文讲解C#如何读取和写入bool类型变量. 操作流程 1.1. PLC程序设计 首先定义相关的变量,如下所示,激活配置: 1.2. C#程序设计 关于C#和倍 ...

  6. C# TCP/IP通讯协议的整理(三)附带——与倍福PLC通讯

    首先,需要一个和倍福PLC通讯的dll,一般厂家会提供 添加到引用后,直接创建通讯类 using System; using System.Collections.Generic; using Sys ...

  7. 倍福PLC的Modbus-TCP Server入门教程

    倍福PLC的Modbus-TCP Server入门教程 一.实验目的 二.实验环境 三.需要的软件 四.操作步骤 五.问题 作者:SAM 一.实验目的 1.使用倍福TwinCAT3作为Modbus-T ...

  8. 线扫描枪条码测试机程序,完整倍福PLC程序完整beckhoff倍福PLC ST 语言

    线扫描枪条码测试机程序,完整倍福PLC程序完整beckhoff倍福PLC ST 语言ethercat总 线扫描枪条码测试机程序 和MES系统通讯 和Wenglor 扫描枪 RS232通讯 FB块可以直 ...

  9. 倍福PLC串口模块使用

    之前使用的EL6021模块,如果使用PLC自带的COM口,则ST代码编写方面需要做一些修改: 1.将COMIN和OUT的类型修改 修改为: 2.将功能块的调用Mode修改: 修改为: 此外,COM口设 ...

最新文章

  1. C语言中static全局变量与普通的全局变量区别
  2. .jsp与servlet之间页面跳转及参数传递实例
  3. [USACO1.2]方块转换 Transformations
  4. Objective-C:OC内部可变对象和不可变对象的深(复制)拷贝问题思考:
  5. 最容易进的大厂工作,百度经典百题
  6. 通过Small Basic把儿子/女儿带入编程的世界
  7. Vivado生成bit文件报错彻底解决
  8. 修改Android Studio默认的gradle配置文件
  9. idea打包Jar包
  10. 打开计算机桌面里面有其他图标,Win7电脑桌面上的快捷方式图标都变成相同且打不开...
  11. 【Flutter】Dart 中国身份证验证,出生日期、性别、年龄查询
  12. 性能服务器可以同时标注吗,关于服务器性能的一些思考
  13. CS5218 DP转HDMI 4K30HZ转换方案|DP++to HDMI(4k30)
  14. pdf合并软件下载的旗舰版
  15. 另类办公Word2003也当“扫描大师”(转)
  16. kali2021 JDK配置与安装(独一无二的详细)
  17. 爱普迪供应CS连接器,适用400G传输
  18. OUC软件开发实验4
  19. Android实用代码大全
  20. 股票基本名词,你知道多少?

热门文章

  1. 亲身体验 北京-环球嘉年华!
  2. TypeScript(八)装饰器
  3. proteus——555 PWM 发生器
  4. GBA探索日记(-)
  5. mysql三个分组排序方法row_number()、rank()、dense_rank()
  6. SQL取整与时间差值返回
  7. 直播间与2位优秀创作者分享经历
  8. fatal remote does not appear to be a git repository
  9. python的pyautogui的函数,实现按键精灵
  10. 冷门又实用的Excel技巧知识,还不来看看嘛