打印

正在学Python小试牛刀访问modbus从站点

[复制链接]
2184|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
McuPlayer|  楼主 | 2018-9-24 23:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#!/usr/bin/env python3
#-*- coding:utf-8 -*-

from socket import *

HOST ='192.168.100.21'
PORT = 502                  # The default port for modbus-TCP
ADDR = (HOST,PORT)

#           transactionID   Protocol    DatSize     Addr    CMD     paramter
cmd = bytes([ 0x12, 0x34,   0, 0,       0, 6,       0x09,   0x03,   0, 0, 0, 2 ]);


tcp = socket(AF_INET,SOCK_STREAM)       # create a Socket
tcp.connect(ADDR)                       # connect to Server
tcp.send(cmd)                           # Send a command-frame
ack = tcp.recv(13)                      # receive a answer-frame
tcp.close()                             # close the tcp-connection



def trans(s):
    return " ".join('0x%.2x' % x for x in s)

print( "\n" )
print( "Send: ", trans(cmd) )     # cmd to station
print( "Recv: ", trans(ack) )     # ack from station

print( "\n" )
print( "CMD->Transaction ID:%d" % (cmd[0]*256+cmd[1]) )
print( "CMD->Protocol    ID:%d" % (cmd[2]*256+cmd[3]) )
print( "CMD->Data      Size:%d" % (cmd[4]*256+cmd[5]) )
print( "CMD->Device    Addr:%d" % (cmd[6]) )
print( "CMD->Operation  CMD:%d" % (cmd[7]) )
print( "CMD->Register  Addr:%d" % (cmd[8]*256+cmd[9]) )
print( "CMD->Register Count:%d" % (cmd[10]*256+cmd[11]) )
print( "\n" )
print( "ACK->Transaction ID:%d" % (ack[0]*256+ack[1]) )
print( "ACK->Protocol    ID:%d" % (ack[2]*256+ack[3]) )
print( "ACK->Data      Size:%d" % (ack[4]*256+ack[5]) )
print( "ACK->Device    Addr:%d" % (ack[6]) )
print( "ACK->Operation  CMD:%d" % (ack[7]) )
print( "ACK->Raw Data  Size:%d" % (ack[8]) )
print( "ACK->Register[0000]:%d" % (ack[9]*256+ack[10]) )
print( "ACK->Register[0001]:%d" % (ack[11]*256+ack[12]) )


因为打算给客户演示用,所以有一大半代码是Print
Python的语法初步了解了,做点小工具还是挺方便的

相关帖子

沙发
McuPlayer|  楼主 | 2018-9-25 10:58 | 只看该作者
var net = require('net');

var HOST = '127.0.0.1';
var PORT = 502;

//          transactionID   Protocol    DatSize     Addr  CMD-ReadReg   CMD-Reg-Addr  CMD-Reg-Count
const cmd = Buffer.from([ 0x55, 0xAA,   0, 0,       0, 6,       0x09,   0x03,       0x13, 0x94,      0, 2 ]);

var tcp = new net.Socket();
tcp.connect(PORT, HOST, function() {
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});


tcp.on('data', function(ack){
        var sLine = "Recv: ";
        for(var i=0;i<ack.length;i++)
                sLine += ack[i].toString(16) + ' ';
       
        console.log( sLine );

//        console.log( '\n' )
        console.log( "CMD->Transaction ID:" + (cmd[0]*256+cmd[1]) )
        console.log( "CMD->Protocol    ID:" + (cmd[2]*256+cmd[3]) )
        console.log( "CMD->Data      Size:" + (cmd[4]*256+cmd[5]) )
        console.log( "CMD->Device    Addr:" + (cmd[6]) )
        console.log( "CMD->Operation  CMD:" + (cmd[7]) )
        console.log( "CMD->Register  Addr:" + (cmd[8]*256+cmd[9]) )
        console.log( "CMD->Register Count:" + (cmd[10]*256+cmd[11]) )
//        console.log( "\n" )
        console.log( "ACK->Transaction ID:" + (ack[0]*256+ack[1]) )
        console.log( "ACK->Protocol    ID:" + (ack[2]*256+ack[3]) )
        console.log( "ACK->Data      Size:" + (ack[4]*256+ack[5]) )
        console.log( "ACK->Device    Addr:" + (ack[6]) )
        console.log( "ACK->Operation  CMD:" + (ack[7]) )
        console.log( "ACK->Raw Data  Size:" + (ack[8]) )
        console.log( "ACK->Register[" + (cmd[8]*256+cmd[9]+0) + "]:" + (ack[9]*256+ack[10]) )
        console.log( "ACK->Register[" + (cmd[8]*256+cmd[9]+1) + "]:" + (ack[11]*256+ack[12]) )

});

tcp.on('close', function() {
    console.log('Connection closed');
});

var nCnt = 0;

setInterval(function(){
        console.log( ">>>>---------------------------------------->>>" )

        var Tim = new Date();
        console.log( Tim.getFullYear() + '.' + Tim.getMonth() + '.' + Tim.getDate() + '-' + Tim.getHours() + ':' + Tim.getMinutes() + ':' + Tim.getSeconds() )


        tcp.write(cmd);

        var sLine = "Send: ";
        for(var i=0;i<cmd.length;i++)
                sLine += cmd[i].toString(16) + ' ';
        console.log( sLine );

        if(nCnt>10)
                client.destroy();
        nCnt++;

},3000);



再贴一个js版本的,浏览器只支持websocket不支持纯Socket,需要有nodejs环境下Run

使用特权

评论回复
板凳
gaoyang9992006| | 2018-10-16 16:30 | 只看该作者
楼主加油了。还有做WIFI模块演示用Lua也非常给力,就是NodeMCU固件。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

338

主题

7307

帖子

26

粉丝