64-bit to 32-bit communication tip

A common problem I’ve noticed is that automation hardware often uses 32-bit DLLs for communication. This can be annoying because it sets a limit on memory usage and many libraries (like Python’s scipy) aren’t compatible with 32-bit.

One work-around is to use a server running in 32-bit that can talk to the 32-bit DLLs, but receive HTTP requests from 64-bit processes. Nothing crazy complicated but this has come up so often for me that I figured it might be helpful for other people. Here’s an example:

class COMClient:
    def __init__(self):
        self.client = None
    
    def ensure_device_exists(self):
        """Initialize the COM client connection."""
        if self.client is None:
            import win32com.client
            self.client = win32com.client.Dispatch('BMG_ActiveX.BMGRemoteControl')
            self.client.OpenConnectionV('CLARIOstar')  # Replace with your connection parameters
        
    def execute_and_wait(self, activex_args):
        """Execute and wait method on the COM client."""
        self.ensure_device_exists()
        self.client.ExecuteAndWait(activex_args)


# Create a global COMClient instance
com_client = COMClient()

def create_server():

    app = Flask(__name__)
    CORS(app, origins=["*"])  # Allow any origin
    
    # Initialize scheduler and runtime with database directory

    @app.route('/execute-and-wait', methods=['POST'])
    def execute_and_wait():
        data = request.get_json() or {}
        activex_args = data.get('activex_args')
        com_client.execute_and_wait(activex_args)
        return jsonify({
            'success': True
        })
    
    app.run(host='0.0.0.0', port=5100)

5 Likes

Great post, this also highlights the beauty of standardized communication formats for hardware. You can abstract the messiness away.

1 Like

We’ve come into issues with this recently too. Nice solution, at least for where you can use python.

Why don’t manufacturer’s upgrade to 64-bit architecture? It’s existed since the 1970s!!!

Not in itself an issue but can also be seen as an example of an indicator that shows that our field still has a long way to modernise and improve.