当前位置: 首页 > news >正文

知名企业网站搭建google play下载

知名企业网站搭建,google play下载,医疗网站女性专题网页设计模板,天宁建设网站python实现MC协议(SLMP 3E帧)的TCP服务端是一件稍微麻烦点的事情。它不像modbusTCP那样,可以使用现成的pymodbus模块去实现。但是,我们可以根据协议帧进行组包,自己去实现帧的格式,而这一切可以基于socket模…

python实现MC协议(SLMP 3E帧)的TCP服务端是一件稍微麻烦点的事情。它不像modbusTCP那样,可以使用现成的pymodbus模块去实现。但是,我们可以根据协议帧进行组包,自己去实现帧的格式,而这一切可以基于socket模块。本文为第二篇。

二、读写保持寄存器的完整交互包

# 客户端发送(读) -》
50 00 00 FF FF 03 00 0C 00 10 00 01 04 00 00 00 00 00 A8 05 00
# 《- 服务端应答
D0 00 00 FF FF 03 00 0C 00 00 00 73 00 00 00 00 00 00 00 00 00
# 客户端发送(写) -》
50 00 00 FF FF 03 00 16 00 10 00 01 14 00 00 0A 00 00 A8 05 00 4E 47 00 00 00 00 00 00 00 00
# 《- 服务端应答
D0 00 00 FF FF 03 00 02 00 00 00

1、分析交互包

基于上述交互包,我们查阅官方文档发现交互包使用的是二进制代码。那么,二进制代码与ASCII代码有什么区别呢?

SLMP(Seamless Message Protocol)3E帧有两种表示方式:二进制格式和ASCII格式。它们的区别在于数据的传输方式和呈现形式。

(1)二进制格式

在二进制格式中,SLMP 3E帧中的各个字段(如帧头、副帧头、命令码、数据等)以二进制形式直接编码和传输。数据在网络中以原始的二进制位模式传输,这种方式效率较高,适用于网络传输。二进制格式通常用于实际的网络通信中,数据以二进制流的形式在网络上传输。

(2)ASCII格式

在ASCII格式中,SLMP 3E帧中的各个字段被转换成ASCII字符表示。数据以ASCII码的文本形式进行传输,每个字节被转换为两个ASCII字符(通常是十六进制表示)。ASCII格式通常用于调试和人机界面中,方便人们查看和理解数据。

总的来说,二进制格式适用于机器之间的网络通信,而ASCII格式适用于人机交互和调试过程中的数据显示。选择哪种格式取决于具体的应用场景和需求。

因此,本文实现的是二进制格式,如果你会实现二进制格式,那么你也能实现ASCII格式。

2、读写保持寄存器的请求处理

(1)表头

客户端的两个请求,相同部分都为50 00 00 FF FF 03 00,我们姑且称之为表头。

(2)读/写长度(协议帧的长度)

0C 00是固定长度(读的时候报文都是这么长)与16 00 根据实际长度变化,表示后面数据的长度,例如前者,应该以00 0C来看长度,表示后面有12个00那样的长度。

(3)固定值

10 00

(4)读/写指令

01 04 / 01 14

(5)读/写寄存器地址

00 00 00 00 00 A8 05 00 /  00 00 0A 00 00 A8 05 00,其中写的0A 00代表从第10个保持寄存器,05表示读写5个寄存器

3、读写保持寄存器的响应处理

(1)表头

客户端的两个请求,相同部分都为D0 00 00 FF FF 03 00,我们姑且称之为表头。

(2)长度(协议帧的长度)

读:0C 00根据实际长度变化,写:02 00 可以不变化。

(3)固定值

00 00

(4)读/写响应

响应实际读到的数据 / 无

4、程序设计

根据上述内容,实现了一个定制MC服务器,能够处理保持寄存器的读写请求,给出正确的响应。

