CCW Micro850 Simulator http API

MikelAyani

Member
Join Date
Aug 2021
Location
Sweden
Posts
2
Hi there!
I just downloaded CCW v13 and wanted to play a bit with the Micro850 Simulator and its API. I have been able to modify the input variables using the XML API, just making changes in the inputs.xml file and it works as expected.
In the other hand, when I try to use the HTTP API and follow the examples in the manual, I am able to do GET requests to obtain the actual input and outputs status, but the POST request does not seem to work. I get a 200 response (OK) but the inputs are not updated in the simulator. Does anyone managed to get it work?
I leave you some Python code that can be used to test this. Just install the requests library and modify the PORT value.

Code:
import requests
import time

# api-endpoint
PORT = 65173
URL = f"http://localhost:{PORT}/"

MAX = 2**32-1
inputs = 0

while True:
    # Write inputs
    inputs += 1
    input_data = []
    for bit in range(20):
        value = True if(inputs & 2**bit) > 0 else False
       
  input_data.append({"Name":"_IO_EM_DI_"+str(bit).zfill(2),"Value":value})

    print("SENT:", inputs)
    print(input_data)
    r = requests.post(url=URL+"inputs", data=input_data)
    print(r)
    
    print("RECEIVED:")
    r = requests.get(url=URL+"inputs")
    data = r.json()
    print(data)
    
    time.sleep(1)
 
[Update: I think the requests.post call will have to be moved inside the inner (bit) loop

Also, is it possible that the URL for the post should be /outputs?
]

I haven't looked at the request module in a while, but the input_data object created is a list


  • with one dict when bit is 0,
  • with two dicts when bit is 1,
  • etc.
And that is not what requests.post is expecting.

I think OP is needs to do summat more like this:

Code:
...
input_data = dict()
...
and this in the innermost (bit) loop:
Code:
...
  input_data.update(dict(Name=f"_IO_EM_DI_{bit:02d}",Value=value))
...
or just drop the initial <input_data = dict()> and do this in the innermost (bit) loop:
Code:
    input_data = dict(Name=f"_IO_EM_DI_{bit:02d}",Value=value)
Cf.
Code:
>>> print(requests.post.__doc__)
Sends a POST request.

    :param url: URL for the new :class:`Request` object.
    [COLOR=Blue][I][B]:param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.[/B][/I][/COLOR]
    :param json: (optional) json data to send in the
        body of the :class:`Request`.
...
 
Last edited:
can you point us to the manual?

FYI: a possibly cleaner way to convert a bit in an integer to a boolean in Python is
Code:
  value - True if (inputs & (1<<bit)) else False

OR (my favorite)

  value = (inputs & (1<<bit)) and True or False
 
Also, is OP sure that the simulator is supposed to be able to assign the ouptuts? Isn't that something the PLC simulator should be doing, and a third-party app should be reading the state of the outputs?
 
Hi, thanks for your help but the issue was just the header in the POST request.
I tried with another library (urllib) and it worked well.

The next code reads the outputs (GET) and writes the inputs (POST):

Code:
from urllib import request
import json
import time

PORT = 63912
IP = 'localhost'
URL = f"http://{IP}:{PORT}/"

value = False

while True:
    
    # Read outputs
    req = request.Request(URL+"/outputs", method="GET")
    r = request.urlopen(req)
    content = r.read()
    output_data = json.loads(content)

    # Write inputs
    input_data = []
    value = not value # Makes inputs blink
    for i in range(28):
        input_data.append({'Name':"_IO_EM_DI_"+str(i).zfill(2), 'Value':value})
    data = json.dumps(input_data)
    req = request.Request(URL+"/inputs", method="POST")
    req.add_header('Content-Type', 'application/json')
    r = request.urlopen(req, data=data.encode())
    content = r.read()

    # Wait
    time.sleep(0.1)
 

Similar Topics

Hi guys, I have had some issues with uploading a program from a Micro 850 PLC. This is the first time connecting so I don’t have a file on my...
Replies
8
Views
304
Currently a student, so somewhat new to this. But working with an AB 2080-LC50-24QWB, CCW version 21.01.00. CCW will connect okay to the Micro850...
Replies
9
Views
308
I'm controlling an Applied Motion HW23-601D using a Leadshine EM542S connected to a 2080-LC50-48QBB, using CCW, and experiencing some weird...
Replies
2
Views
614
Hi All, Hoping the community here can help as I'm new to PLC programming. The project I'm working on is using a Micro850 2080-LC50-24QBB and...
Replies
3
Views
1,694
I am working with Rockwell Micro850 PLCs and CCW. In our application we use various arrays for pretty much everything. Some logics require...
Replies
2
Views
1,113
Back
Top Bottom