CASE statement doubts

3 posts in this topic

Hello all,

I'm quite new to PLC programming, but having learnt digital electronics and programming in PASCAL and C in the 90s, I'm trying to implement a Finite State Machine within an FX5u PLC to control an industrial automation. If it was something for myself that wouldn't require flexibility, I hard-wired it using logic gates, a counter, and latches for inputs and outputs, done several Karnaugh diagrams to simplify the circuit, and so on. If it did require flexibility and it was for a hobby, I'd probably gone for an Arduino or a simple program running in a raspberry pi. However, and since it's for work, I have to use the tools that my fellow co-workers use.

Now, in my company they use mostly Mitsubishi PLCs, but they tend to program everything in ladder and using huge, complex programs which are a pain to trace and debug, with a very slow execution time. They also tend to program in iterative steps, but they rely on a clean initial condition (meaning: if there is an error or a power cut, the operator of the machine must empty the material from the automation, home the robot, even reset all the cylinders to their initial positions (with a homing program activated from the HMI), and then restart the machine.

To prevent that, and make it more user-friendly, I'm thinking to implement a modular finite state machine where the different modules for the machine (material loading ports, doors, conveyors, robot) are all interlocked together. Having a background in PASCAL programming, it seems like going with ST language will be easier for me to get started in PLC programming.

To begin, I've declared a series of global variables for each module, called STATE_<module-name>_CURRENT and STATE_<module-name>_NEXT, all of them set as unsigned int words, so they can store an integer value and I can use them with a CASE statement.

Now, for clarity, I was planning on using global constants in those CASE statements. So, if I declare the constant ST_MACHINE_INIT as unsigned int word with value 0, typing ST_MACHINE_INIT should be the same as typing 0 in the CASE statement, and if ST_MACHINE_MANUAL_MODE is declared as a constant with a value of 1, it should be the same as typing 1, right? at least, in PASCAL (upon which the ST language seems to be based) would work like that. I have checked several manuals from Mitsubishi, but I didn't find anything in regard to that. They only say:

CASE <variable> OF

However, I've noticed that when I use a constant as value1 (declared as global labels with an unsigned integer value, same as the variables), GX Works 3 is giving me syntax errors after OF and after the statements (represented as ~).

CASE <variable> OF~

If I replace the constant by the integer value, the syntax error goes away. It seems, then, that GX Works is not accepting constants as values. Is that the case? I prefer to use constants or variables with a fixed, known value instead of just typing the integers for code clarity and because they allow for posterior modifications easily.

Does anyone have experience in this?


Share this post

Link to post
Share on other sites

Have you tried use the e—Manual Viewer?

GX3>help>e-manual viewer or

Melsec iQ-F

FX5 Programing Manual (Program Design).

constant methods for expressing constants

Data type: String(32) 

enclose character strings with single quotation

example: Stest :=‘ABC’;

thats it are you looking for?

Edited by Copyleft

Share this post

Link to post
Share on other sites

Yes, I did try the manual. I've also tried the PDFs for the GX Works 3 manual, the Structured Text Manual, the FX5 program design manual, and the FX5 application manuals.

Every place where they reference the <value> in a case statement, they don't specify if it can be a constant. A variable doesn't work because it is an expression and not a value, so I guess internally it's treating constants as expressions and not as values. The program works if I just type the integer for the constant value.

There was also another problem I figured out, which is that I can't use an unsigned word as an integer, the GX Works 3 will assume it's a 16-bit array instead, so it will give me a type mismatch error. After changing my variables to signed words, I had no more problems calling functions that require integer parameters, but still I couldn't use the constants in the CASE statement.

It's a pity, because the code is much more readable by using integer constants with significative names instead of just integer values.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now