Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:40:23+00:00 2026-06-10T02:40:23+00:00

I need to receive data from device connected via Ethernet (modbus/TCP) and send it

  • 0

I need to receive data from device connected via Ethernet (modbus/TCP) and send it to webpage (maybe using web sockets).

I can’t find good examples. Now I can connect with driver and print values using ModbusClientProtocol.read_input_registers() but I had to create own factory and protocol class. I am using autobahn, twisted, pymodbus.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:40:24+00:00Added an answer on June 10, 2026 at 2:40 am

    I’ve no familiarity with modbus or pymodbus, so I’m guessing and leaving a lot of blanks for you to fill in.

    This is hacked out of something I recently put together to receive snmptraps and redistribute the information to connected websockets.

    Hopefully this is enough to get you going:

    #!/usr/bin/python
    from twisted.internet import protocol, reactor, utils, defer
    from twisted.web.server import Site
    from twisted.web.static import File
    
    from autobahn.websocket import WebSocketServerFactory, WebSocketServerProtocol
    from autobahn.util import newid
    from autobahn.resource import WebSocketResource
    
    class ModbusThing(object):
      def __init__(self,clientAddress):
        self.clientAddress = clientAddress
        self.client = None
      def start(self):
        pass
        ## Create client connection to modbus server
        ## Start Looping Call of pollForData with suitable interval
      def pollForData(self):
        pass
        ## Call read methods on ModbusClient object, add call backs to process the results
        ## Add errorBacks to notify of errors
      def resultCallback(self,result):
        pass
        ## Process the data from a read request
        ## Assumes that your websocket clients expect json like {"event":"update","data":[0,1,2]}
        message = dict(event="update",data=processedResults)
        self.broadcast(json.dumps(message))
      def broadcast(self,msg):
        """Override me"""
        pass
    
    class TrackingWebSocketProtocol(WebSocketServerProtocol):
      def onOpen(self):
        self.session_id = newid()
        self.factory._addSession(self,self.session_id)
        print "Socket Open %s" % (self.peerstr,)
    
      def onMessage(self,payload,isBinary):
        print "Message received from %s\n\t: %r" % (self.peerstr,payload)
    
      def onClose(self,wasClean,code,reason):
        self.factory._removeSession(self)
        print "Socket Closed %s" % (self.peerstr,)
    
    class TrackingWebSocketFactory(WebSocketServerFactory):
      def __init__(self,*args,**kwargs):
        WebSocketServerFactory.__init__(self,*args,**kwargs)
        self.proto2session = {}
        self.session2proto = {}
      def _addSession(self,proto,session_id):
        if not self.proto2session.has_key(proto):
           self.proto2session[proto] = session_id
        else:
           raise Exception("logic error - dublicate _addSession for protoToSessions")
        if not self.session2proto.has_key(session_id):
           self.session2proto[session_id] = proto
        else:
           raise Exception("logic error - dublicate _addSession for sessionsToProto")
      def _removeSession(self,proto):
        if proto in self.proto2session:
          session_id = self.proto2session[proto]
          del self.proto2session[proto]
          if session_id in self.session2proto:
            del self.session2proto[session_id]
      def sendToAll(self,message,binary=False):
        prepped = self.prepareMessage(message,binary)
        for proto in self.proto2session.keys():
          proto.sendPreparedMessage(prepped)
    
    
    def run():
      ## WebSocket Factory
      wsfactory = TrackingWebSocketFactory('ws://yourhostname:80')
      wsfactory.protocol = TrackingWebSocketProtocol
      wsresource = WebSocketResource(wsfactory)
      ## Modbus handler
      modbus_thing = ModbusThing((addressofserver,portofserver))
      modbus_thing.broadcast = wsfactory.sendToAll
      modbus_thing.start()
      ## WebServer Site
      # "static" subdirectory, containing http served resources, e.g. index.html, javascript and css
      root = File("static")
      # Your websocket service as 'ws://yourhostname/ws'
      root.putChild("ws", wsresource)
      site = Site(root)
      reactor.listenTCP(80,site)
    
    def main():
      reactor.callWhenRunning(run)
      reactor.run()
    
    if __name__=='__main__':
      main()
    

    On the browser side of things. A little module for interacting with websockets is handy:

    var FancyWebSocket = function(url){
      var conn = null;
      var fws = this;
      if ("WebSocket" in window) {
        conn = new WebSocket(url);
      } else if ("MozWebSocket" in window) {
        conn = new MozWebSocket(url);
      } else {
        console.log("Error Websockets not supported in browser");
        return;
      }
      var callbacks = {};
      var debug = true;
      this.bind = function(event_name, callback){
        callbacks[event_name] = callbacks[event_name] || [];
        callbacks[event_name].push(callback);
        return this;// chainable
      };
      this.send = function(event_name, event_data){
        var payload = JSON.stringify({event:event_name, data: event_data});
        conn.send( payload ); // <= send JSON data to socket server
        return this;
      };
      this.close = function(){ conn.close(); return this;}
      // dispatch to the right handlers
      conn.onmessage = function(evt){
        if (debug) console.log("Websocket(" + conn.URL + ") Message: " + evt.data)
        var json = JSON.parse(evt.data)
        dispatch(json.event, json.data)
      };
      conn.onclose = function(){
        if (debug) console.log("Websocket(" + conn.URL + ") Closed");
        dispatch('close',fws);
      }
      conn.onopen = function(){
        if (debug) console.log("Websocket(" + conn.URL + ") Open");
        dispatch('open',fws);
      }
      conn.onerror = function(e){
        if (debug) console.log("Websocket(" + conn.URL + ") Error: " + error);
        dispatch('error',fws,e);
      }
      this.setdebug = function(v) { debug=v; return this; }
      var dispatch = function(event_name, message){
        var chain = callbacks[event_name];
        if(typeof chain == 'undefined') return; // no callbacks for this event
        for(var i = 0; i < chain.length; i++){
          chain[i]( message )
        }
      }
    };
    

    Then in your browser console:

    conn = new FancyWebSocket("ws://yourhostname/ws");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to send and receive data to/from a microcontroller using java using usb
Ok i need to use json on my iOS application to send/receive data to/from
I am using the Add-on builder and I need to receive binary data (image).
In order to send and receive encrypted messages from/to the iPhone I need to
I'm looking to work on an iOS app that would need to send/receive data
I need to receive XML data from HttpPost requests. Currently I use HttpWebRequest to
I am looking for suggestions on the best way to send/receive data from a
My app receives data from a device via bluetooth continuously, depending on the data
My project has a need to cache on the iOS device data retrieved from
On success of a web service i receive a UUID. ie C3D668DC-1231-3902-49318B046AD48A5F I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.