cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Get file from PLC to PC with Python

Get file from PLC to PC with Python

Antonio3
Member

Hello,

I am trying to get .bin files from PLC to PC. Currently I can do so with usage of FB IL_FTPPutFileAsync (from CXA_SocketComm library). Which means initiate data transfer from PLC (client) to PC (server).

But my question is, is it possible to initiate data transfer from PC (specifically with Python)? Should it be possible with FTP or with WebDAV e.g.?

I have read a lot of similar topics so sorry for asking question again, but I haven't found clear answer for initiating that kind of data transfer from PC.

Thanks a lot for your support.

3 REPLIES 3

AndreasL
New Contributor

There are no FTP server in the core, but it's possible to send a file from a PC to a core via WebDAV, for instance WinSCP as described in this post:

https://developer.community.boschrexroth.com/t5/ctrlX-WORKS/How-to-update-the-configuration-of-an-ap...

Another way is to have a file share on the Windows PC, and in the core have a Node-RED flow that scans for new files in a folder on the share, and then automatically copy files from the PC to the core from Node-RED.

https://flows.nodered.org/node/node-red-contrib-smb

But there is also another ways to do this, is it only text files they can be sent via the rest api to a PLC variable.

Its also possible to upload a file to the app data in the core using the REST interface.

So it mosly depends what's the application.

 

 

Thanks. But my goal is to get files from PLC to PC, and use PC as initiator of this data transfer.

In my application I would like to use python script to do all the work, like get files and then work with them. 

Firstly I was thinking about ftp, because in CXA_SocketComm documentation is mentioned this:

Antonio3_0-1707391510311.png

It should mean that there is running server. Also when I tried to connect to it with FileZilla client it says "Connection refused by server", so if I am not mistaken, than there is some server running, no? But I didn't find any way, how to connect to it and use it.

Antonio3_2-1707392649380.png

So shouldn't it be possible to initiate file transfer from some PC client or server?

 

The other way is to use WebDAV. I setup connection between CtrlX CORE and Windows PC via Map network drive as mentioned in the topic mentioned below. Then I am able to work with files directly.  

https://developer.community.boschrexroth.com/t5/ctrlX-PLC/Accessing-Logfile-on-ctrlX-with-tail-progr...

Antonio3_1-1707392368969.png

I don't have much experience with WebDAV yet. Is that connection stable? Is that good way to get files from PLC, or is there better way? 

Thanks.

 

 

Do get a file from a core to a PC, you can just use the REST interface like this:

import urllib.request
import ssl
import json

ssl._create_default_https_context = ssl._create_unverified_context

def auth():
    head = {
        "Content-Type": "application/json",
        "Accept": "application/json",
    }

    body = {
        'name': 'boschrexroth',
        'password': 'boschrexroth',
    }

    data = json.dumps(body).encode("utf-8")

    req = urllib.request.Request('https://192.168.1.1/identity-manager/api/v2/auth/token', data, head)

    with urllib.request.urlopen(req) as f:
        res = f.read()

    resonse = json.loads(res.decode())

    return resonse['access_token']

 

def download(token, name):
    opener = urllib.request.build_opener()
    opener.addheaders = [('Authorization', 'Bearer ' + token)]

    urllib.request.install_opener(opener)
    urllib.request.urlretrieve("https://192.168.1.1/solutions/files/DefaultSolution/configurations/appdata/" + name, name)
    

token = auth()

download(token, 'configuration.json')

Modify the IP-adress and filenames according to your settings.

This can download files from the APP data folders of the core, in the example above the configuration.json file:

AndreasL_0-1707399415702.png

 

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist