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

[复制链接]
2381|2
 楼主| McuPlayer 发表于 2018-9-24 23:55 | 显示全部楼层 |阅读模式
  1. #!/usr/bin/env python3
  2. #-*- coding:utf-8 -*-

  3. from socket import *

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

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


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



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

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

  19. print( "\n" )
  20. print( "CMD->Transaction ID:%d" % (cmd[0]*256+cmd[1]) )
  21. print( "CMD->Protocol    ID:%d" % (cmd[2]*256+cmd[3]) )
  22. print( "CMD->Data      Size:%d" % (cmd[4]*256+cmd[5]) )
  23. print( "CMD->Device    Addr:%d" % (cmd[6]) )
  24. print( "CMD->Operation  CMD:%d" % (cmd[7]) )
  25. print( "CMD->Register  Addr:%d" % (cmd[8]*256+cmd[9]) )
  26. print( "CMD->Register Count:%d" % (cmd[10]*256+cmd[11]) )
  27. print( "\n" )
  28. print( "ACK->Transaction ID:%d" % (ack[0]*256+ack[1]) )
  29. print( "ACK->Protocol    ID:%d" % (ack[2]*256+ack[3]) )
  30. print( "ACK->Data      Size:%d" % (ack[4]*256+ack[5]) )
  31. print( "ACK->Device    Addr:%d" % (ack[6]) )
  32. print( "ACK->Operation  CMD:%d" % (ack[7]) )
  33. print( "ACK->Raw Data  Size:%d" % (ack[8]) )
  34. print( "ACK->Register[0000]:%d" % (ack[9]*256+ack[10]) )
  35. print( "ACK->Register[0001]:%d" % (ack[11]*256+ack[12]) )


因为打算给客户演示用,所以有一大半代码是Print
Python的语法初步了解了,做点小工具还是挺方便的
 楼主| McuPlayer 发表于 2018-9-25 10:58 | 显示全部楼层
  1. var net = require('net');

  2. var HOST = '127.0.0.1';
  3. var PORT = 502;

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

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


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

  16. //        console.log( '\n' )
  17.         console.log( "CMD->Transaction ID:" + (cmd[0]*256+cmd[1]) )
  18.         console.log( "CMD->Protocol    ID:" + (cmd[2]*256+cmd[3]) )
  19.         console.log( "CMD->Data      Size:" + (cmd[4]*256+cmd[5]) )
  20.         console.log( "CMD->Device    Addr:" + (cmd[6]) )
  21.         console.log( "CMD->Operation  CMD:" + (cmd[7]) )
  22.         console.log( "CMD->Register  Addr:" + (cmd[8]*256+cmd[9]) )
  23.         console.log( "CMD->Register Count:" + (cmd[10]*256+cmd[11]) )
  24. //        console.log( "\n" )
  25.         console.log( "ACK->Transaction ID:" + (ack[0]*256+ack[1]) )
  26.         console.log( "ACK->Protocol    ID:" + (ack[2]*256+ack[3]) )
  27.         console.log( "ACK->Data      Size:" + (ack[4]*256+ack[5]) )
  28.         console.log( "ACK->Device    Addr:" + (ack[6]) )
  29.         console.log( "ACK->Operation  CMD:" + (ack[7]) )
  30.         console.log( "ACK->Raw Data  Size:" + (ack[8]) )
  31.         console.log( "ACK->Register[" + (cmd[8]*256+cmd[9]+0) + "]:" + (ack[9]*256+ack[10]) )
  32.         console.log( "ACK->Register[" + (cmd[8]*256+cmd[9]+1) + "]:" + (ack[11]*256+ack[12]) )

  33. });

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

  37. var nCnt = 0;

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

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


  42.         tcp.write(cmd);

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

  47.         if(nCnt>10)
  48.                 client.destroy();
  49.         nCnt++;

  50. },3000);



再贴一个js版本的,浏览器只支持websocket不支持纯Socket,需要有nodejs环境下Run
gaoyang9992006 发表于 2018-10-16 16:30 | 显示全部楼层
楼主加油了。还有做WIFI模块演示用Lua也非常给力,就是NodeMCU固件。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

338

主题

7307

帖子

26

粉丝
快速回复 在线客服 返回列表 返回顶部