c/c++ useful skill for Controls guys?

That book may be the one written by Axel Tobias Schreiner. He was at Bell labs with Dennis Ritchie and Brian Kernighan. I've read the book and it is outstanding and proves that ANSI C is an OOP language, but the actual definition of an OOP language is one that supports 1. encapsilation, 2. Inheritance and/or composition. 3. polymorphism. ANSI C does this.

That book sounds interesting but it wasn't the one I was thinking of and I'll defer to the experts then if ANSI C is OOP. I guess then I'd revise my statement that languages like C++ and Swift make it more convenient to do OOP because they provide built in or short cut constructs for the encapsulation, inheritance and polymorphism. :p
 
The employers question could just be a way of asking for any knowledge of high level languages.

The B&R plc implements their version of c++ in their plc to use by the daring. For HMI then visual basic is a common script language.
 
If you want to learn OOP start using an appropriate language for it, I recommend Java that is easier than C ++.

Although the most difficult part of OOP is not to master the language but to design a proper classes heriarchy.
 
The last time I looked, C was not object oriented. You can almost make it so by creating structures with pointers to methods. However, when you create a new instance of a structure for and new instance of a class, the programmer must fill in any of the static methods that are common to the class and then create all the instance methods. None of this is done automatically in C.

Our motion controllers are programmed in C that has been coerced to being very oop like so I know the tricks and what must be done. There are a lot of pointers necessary to do this whereas in most OOP languages the use of pointers in prohibited or frowned upon.

I find C to be very simple except for one thing. I am constantly looking up how to declare complex data structures. This isn't required in Java or C++. Once the data structure is correctly defined I make a typedef and carry on.
 
The last time I looked, C was not object oriented. You can almost make it so by creating structures with pointers to methods. However, when you create a new instance of a structure for and new instance of a class, the programmer must fill in any of the static methods that are common to the class and then create all the instance methods. None of this is done automatically in C.

Our motion controllers are programmed in C that has been coerced to being very oop like so I know the tricks and what must be done. There are a lot of pointers necessary to do this whereas in most OOP languages the use of pointers in prohibited or frowned upon.

I find C to be very simple except for one thing. I am constantly looking up how to declare complex data structures. This isn't required in Java or C++. Once the data structure is correctly defined I make a typedef and carry on.

Interesting comment on the static methods. The static keyword has 3 meanings in C.

1. A static variable declared in the body of a function retains its value between function invocations.

2. A static variable declared outside the body of a function is only visible to methods inside that .c module or file. (private field)

3. Methods declared static are only visible to other methods inside of that .c module or file. (private method)

Items 2 and 3 are used for the encapsulation portion of OOP. I'm not sure why you would want to re-write any static methods. Those static(private) methods are there to hide the implementation details of a class. However, there's more than one way to do OOP in C so maybe its required in some libraries. I'd be curious to see an example.

SD
 
Last edited:
in C ++ a static variable member of a class means that it is the same variable for all instances that are created of that class.

A static function member of a class means that the function can only access static variables and other static functions, it can also be called without the need to create an instance.

In Java it is very similar, but in Java the class variables are called fields and the functions methods.
 
in C ++ a static variable member of a class means that it is the same variable for all instances that are created of that class.

A static function member of a class means that the function can only access static variables and other static functions, it can also be called without the need to create an instance.

In Java it is very similar, but in Java the class variables are called fields and the functions methods.

Yes, it's similar in C#. Also C# is pass by value. That's a fun one too. :)
 
This thread has inspired me to learn C properly.

I know enough to get by and fudge around in a few languages but have never actually 'studied' any, per-se.

That book 'The C Programming Language' is actually quite cheap on eBay, just ordered myself a copy!

Glad to hear you are taking the journey.

I may have slightly derailed the thread with the OOP in C stuff. I wouldn't recommend doing that. However there are a number of things in C# that are really common like events for example that are good to know in C. It's called the publish subscribe design pattern or the observer design pattern. That's the only reason I think people should learn a little C. It will give you some knowledge of what goes on under the hood.

Good luck
 
Some Ancient History and the nitty gritty.

Glad to hear you are taking the journey.
ditto

It will give you some knowledge of what goes on under the hood.

