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

企业网站外包建设网站开发与spark

企业网站外包建设,网站开发与spark,天蝎网站推广优化,开网站购买的服务器放自己家还是放别人那里使用我前面几篇博文的内容,能够使用Python编写一个最简单的OPC UA /ModbusTCP网关。 从这个程序可以看出: 应用OPC UA 并不难,现在我们就可以应用到工程应用中,甚至DIY项目也可以。不必采用复杂的工具软件。使用Python 来构建工…

        使用我前面几篇博文的内容,能够使用Python编写一个最简单的OPC UA /ModbusTCP网关。

从这个程序可以看出:

  1. 应用OPC UA 并不难,现在我们就可以应用到工程应用中,甚至DIY项目也可以。不必采用复杂的工具软件。
  2. 使用Python 来构建工业自动化领域的原型机程序是不错的选择。

OPCUA_modbus 网关

import sys
sys.path.insert(0, "..")
import time
from opcua import  Server
from pyModbusTCP.client import ModbusClient # Modbus TCP Client
from pyModbusTCP import utils 
if __name__ == "__main__":# setup our serverserver = Server()server.set_endpoint("opc.tcp://127.0.0.1:48400/freeopcua/server/")# setup our own namespace, not really necessary but should as specuri = "http://examples.freeopcua.github.io"idx = server.register_namespace(uri)# get Objects node, this is where we should put our nodesobjects = server.get_objects_node()# populating our address spacemyobj = objects.add_object(idx, "MyObject")myvar = myobj.add_variable(idx, "MyVariable", 6.7)myvar.set_writable()    # Set MyVariable to be writable by clientsModbusInterface = ModbusClient(host="localhost", port=502, unit_id=1, auto_open=True, auto_close=False) # starting!server.start()try:count = 0while True:time.sleep(1)reg_l=ModbusInterface.read_input_registers(0,2)val=utils.word_list_to_long(reg_l)print(utils.decode_ieee(val[0],False)) myvar.set_value(utils.decode_ieee(val[0],False))finally:#close connection, remove subcsriptions, etcserver.stop()

modbusTCP服务器程序

import argparse
from pyModbusTCP.server import ModbusServer, DataBank
from pyModbusTCP import utils
from datetime import datetimeimport numpy as np 
Fs = 8000
f = 50
x=0
coil_state=True 
class MyDataBank(DataBank):"""A custom ModbusServerDataBank for override get_holding_registers method."""def __init__(self):# turn off allocation of memory for standard modbus object types# only "holding registers" space will be replaced by dynamic build values.super().__init__(virtual_mode=True)def get_coils(self, address, number=1, srv_info=None):global coil_statecoil_state=not coil_statereturn coil_statedef get_holding_registers(self, address, number=1, srv_info=None):"""Get virtual holding registers."""# populate virtual registers dict with current datetime valuesnow = datetime.now()return now.seconddef get_input_registers(self, address, number=1, srv_info=None):global xwave=np.sin(2 * np.pi * f * x / Fs)*10x=x+1b32_l=[utils.encode_ieee(wave,False)]b16_l = utils.long_list_to_word(b32_l)print(b16_l)              return  b16_lif __name__ == '__main__':# parse argsparser = argparse.ArgumentParser()parser.add_argument('-H', '--host', type=str, default='localhost', help='Host (default: localhost)')parser.add_argument('-p', '--port', type=int, default=502, help='TCP port (default: 502)')args = parser.parse_args()# init modbus server and start itserver = ModbusServer(host=args.host, port=args.port, data_bank=MyDataBank())server.start()

        最后透过uaExpert 程序访问OPCUA Server。可以看到myVar 变量在变化。

 OPCUA 客户端 

import sys
sys.path.insert(0, "..")
import numpy as np 
import matplotlib.pyplot as plt
from opcua import Clientx = np.arange(0,1000,1,dtype=np.int16)
y=np.arange(-10,10,0.02,dtype=np.float32)
if __name__ == "__main__":client = Client("opc.tcp://localhost:48400/freeopcua/server/")# client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a usertry:client.connect()# Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objectsroot = client.get_root_node()print("Objects node is: ", root)# Node objects have methods to read and write node attributes as well as browse or populate address spaceprint("Children of root are: ", root.get_children())while True:myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])obj = root.get_child(["0:Objects", "2:MyObject"])print("myvar is: ", myvar.get_value())y=np.append(y,myvar.get_value())y=np.delete(y, 0, axis=0)      plt.clf()plt.plot(x, y, ls="-", lw=2, label="plot figure")plt.legend()plt.show()plt.pause(0.1)# Stacked myvar access# print("myvar is: ", root.get_children()[0].get_children()[1].get_variables()[0].get_value())finally:client.disconnect()

看出来了吧这些程序都短小精悍。编写程序是学习计算机网络协议最好的方法。

http://www.yayakq.cn/news/719110/

相关文章:

  • 中国做的最好的网站建设劳务公司注册条件
  • 建设电影网站算盗版吗家装公司报价
  • 台州那家网站做的好河北省邢台市建设工程网站
  • 网站设计内容清单电子工程网mcu
  • 网站建设技术外包公益永久免费主机
  • 韶关市建设工程造价网站企业网站内页设计模板
  • 东莞网站设计建设公司做网站开票是多少个点的票
  • 惠安县住房和城乡建设局网站wordpress批量导入tag
  • 蓝色网站建设大连网站建
  • 个人网站注册流程广州市地铁最新消息
  • 网址和网站的区别网站域名跟谁买
  • 汕头网站推广美术教育机构网站建设方案
  • 网站标签怎么做跳转页面西安企业建站在哪里做
  • 网站建设开发有什么好处做3d图的网站
  • 怎么用云主机做网站长沙企业网站开发微联讯点
  • 黄石下陆区建设局网站用.net编写网站
  • 软件开发公司网站诊断及优化方案
  • 减肥产品网站模板网站硬件需求
  • 什么网站做简历免费网页设计心得体会正文
  • 多语言网站东森推广官网
  • 企业网站优化方案范本两学一做网站专栏怎么设置
  • 做电子请帖网站有哪些python编程网站推荐
  • 徐州网站建设找哪家二次开发接口
  • 手机wap网站源码网络推广的平台
  • 企业微信网站怎么建设空间租用 网站开发
  • 国家城乡与建设部网站杭州职业能力建设网
  • 做网站就业要会什么网站备案有效期
  • 网站开发 面试做外国网站百度搜到
  • 做视频up主视频网站南阳网网站建设
  • 织梦网站源码下载上海网网站建设