MySQL Connector For B&R PLC Without MappDB

Hello,

I created a working MySQL database connector using AsTCP and some homemade libraries in C.

The reason for creating this was to avoid having to run the python script on the PC with the database. The python connector only supports 1 PLC at a time and is another thing you have to make sure is running when you start the PC/server. I’ve tested this and got it fully functioning, but it looks like I may never get to use it as my employer wants to go a different direction for the project that I developed it for.

To run it, you just need to create a task with all of the files included, declare variables of the UNI_MYSQL_PUB_typ type and UNI_MYSQL_typ type, then run the init function (not in the _INIT as it may need more than 1 scan to complete), call the configuration functions to setup the login for the database, set a timeout, then cyclically call the Cycle function. This task will be responsible for handling queries generated by other tasks and managing the connection.

Tasks outside of the one running the connection may use the public query function to get/send data to/from the database. The task will need a simple state machine in which the first state is calling the query function with a pointer to the public datatype, passes a string for the name of the semaphore holder (can be anything, just to keep track of who/what is using the database connection), a query string (same as you would enter in MySQL workbench), and a pointer to a variable of UNI_MYSQL_RETURN_DATA_typ type. This will be where the array of STRINGs returned from a SELECT statement will go. After executing the query, move to the next state where it’ll wait for the return data and then you can parse it out however you’d like.

That is a very simple explanation, but if anybody has any questions, I’d be happy to answer them.

I haven’t seen anybody else share anything like it so I thought I would. If anybody wants to try it out or has a practical application for it and wants me to send them an example let me know!

20 Likes

Thank you for sharing!
I’m glad to see I’m not the only one that thinks the solution with a python script is not very robust.
I’m still using the old AsDB library as a consequence.

4 Likes

Thanks for sharing!
I’m not quiet clear with how PLC connect with DB, if there is no HTTP connector. Could you give us a basic example about how to use this lib. That’s will be very helpful!

Hi

thx for the work. Is it possible to get an example on how to implement this?

Greetings Kevin

1 Like

Here’s my example project. I’m generating queries from 2 tasks and have the connection operated by another. After I get the return data I’m parsing it as string into the appropriate fields.

Let me know if you have any questions!

https://www.dropbox.com/scl/fi/qyapv27llknmfv8r33je0/SQL-Connector-Example.zip?rlkey=oup9n4xjb1u6x5qf4tshcgosl&st=an7px7oe&dl=0