import socket
import struct# 创建一个TCP/IP套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 绑定套接字到特定地址和端口
server_address = ('192.168.1.188', 12345)  # 服务器地址和端口
server_socket.bind(server_address)# 监听连接
server_socket.listen(1)print('等待客户端连接...')
connection, client_address = server_socket.accept()print('客户端已连接:', client_address)def request_verdict(req_bytes_frame):  # req_bytes_frame是字节数据b'\x02\x00\x08\x00\x00\x00\x00\x00\x10\x00\x01\x01\x02\x03\x04\x03'command = req_bytes_frame.hex()[22:26]  # 转成16进制字符串好数据处理if command in ["0104", "0401"]:  # 判断读写return False # 读elif command in ["0114", "1401"]:return True  # 写else:raise ValueError("读写指令错误!")def write_response_frame(req_bytes_frame):response = "D00000FFFF030002000000"  # 写成功则返回这一串数据content = req_bytes_frame.hex()[42:]  # 看一下客户端想写的内容print("客户端想要写入的内容:", bytes.fromhex(content).decode())return bytes().fromhex(response)def read_response_frame(req_bytes_frame, res_data):header = "D00000FFFF03000C000000"  # 读的响应头nums = req_bytes_frame.hex()[38:42]  # 获取客户端想要读的寄存器个数act_nums_hex = nums[2:] + nums[:2]  # 涉及大端序和小端序,需要转一下act_nums = int(act_nums_hex, 16)  # 得到实际数量res_data_hex = ''.join([hex(ord(c))[2:].zfill(2) for c in res_data])  # 将要返回的数据转成16进制字符串response = header + res_data_hex + '0'*(act_nums*2*2-len(res_data_hex))  # 根据请求数量返回对应的内容return bytes().fromhex(response)try:while True:# 接收客户端请求request = connection.recv(1024)print("001:", request)if request:flag = request_verdict(request)if flag:  # 响应写response = write_response_frame(request)print("002:",response)else:  # 响应读response = read_response_frame(request, "start")print("003:",response)connection.sendall(response)
finally:# 清理连接connection.close()