Just about any compiler that will generate assembly code will let you see what is under-the-hood. I definite recommend looking at the generated assembly code.

Way back in the 1970s I bought a copy of UCSD Pascal. I was in the navy at the time stationed in San Diego at Ballast Point. Donald Knuth was teaching at UCSD then. I saw him in the halls. I didn't really know who he was then except for yet another professor. I got copy 452 of UCSD Pascal and as one of the first outside the university to get my copy running. Some assembly was required :) UCSD Pascal was one of the first programming environments that would quickly put the cursor where you made a lexical mistake. Logic mistakes you had to find on your own.

The key thing I learned was how a modern compiler generated code with stack frames etc. Pascal was not object oriented but it did stress being organize, block programming, and eliminating gotos that were common is basic programming.

At a computer club meeting, yes there were computer clubs back then, I showed of my UCSD Pascal and immediately got an offer for twice what I was making in the navy back then but I was stuck :(

I think a second point I would like to make is that low level drivers require knowing assembly language which is kind of like programming in STL on a S7-300. One had to know how to use a linker and locator back then so the code would execute at a specific spot in the computer memory.

I have written a couple of real time kernels. I can't call them operating systems because they weren't. An embedded project often will have 5 or 6 tasks the kernel needs to manage. There are basic functions that are necessary.

Wait flag. This call is made when waiting for something. Waiting for a character interrupt from a serial uart was common back then. The wait flag routine would would stop the running task and call the task switcher. The task was put on a priority queue.

Set flag. This call is usually made from an interrupt routine. For example a set flag would be called when a character was received. This would allow the routine that called the wait flag to run by putting it on the run queue.

delay. This call is similar to the wait flag but it also has a delay parameter. The task stops running and is put on the delay queue.

dec_dealy. Decrement delay is called by the clock interrupt. Any tasks that have been running long enough are put on run queue.

mx_access. mx is short for mutual exclusion. mx_access is necessary to keep two task from accessing the same resource at the same time. A resources is assigned a token and this is used in the mx_access call. Any other task that wants to access this same resource must also call mx_access. If the resource is already in use the task will stop running and put on the waiting queue for that resource.

mx_release. mx_release is called when a task is done with the resource. The next device that is waiting for that resource is put on the run queue.

BTW, I didn't mention that when tasks are put on the run queue it is done by priority.

Notice I didn't cover memory allocation. This is because all memory is normally fixed in embedded applications.

Haven't you wondered how the internals of a PLC or motion controller work?
There is a lot more to it then just knowing C.
 
B&R supports ANSI C right out of the box and it is my goto for text heavy programming like generating ZPL labels or parsing ASCII based serial communications to old garbage. There are just so many more useful standard string functions baked into C that it just makes sense. I also write some libraries in C, partly because I don't really want someone else dicking around in my library and changing it when I use the same library on dozens/hundreds of machines. If they really want to dig in there and change it, they can find someone that knows C and won't make a mess of it.

I used to do quite a bit of motion programming in ANSI C as well since it does have many features that make it faster/easier to reuse code and scale things from 2 axes to 200 axes without much trouble. However, I have converted back to Structured Text for this kind of thing and done a lot more longhand since I find not many of my peers are comfortable with C and B&R greatly improved the capabilities of their ST implantation back in AS3.0. I do miss my macros and other pre-processor stuff, but it's not too bad.
 

Similar Topics

I am deciding on components for a bayed modular enclosure containing multiple VFDs (480V/350A/321Hp each) for a dynamometer. I am concerned given...
Replies
7
Views
1,671
I've been banging my head for a way to generalize data handling in a meaningful way in Logix. What I've concocted so far is the idea of "visitors"...
Replies
28
Views
5,423
I work for a firm that’s tied down to just a couple of older PLC types. Looking to the future I’m wondering what features particular brands have...
Replies
17
Views
4,706
Hy , so here is my problem. I what to make a program for a S5 100U PLC, the application consist in rejecting, lets say a blue cube, from a...
Replies
4
Views
1,736
Hi all. During a rummage in a dark and dusty corner today, I found an unopened RSLogix500 :) but the version was "one point something" :( (it was...
Replies
6
Views
2,058
Back
Top Bottom