x64下封装modbus_tcp库,测试可用!!!附带modbus_tk脚本用于测试

  • x6_801717
    了解作者
  • 6.2KB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-29 04:03
    上传日期
1.该测试模块用于测试上位机使用modbus-tcp协议,对PLC寄存器读写测试; 2.x64下封装modbus协议栈,为modbus协议添加了多线程使用安全; 3.modbus类使用单例模式,CommunicationWithPLC通过静态方法获得modbus类实例: CommunicationWithPLC::CommunicationWithPLC() { mb = Modbus::getInstance(); } 4.单例模式的作用是无论上位机中开多少线程,都表现一个端口与plc通讯。 5.README.py用modbus-tk仿真plc行为。
modbus.rar
  • TestPLCInitial
  • README.py
    1.6KB
  • testPLCCommunication.cpp
    3.6KB
  • CommunicationWithPLC.cpp
    7.2KB
  • Modbus.cpp
    4.2KB
  • CommunicationWithPLC.h
    2KB
  • Modbus.h
    1.1KB
内容介绍
#include "stdafx.h" #include "CommunicationWithPLC.h" namespace cis { CommunicationWithPLC::CommunicationWithPLC() { mb = Modbus::getInstance(); } CommunicationWithPLC::~CommunicationWithPLC() { } bool CommunicationWithPLC::modbusConnect() { return mb->modbusConnect(); } void CommunicationWithPLC::modbusClose() { mb->modbusClose(); } void CommunicationWithPLC::floatToUnsignedShort(float f, unsigned short *value) { void *v; v = &f; for (int i = 0; i < 2; i++) value[i] = *((unsigned short*)v + i); } void CommunicationWithPLC::unsignedCharToUnsignedShort(unsigned char c, unsigned short *value) { void *a; unsigned char *b; a = value; b = (unsigned char*)a; //little-endian representation b[1] = 0; b[0] = c; } float CommunicationWithPLC::unsignedShortToFloat(unsigned short *buffer) { void *v; v = buffer; return *((float*)v); } short CommunicationWithPLC::unsignedShortToShort(unsigned short *buffer) { void *v; v = buffer; return *((short*)v); } unsigned char CommunicationWithPLC::unsignedShortToUnsignedChar(unsigned short *buffer) { void *v; v = buffer; return *((unsigned char*)v); } bool CommunicationWithPLC::ConfigureSystem(unsigned char acquisitionMode, float startAngle, float endAngle, unsigned char speed, float targetSourceAxisDistanceA, float targetSourceAxisDistanceB, float targetDetectorOffsetXA, float targetDetectorOffsetXB, float step) { unsigned short toSend[17]; unsignedCharToUnsignedShort(acquisitionMode, toSend); floatToUnsignedShort(startAngle, toSend + 1); floatToUnsignedShort(endAngle, toSend + 3); unsignedCharToUnsignedShort(speed, toSend + 5); floatToUnsignedShort(targetSourceAxisDistanceA, toSend + 7); floatToUnsignedShort(targetSourceAxisDistanceB, toSend + 9); floatToUnsignedShort(targetDetectorOffsetXA, toSend + 11); floatToUnsignedShort(targetDetectorOffsetXB, toSend + 13); floatToUnsignedShort(step, toSend + 15); if (!mb->getTCPStatus()) { bool tmp; tmp = mb->modbusConnect(); if (!tmp) return false; } mb->modbusWriteRegisters(12305, 17, toSend); return true; } struct CurrentSystemStatus CommunicationWithPLC::GetCurrentSystemStatus() { unsigned short HoldingRegs[17]; unsigned char c1, c2; float m_CurrentGantryAngle = 0; float m_GantryAngleSync = 0; unsigned short m_XrayExposureCountA = 0; unsigned short m_XrayExposureCountB = 0; unsigned char CurrentPLCStatus = 0; /* * SystemUnconfigured 0 * SystemConfigured 1 * SystemInPosition 2 * SystemImaging 3 * SystemHalt 4 */ float m_CurrentSourceAxisDistenceA = 0; float m_CurrentSourceAxisDistenceB = 0; float m_CurrentDetectorOffsetXA = 0; float m_CurrentDetectorOffsetXB = 0; bool m_IsSystemInitialized = FALSE; bool m_IsSystemConfigured = FALSE; bool m_IsHVAXrayOnState = FALSE; bool m_IsHVBXrayOnState = FALSE; bool m_IsHVAXrayRunState = FALSE; bool m_IsHVBXrayRunState = FALSE; bool m_IsGantryMoving = FALSE; bool m_IsEmergencyStop = FALSE; bool m_IsShieldDoorClosed = FALSE; bool m_IsMotorOverLoad = FALSE; bool m_IsOutOfMotionRange = FALSE; bool m_IsImagingSyncError = FALSE; CurrentSystemStatus CSS = { m_CurrentGantryAngle, m_GantryAngleSync, m_XrayExposureCountA, m_XrayExposureCountB, CurrentPLCStatus, m_CurrentSourceAxisDistenceA, m_CurrentSourceAxisDistenceB, m_CurrentDetectorOffsetXA, m_CurrentDetectorOffsetXB, m_IsSystemInitialized, m_IsSystemConfigured, m_IsHVAXrayOnState, m_IsHVBXrayOnState, m_IsHVAXrayRunState, m_IsHVBXrayRunState, m_IsGantryMoving, m_IsEmergencyStop, m_IsShieldDoorClosed, m_IsMotorOverLoad, m_IsOutOfMotionRange, m_IsImagingSyncError }; if (!mb->getTCPStatus()) { bool tmp; tmp = mb->modbusConnect(); if (!tmp) return CSS; } mb->modbusReadHoldingRegisters(12288, 17, HoldingRegs); m_CurrentGantryAngle = unsignedShortToFloat(HoldingRegs); m_GantryAngleSync = unsignedShortToFloat(HoldingRegs + 2); m_XrayExposureCountA = unsignedShortToShort(HoldingRegs + 4); m_XrayExposureCountB = unsignedShortToShort(HoldingRegs + 5); CurrentPLCStatus = unsignedShortToUnsignedChar(HoldingRegs + 6); m_CurrentSourceAxisDistenceA = unsignedShortToFloat(HoldingRegs + 8); m_CurrentSourceAxisDistenceB = unsignedShortToFloat(HoldingRegs + 10); m_CurrentDetectorOffsetXA = unsignedShortToFloat(HoldingRegs + 12); m_CurrentDetectorOffsetXB = unsignedShortToFloat(HoldingRegs + 14); //little-endian representation c1 = (unsigned char)HoldingRegs[16]; c2 = (unsigned char)HoldingRegs[16] >> 8 ; m_IsSystemInitialized = (c1 | 0xFE) == 0xFF; m_IsSystemConfigured = (c1 | 0xFD) == 0xFF; m_IsHVAXrayOnState = (c1 | 0xFB) == 0xFF; m_IsHVBXrayOnState = (c1 | 0xF7) == 0xFF; m_IsHVAXrayRunState = (c1 | 0xEF) == 0xFF; m_IsHVBXrayRunState = (c1 | 0xDF) == 0xFF; m_IsGantryMoving = (c1 | 0xBF) == 0xFF; m_IsEmergencyStop = (c1 | 0x7F) == 0xFF; m_IsShieldDoorClosed = (c2 | 0xFE) == 0xFF; m_IsMotorOverLoad = (c2 | 0xFD) == 0xFF; m_IsOutOfMotionRange = (c2 | 0xFB) == 0xFF; m_IsImagingSyncError = (c2 | 0xF7) == 0xFF; CSS = { m_CurrentGantryAngle, m_GantryAngleSync, m_XrayExposureCountA, m_XrayExposureCountB, CurrentPLCStatus, m_CurrentSourceAxisDistenceA, m_CurrentSourceAxisDistenceB, m_CurrentDetectorOffsetXA, m_CurrentDetectorOffsetXB, m_IsSystemInitialized, m_IsSystemConfigured, m_IsHVAXrayOnState, m_IsHVBXrayOnState, m_IsHVAXrayRunState, m_IsHVBXrayRunState, m_IsGantryMoving, m_IsEmergencyStop, m_IsShieldDoorClosed, m_IsMotorOverLoad, m_IsOutOfMotionRange, m_IsImagingSyncError }; return CSS; } bool CommunicationWithPLC::SystemConfigured() { unsigned short toSend[2]; unsigned char c = 1; unsignedCharToUnsignedShort(c,toSend); if (!mb->getTCPStatus()) { bool tmp; tmp = mb->modbusConnect(); if (!tmp) return false; } mb->modbusWriteRegisters(12294, 1, toSend); return true; } bool CommunicationWithPLC::SystemUnconfigured() { unsigned short toSend[2]; unsigned char c = 0; unsignedCharToUnsignedShort(c, toSend); if (!mb->getTCPStatus()) { bool tmp; tmp = mb->modbusConnect(); if (!tmp) return false; } mb->modbusWriteRegisters(12294, 1, toSend); return true; } bool CommunicationWithPLC::SystemInPosition() { unsigned short toSend[2]; unsigned char c = 2; unsignedCharToUnsignedShort(c, toSend); if (!mb->getTCPStatus()) { bool tmp; tmp = mb->modbusConnect(); if (!tmp) return false; } mb->modbusWriteRegisters(12294, 1, toSend); return true; } bool CommunicationWithPLC::SetEndAngle(float endAngle) { unsigned short toSend[4]; floatToUnsignedShort(endAngle, toSend); if (!mb->getTCPStatus()) { bool tmp; tmp = mb->modbusConnect(); if (!tmp) return false; } mb->modbusWriteRegisters(12308, 2, toSend); } bool CommunicationWithPLC::getTCPStatus() { return mb->getTCPStatus(); } }//namespace CIS
评论
    相关推荐
    • 单例模式
      NULL 博文链接:https://andydufresneone.iteye.com/blog/1156641
    • 单例模式
      NULL 博文链接:https://songjianyong.iteye.com/blog/1970689
    • 单例模式33
      是一种“思想”,就像做菜,一开始学习的人难免会问"我现在学做了几道菜?",其实,真正的精髓不在菜品,而在如何调味,如何选料,备料等"最基本的原理和法则",掌握并熟练应运了这些基本思想,才能真正的完成从"无招...
    • 单例模式 工厂模式DEMO
      1、单例模式 结论:单例模式可以把数据封装在对象里,操作对象;而静态类是把数据封装在类里,操作类。单例模式中,数据只有在对象存在的时候才能操作;而静态类中,数据可以直接通过静态类点出来操作。 2、工厂模式...
    • 单例模式
      NULL 博文链接:https://terryjs.iteye.com/blog/716036
    • 单例模式
      这个是OC的单例模式的源代码课件
    • 单例模式demo
      饿汉式单例模式,挺简单的一个demo,希望可以帮助大家更好的理解
    • winfrom单例模式
      show出窗体的时候防止重复点击按钮填出多个窗体,此例为一个demo,可调试运行,用户可举一反三使用
    • 单例模式代码
      最近空闲的时候回顾了一下常用的设计模式,其中单例模式是创建型模式中比较基础的一种设计模式,说起单例模式,想必大家并不模式,我们都知道的是,在单例模式下,能够保证一个类只有一个实例对象,就是说,外部访问...
    • 单例模式
      NULL 博文链接:https://ylxy3058.iteye.com/blog/2225863