增加了消息读取的功能,并且添加了python测试脚本

This commit is contained in:
zjk
2024-11-14 18:22:58 +08:00
parent e2bfa98754
commit 590662d7a5
13 changed files with 622 additions and 212 deletions
+44
View File
@@ -0,0 +1,44 @@
import socket
import time
import struct
from udpmsg import msg_CcuStateFbMsg
from udpmsg import msg_CcuSetParamFbMsg
# 目标IP地址和端口
target_ip = '127.0.0.1'
target_port = 5000
mTest = msg_CcuStateFbMsg()
mTest2 = msg_CcuSetParamFbMsg()
valid_msg = mTest.pack()
valid_msg2 = mTest2.pack()
print(valid_msg)
# 创建UDP套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 要发送的信息
# message = "Hello, this is a test message."
message = valid_msg
# 发送信息的次数
num_messages = 10
# 发送信息的时间间隔(秒)
interval = 1
for i in range(num_messages):
# 发送信息
sock.sendto(message, (target_ip, target_port))
# 打印发送的信息和目标地址
print(f"Sent message to {target_ip}:{target_port}: {message}")
# 等待一段时间
time.sleep(interval)
sock.sendto(valid_msg2, (target_ip, target_port))
time.sleep(interval)
# 关闭套接字
sock.close()
+224
View File
@@ -0,0 +1,224 @@
import struct
class msg_CcuStateFbMsg:
def __init__(self):
self.start1 = 0x20
self.start2 = 0x20
self.id = 0x0051
self.length = 0x5A
self.currentWorkCondition = 0x01
self.fcuState = 0x01
self.fcuFaultLeve = 0x01
self.powerLimit = 0x01
self.totalPowerGeneration = 0x01
self.ch4o = 0x01
self.o2 = 0x01
self.fcuVoltage = 0x01
self.fcuCurrent = 0x01
self.o2Level = 0x01
self.placeholder1 = 0x01
self.exchangerTemperature = 0x01
self.fcuOutPower = 0x01
self.fcuOutIns = 0x01
self.fcuVoltageIns = 0x01
self.fcuVoltagePower = 0x01
self.fcuCurrentIns = 0x01
self.fcuCurrentPower = 0x01
self.h2Concentration1 = 0x01
self.h2Concentration2 = 0x01
self.o2Concentration1 = 0x01
self.o2Concentration2 = 0x01
self.ch4oConcentration1 = 0x01
self.ch4oConcentration2 = 0x01
self.backpressure1 = 0x01
self.backpressure2 = 0x01
self.flameDetection = 0x01
self.fcuGZ_1L = 0x01
self.fcuGZ_1H = 0x01
self.fcuGZ_2L = 0x01
self.fcuGZ_2H = 0x01
self.fcuGZ_3L = 0x01
self.fcuGZ_3H = 0x01
self.insPosRelayState = 0x01
self.insNegRelayState = 0x01
self.powerPosRelayState = 0x01
self.powerNegRelayState = 0x01
self.insBatLimit = 0x01
self.powerBatLimit = 0x01
self.insBatSoc = 0x01
self.powerBatSoc = 0x01
self.insBatTotalElectricity = 0x01
self.powerBatTotalElectricity = 0x01
self.insBatCurrentElectricity = 0x01
self.powerBatCurrentElectricity = 0x01
self.insChargeState = 0x01
self.powerChargeState = 0x01
self.insLinkVoltage = 0x01
self.insPackVoltage = 0x01
self.insCurrent = 0x01
self.insPosInsulationResistance = 0x01
self.insNegInsulationResistance = 0x01
self.powerLinkVoltage = 0x01
self.powerPackVoltage = 0x01
self.powerCurrent = 0x01
self.powerPosInsulationResistance = 0x01
self.powerNegInsulationResistance = 0x01
self.insEmergencyStatusWord = 0x01
self.powerEmergencyStatusWord = 0x01
self.insAlarmIdentificationWord = 0x01
self.powerAlarmIdentificationWord = 0x01
self.insCurrentSocHold1 = 0x01
self.insCurrentSocHold2 = 0x01
self.insCurrentSocHold3 = 0x01
self.fcuCurrentToInsLimit1 = 0x01
self.fcuCurrentToinsLimit2 = 0x01
self.powerCurrentSocHold1 = 0x01
self.powerCurrentSocHold2 = 0x01
self.powerCurrentSocHold3 = 0x01
self.powerCurrentToInsLimit1 = 0x01
self.powerCurrentToinsLimit2 = 0x01
self.placeholder2 = 0x01
self.placeholder3 = 0x01
self.placeholder4 = 0x01
self.placeholder5 = 0x01
self.checkCode = 0x01
def pack(self):
# 初始化数据
self.length = 0x0000
self.checkCode = self.start1 + self.start2 + self.id + self.length
+ self.currentWorkCondition + self.fcuState + self.fcuFaultLeve
+ self.powerLimit + self.totalPowerGeneration + self.ch4o + self.o2
+ self.fcuVoltage + self.fcuCurrent + self.o2Level + self.placeholder1
+ self.exchangerTemperature + self.fcuOutPower + self.fcuOutIns + self.fcuVoltageIns
+ self.fcuVoltagePower + self.fcuCurrentIns + self.fcuCurrentPower + self.h2Concentration1
+ self.h2Concentration2 + self.o2Concentration1 + self.o2Concentration2 + self.ch4oConcentration1
+ self.ch4oConcentration2 + self.backpressure1 + self.backpressure2 + self.flameDetection + self.fcuGZ_1L
+ self.fcuGZ_1H + self.fcuGZ_2L + self.fcuGZ_2H + self.fcuGZ_3L + self.fcuGZ_3H + self.insPosRelayState
+ self.insNegRelayState + self.powerPosRelayState + self.powerNegRelayState + self.insBatLimit
+ self.powerBatLimit + self.insBatSoc + self.powerBatSoc + self.insBatTotalElectricity
+ self.powerBatTotalElectricity + self.insBatCurrentElectricity + self.powerBatCurrentElectricity
+ self.insChargeState + self.powerChargeState + self.insLinkVoltage + self.insPackVoltage
+ self.insCurrent + self.insPosInsulationResistance + self.insNegInsulationResistance
+ self.powerLinkVoltage + self.powerPackVoltage + self.powerCurrent + self.powerPosInsulationResistance
+ self.powerNegInsulationResistance + self.insEmergencyStatusWord + self.powerEmergencyStatusWord
+ self.insAlarmIdentificationWord + self.powerAlarmIdentificationWord + self.insCurrentSocHold1 + self.insCurrentSocHold2
+ self.insCurrentSocHold3 + self.fcuCurrentToInsLimit1 + self.fcuCurrentToinsLimit2 + self.powerCurrentSocHold1
+ self.powerCurrentSocHold2 + self.powerCurrentSocHold3 + self.powerCurrentToInsLimit1 + self.powerCurrentToinsLimit2
+ self.placeholder2 + self.placeholder3 + self.placeholder4 + self.placeholder5
data = struct.pack('<B', self.start1)
data += struct.pack('<B', self.start2)
data += struct.pack('<H', self.id)
data += struct.pack('<H', self.length)
# 填充数据
data += struct.pack('<B', self.currentWorkCondition)
data += struct.pack('<B', self.fcuState)
data += struct.pack('<B', self.fcuFaultLeve)
data += struct.pack('<I', self.powerLimit)
data += struct.pack('<I', self.totalPowerGeneration)
data += struct.pack('<I', self.ch4o)
data += struct.pack('<I', self.o2)
data += struct.pack('<I', self.fcuVoltage)
data += struct.pack('<I', self.fcuCurrent)
data += struct.pack('<I', self.o2Level)
data += struct.pack('<I', self.placeholder1)
data += struct.pack('<I', self.exchangerTemperature)
data += struct.pack('<I', self.fcuOutPower)
data += struct.pack('<I', self.fcuOutIns)
data += struct.pack('<I', self.fcuVoltageIns)
data += struct.pack('<I', self.fcuVoltagePower)
data += struct.pack('<I', self.fcuCurrentIns)
data += struct.pack('<I', self.fcuCurrentPower)
data += struct.pack('<I', self.h2Concentration1)
data += struct.pack('<I', self.h2Concentration2)
data += struct.pack('<I', self.o2Concentration1)
data += struct.pack('<I', self.o2Concentration2)
data += struct.pack('<I', self.ch4oConcentration1)
data += struct.pack('<I', self.ch4oConcentration2)
data += struct.pack('<I', self.backpressure1)
data += struct.pack('<I', self.backpressure2)
data += struct.pack('<B', self.flameDetection)
data += struct.pack('<B', self.fcuGZ_1L)
data += struct.pack('<B', self.fcuGZ_1H)
data += struct.pack('<B', self.fcuGZ_2L)
data += struct.pack('<B', self.fcuGZ_2H)
data += struct.pack('<B', self.fcuGZ_3L)
data += struct.pack('<B', self.fcuGZ_3H)
data += struct.pack('<B', self.insPosRelayState)
data += struct.pack('<B', self.insNegRelayState)
data += struct.pack('<B', self.powerPosRelayState)
data += struct.pack('<B', self.powerNegRelayState)
data += struct.pack('<H', self.insBatLimit)
data += struct.pack('<H', self.powerBatLimit)
data += struct.pack('<B', self.insBatSoc)
data += struct.pack('<B', self.powerBatSoc)
data += struct.pack('<H', self.insBatTotalElectricity)
data += struct.pack('<H', self.powerBatTotalElectricity)
data += struct.pack('<H', self.insBatCurrentElectricity)
data += struct.pack('<H', self.powerBatCurrentElectricity)
data += struct.pack('<B', self.insChargeState)
data += struct.pack('<B', self.powerChargeState)
data += struct.pack('<H', self.insLinkVoltage)
data += struct.pack('<H', self.insPackVoltage)
data += struct.pack('<H', self.insCurrent)
data += struct.pack('<H', self.insPosInsulationResistance)
data += struct.pack('<H', self.insNegInsulationResistance)
data += struct.pack('<H', self.powerLinkVoltage)
data += struct.pack('<H', self.powerPackVoltage)
data += struct.pack('<H', self.powerCurrent)
data += struct.pack('<H', self.powerPosInsulationResistance)
data += struct.pack('<H', self.powerNegInsulationResistance)
data += struct.pack('<B', self.insEmergencyStatusWord)
data += struct.pack('<B', self.powerEmergencyStatusWord)
data += struct.pack('<B', self.insAlarmIdentificationWord)
data += struct.pack('<B', self.powerAlarmIdentificationWord)
data += struct.pack('<B', self.insCurrentSocHold1)
data += struct.pack('<B', self.insCurrentSocHold2)
data += struct.pack('<B', self.insCurrentSocHold3)
data += struct.pack('<B', self.fcuCurrentToInsLimit1)
data += struct.pack('<B', self.fcuCurrentToinsLimit2)
data += struct.pack('<B', self.powerCurrentSocHold1)
data += struct.pack('<B', self.powerCurrentSocHold2)
data += struct.pack('<B', self.powerCurrentSocHold3)
data += struct.pack('<B', self.powerCurrentToInsLimit1)
data += struct.pack('<B', self.powerCurrentToinsLimit2)
data += struct.pack('<I', self.placeholder2)
data += struct.pack('<I', self.placeholder3)
data += struct.pack('<I', self.placeholder4)
data += struct.pack('<I', self.placeholder5)
data += struct.pack('<H', self.checkCode)
return data
import struct
class msg_CcuSetParamFbMsg:
def __init__(self):
self.start1 = 0x20
self.start2 = 0x20
self.id = 0x0040
self.length = 10
self.flag = 0x01
self.failvreCode = 0x01
self.checkCode = 0x01 # 初始值,将在 pack 方法中更新
def pack(self):
# 初始化数据
self.checkCode = self.start1 + self.start2 + self.id + self.length + self.flag + self.failvreCode
data = struct.pack('<B', self.start1)
data += struct.pack('<B', self.start2)
data += struct.pack('<H', self.id)
data += struct.pack('<H', self.length)
# 填充数据
data += struct.pack('<B', self.flag)
data += struct.pack('<B', self.failvreCode)
# 其他参数...
data += struct.pack('<H', self.checkCode)
return data