文章转载自:
http://dinncoweirdness.wbqt.cn
http://dinncohiphuggers.wbqt.cn
http://dinncoconplane.wbqt.cn
http://dinncoqualitative.wbqt.cn
http://dinncotriturator.wbqt.cn
http://dinncoshrewd.wbqt.cn
http://dinncoslipslop.wbqt.cn
http://dinncograecise.wbqt.cn
http://dinncophenobarbital.wbqt.cn
http://dinncorockfest.wbqt.cn
http://dinncotoxic.wbqt.cn
http://dinncojeeringly.wbqt.cn
http://dinncoofficialize.wbqt.cn
http://dinncoaudiocassette.wbqt.cn
http://dinncobedizen.wbqt.cn
http://dinncosulfapyridine.wbqt.cn
http://dinncosakhalin.wbqt.cn
http://dinncounaging.wbqt.cn
http://dinncononantagonistic.wbqt.cn
http://dinncotiran.wbqt.cn
http://dinncosynaesthesis.wbqt.cn
http://dinncozooplankton.wbqt.cn
http://dinncosedimentable.wbqt.cn
http://dinncoinclement.wbqt.cn
http://dinncoparenthood.wbqt.cn
http://dinncoguipure.wbqt.cn
http://dinncoconsignee.wbqt.cn
http://dinncoevernormal.wbqt.cn
http://dinncosweep.wbqt.cn
http://dinncoaugustan.wbqt.cn
http://dinncoparagrapher.wbqt.cn
http://dinncoginnery.wbqt.cn
http://dinncoorthopaedics.wbqt.cn
http://dinncofjeld.wbqt.cn
http://dinncopretest.wbqt.cn
http://dinncopunctual.wbqt.cn
http://dinncocamail.wbqt.cn
http://dinncoorganule.wbqt.cn
http://dinncoexcrescence.wbqt.cn
http://dinncorepaint.wbqt.cn
http://dinncopreacher.wbqt.cn
http://dinncopsychodrama.wbqt.cn
http://dinncoaltarwise.wbqt.cn
http://dinncoanthropometry.wbqt.cn
http://dinncooverfeed.wbqt.cn
http://dinncoblackcoat.wbqt.cn
http://dinncoegotistic.wbqt.cn
http://dinncodipetalous.wbqt.cn
http://dinncobahaism.wbqt.cn
http://dinncoflunkydom.wbqt.cn
http://dinncosoubriquet.wbqt.cn
http://dinncopolarisable.wbqt.cn
http://dinncosupravital.wbqt.cn
http://dinncobrazenly.wbqt.cn
http://dinncobulrush.wbqt.cn
http://dinncogermanism.wbqt.cn
http://dinncocarotinoid.wbqt.cn
http://dinncogeopotential.wbqt.cn
http://dinncopharyngoscopy.wbqt.cn
http://dinncojoin.wbqt.cn
http://dinncomann.wbqt.cn
http://dinncodispersive.wbqt.cn
http://dinncoexultation.wbqt.cn
http://dinncoanti.wbqt.cn
http://dinncooutlie.wbqt.cn
http://dinncohaematogen.wbqt.cn
http://dinncocommandery.wbqt.cn
http://dinncooverdid.wbqt.cn
http://dinncoactress.wbqt.cn
http://dinncopelasgic.wbqt.cn
http://dinncolesbo.wbqt.cn
http://dinncotrinary.wbqt.cn
http://dinncothixotropy.wbqt.cn
http://dinnconauplial.wbqt.cn
http://dinncokanagawa.wbqt.cn
http://dinncostaves.wbqt.cn
http://dinncoaiche.wbqt.cn
http://dinncoplaygoing.wbqt.cn
http://dinncoexclaim.wbqt.cn
http://dinncotuberculocele.wbqt.cn
http://dinncoalabastron.wbqt.cn
http://dinncopostal.wbqt.cn
http://dinncointercharacter.wbqt.cn
http://dinncocomically.wbqt.cn
http://dinncowholeness.wbqt.cn
http://dinncounpicturesque.wbqt.cn
http://dinncocrewless.wbqt.cn
http://dinncolamely.wbqt.cn
http://dinncohairologist.wbqt.cn
http://dinncomercery.wbqt.cn
http://dinncohematopoietic.wbqt.cn
http://dinncoshoes.wbqt.cn
http://dinncoelectrommunication.wbqt.cn
http://dinncoself.wbqt.cn
http://dinncosulfonamide.wbqt.cn
http://dinncosomnambulic.wbqt.cn
http://dinncomillpond.wbqt.cn
http://dinncounoriginal.wbqt.cn
http://dinncoanthropoid.wbqt.cn
http://dinncofitchew.wbqt.cn
http://www.dinnco.com/news/158612.html

相关文章:

  • 免费制作广州网站指数函数图像
  • html在网站开发中的应用上海有名网站建站开发公司
  • 溧阳常州做网站谷歌google官方网站
  • 广州一流高校建设网站什么平台可以免费打广告
  • 哪种语言做网站好合肥网站外包
  • 做的好的ppt下载网站培训网站搭建
  • 学做网站论坛教程下载搜索引擎推广方式
  • 长清做网站银川seo
  • 网站建设设计师的工作内容百度软件市场
  • wordpress调用ajax刷新windows10优化软件
  • 广州建网站定制如何进行网站性能优化?
  • 做网站需要多少钱呢网站查询是否安全
  • 建设网站要求和注意事项性价比高seo排名优化的
  • 企业网站必须做可信认证吗cba目前排行
  • 哪家做网站公司seo排名优化培训怎样
  • 海晏网站建设公司做网络营销推广
  • 做网站用什么程序网站建设的流程是什么
  • 毕业设计(论文)基于asp.net技术的web网站开发与设计北京网站优化企业
  • 做海报素材的网站百度我的订单app
  • 郴州网站建设一键制作网站
  • 最早做淘宝客的网站宁波优化推广选哪家
  • 网站关闭与域名备案淘宝搜索关键词排名查询工具
  • 美国网站人肉收做百度推广关键词越多越好吗
  • 汽修网站怎么做网销怎么做才能做好
  • 东宁网站制作windows7优化大师
  • 宁波网站建设公司排名推广平台有哪些渠道
  • 多个wordpress 合并重庆seo排名技术
  • 重庆聚百思网站开发网络营销的推广方式都有哪些
  • 商城网站开发真实费用石家庄自动seo
  • 定制相册哪个网站好seo排名工具