Rust for PLC programming

Hi guys, I have been following use of Rust in embedded for some time now, and I’m impressed over the progress that is being made all the time.

I have been thinking lately over the possibility of using Rust in relation to PLC’s.

As you may know, programming of PLC’s is usually done in IEC61131-3, which is a collection of 5 languages, where Ladder logic and perhaps Structured Text (a textual language which is limited in ability by design (less footguns) and focused on maximum readability) are the most well-known. The focus I guess is foremost on ease of programming and troubleshooting/debugging.

It would be exciting do something in this space, like a ladder/structured text interpreter or something else to run on resource-constrained devices. PLC’s are similar, but another use case in relation to embedded systems. And I think there is some potential here for Rust.

What are your thoughts on Rust in this space, are there already some discussions or projects going on?

Thanks!

3 Likes

I’ve played around with this idea in the past. In particular, I started writing up a basic compiler frontend for structured text and got about midway through the program analysis stage before getting distracted.

The way I’d probably approach this is to write a compiler that runs on a normal PC and then a #[no_std] virtual machine which can run on your microcontroller. Compilation often requires lots of data structures and dynamically-sized collections, which isn’t great on a constrained device which may not even have an allocator.

2 Likes

Cool, is your description of the approach the goal of your project? Looked at your project, nice to see ecs used for new problems outside of games.

Hey guys. It’s really a cool initiation. I’ve been a PLC programmer for years in the automotive industry and I decided to abandon it since the tooling around development is quite poor.

Back at the time (can’t see much changes until now either) there was no real static code analysis tools, unit testing frameworks, integration to source control to mention the most painful ones.

I like the new features in IEC61131-3’s structured text but still I feel like it would be much better to introduce a modern language with all of its tools to the PLC world instead of falling back to restricted languages.

What do you think about a SoftPLC runtime that would execute rust code?

I agree that Rust is for sure a more safe and powerful language than Structured Text.

On the other hand, I guess sometimes readability and fast troubleshooting “on site” is rated higher than even the problems of the original programmer. Classic example is the technician who can easily read Ladder Logic, but not i.e. Rust. :smiley:

Maybe we could leverage some of Rust’s excellent tooling for even the 61131-3 code if the compiler etc. is written in Rust?

I’ve written something that goes that direction: https://github.com/birkenfeld/ethercat-rs

It’s basically a -sys binding to the IgH Etherlab master, so that a Rust program can communicate with EtherCAT slaves, and (the beginnings of) a higher level API.

The demo program shows how this looks currently. In addition to talking to Beckhoff terminals, most of the code implements an interface that we’re using @work to talk to PLCs from the software side, and exports this via Modbus. (This defines all the iface stuff and the state machines.)

At the moment, no attempt is made to get true real-time behavior, though this should be achievable with the right OS patches. It’s not a priority for us though.

Well I’ve been involved in PLC programming quite a lot, and we utilized a really cool framework with a lot of abstractions in a production environment. Two languages were used extensively, one was ST the other SFC, so no LD not FBD, because they are quite limited and after some complexity they tend to become ugly and quite unmaintainable.

So to wrap it up, our technicians could still easily fix any incidents, mostly due to the high level of diagnostics applied to the stations without even having to peak into the code base. But still when they had to debug the code they could understand it quite easily because of the APIs were easy to follow.

For a newcomer graphically represented languages may be easier to pick up at first glance but after some time they are quite limited and not suitable for complex machines we have nowadays.

1 Like

I really like your example. Maybe we could set up a slack channel and share ideas.

I honestly think that a SoftPLC runtime that could run rust applications to control various IO cards and components would be really beneficial for the future.

The PLC world could be easily introduced to other parts and people of software development, and I think would be much more fun :slight_smile: