PLC for CAI Webcontrol

Mike P

Active Member
Hi, I need a few pointers on writing PLC code. I have a simple email working for one temp. sensor, but when I add a second I receive email from the first one. I know a little of PLC code, but I just can't seem to get a grasp on all of it. Below is what I have so far, If someone could point out my mistakes, that would help me out in learning better about the code.

Thanks,

Mike

START
SET RAM2 0
LOOP:
SUB T1 RAM1
TSTLE RAM1 500 RAM1
GOTO SEND
SET RAM2 0
GOTO LOOP
END
SEND:
BNZ RAM2 LOOP
SET RAM2 1
EMAIL EM1
GOTO LOOP

LOOP:
SUB T3 RAM3
TSTLE RAM3 510 RAM3
GOTO SEND
SET RAM4 0
GOTO LOOP
END
SEND:
BNZ RAM4 LOOP
SET RAM4 1
EMAIL EM2
GOTO LOOP
 
I think I see part of my problem. Can you edit directly in the PLC screen? Everytime I edit the code, send, and exit and return to to the plc screen my changes such as start and end are not there. Must I write it in Word and copy and paste in?
 
I think I see part of my problem. Can you edit directly in the PLC screen? Everytime I edit the code, send, and exit and return to to the plc screen my changes such as start and end are not there. Must I write it in Word and copy and paste in?

Sorry, I haven't studied your code and am afrain I don't have the time right now.

However, regarding your editing issue. You should be able to edit and save right there on the gui. I don't know why you are having issue with that. But, I do recommend using ms word or another word processor as it is just a bit easier to edit.
 
You can edit directly in the PLC screen, thats how I do it. I only copy it and paste to textpad as a backup. You have 2 "Loop" and 2 "SEND" routines, so when you say GOTO LOOP it might get confused. Also you have more than 1 "END" Might work better if you broke it up like this:

Start:
CALLSUB LOOP1
CALLSUB LOOP2
END

LOOP1:
xxx
xx
xx
RET

LOOP2:
XXXX
XXXXXX
XXX
RET

Or somthing to that structure.
 
There is only one START and one END actually allowed, I remember talk to the support before. They told me the PLC logic execute from START to END repeatedly. If you have more than one START or END, that can cause confusion, Like Machineman said, you also have LOOP and SEND used more than once. I would change them like:

START
SET RAM2 0
SET RAM4 0
LOOP:
SUB T1 RAM1
TSTLE RAM1 500 RAM1
CALLSUB SEND1
SET RAM2 0
LOOP2
SUB T3 RAM3
TSTLE RAM3 510 RAM3
CALLSUB SEND2
SET RAM4 0
GOTO LOOP
END

SEND1:
BNZ RAM2 LOOP2
SET RAM2 1
EMAIL EM1
RET

SEND2:
BNZ RAM4 LOOP
ET RAM4 1
EMAIL EM2
RET

I did not see when your RAM1 and RAM3 being initialized.
 
There is only one START and one END actually allowed, I remember talk to the support before. They told me the PLC logic execute from START to END repeatedly. If you have more than one START or END, that can cause confusion, Like Machineman said, you also have LOOP and SEND used more than once. I would change them like:

START
SET RAM2 0
SET RAM4 0
LOOP:
SUB T1 RAM1
TSTLE RAM1 500 RAM1
CALLSUB SEND1
SET RAM2 0
LOOP2
SUB T3 RAM3
TSTLE RAM3 510 RAM3
CALLSUB SEND2
SET RAM4 0
GOTO LOOP
END

SEND1:
BNZ RAM2 LOOP2
SET RAM2 1
EMAIL EM1
RET

SEND2:
BNZ RAM4 LOOP
ET RAM4 1
EMAIL EM2
RET

I did not see when your RAM1 and RAM3 being initialized.
FWD03

I tried your code, it looks good. But I just get constant emails as soon as saving it. I don't see why. Does anybody see why? I know this should be easy, but i'm struggling with it.

Thanks,

Mike
 
It's been 6 months since I have done any PLC programming, but there is a lot of stuff in here that appears to not accomplish anything.

Perhaps you could describe in words what it is you are trying to do.

Your getting the email multiple times because every single time the program loops back and starts over, the bnz statement won't branch since it will be 0 and the email will get sent again. After it sends the email, it returns, and the first command on returning is to set ram2 back to 0
 
It's been 6 months since I have done any PLC programming, but there is a lot of stuff in here that appears to not accomplish anything.

Perhaps you could describe in words what it is you are trying to do.

Your getting the email multiple times because every single time the program loops back and starts over, the bnz statement won't branch since it will be 0 and the email will get sent again. After it sends the email, it returns, and the first command on returning is to set ram2 back to 0

Lou,

Hi, What I am trying to do is have each Temp. sensor send me an email once the temp. reaches a preset low level. A second email also if it drops below a second preset level. I am trying to monitor the temp. in a second home. I have it CS monitored, but all I get is the alarm, no actual temp. The home is in Lake Placid, NY and the winter temp's dip down to -30 and colder sometimes. Pipes will freeze fast if I dont know about a problem.

Thanks,

Mike
 
I wrote this fairly quickely and haven't tested it but, here it is:

START
TSTLT T1 500
CALLSUB COLD1
TSTGT T1 510
SET RAM1 0
TSTLT T1 550
CALLSUB COLD2
TSTGT T1 560
SET RAM2 0
END

COLD1:
TESTEQ RAM1 1
RET
EMAIL EM1
SET RAM1 1
RET

COLD2:
TESTEQ RAM2 1
RET
EMAIL EM2
SET RAM2 1
RET

The first line tests if the temp is less than 50 degrees and calls the sub if it is. The sub tests to see if ram1 is 1 and if so it aborts. If not, it skips the ret and sends the email. ram value default to 0 each time you power cycle so the first time the subroutine runs it will finish. At the end of the subroutine, it sets ram1 to 1. Ram1 will stay 1 until the temp goes above 51 at which time it returns to 0. You want at least a one degree "bounce" range in there to keep from getting multiple emails if the temp probe is hovereing at 50 plus or minus a tenth or two.

Cold2 subroutine does the same thing for 55 degrees.
 
Lou, Thank You very much. I will give this code a try and try to edit if needed. Im at the house now and for the weekend, But I don't think I will have time to test until i'm back home on Monday. Adding a new kitchen and removing all the existing plumbing and electric and replacing with new, doing it all myself. quite busy...It is fun(but dirty).

Thanks,

Mike

I'll learn this code if it kills me
 
Lou,
I tried your code, a couple of edits and it works fine...Thanks. I am confused about the start and end of PLC though. I plan on adding temp. sensors on each floor, do I just keep inserting the text before the end command?

Thanks,

Mike
 
Start and end define the main body of the program. End just sends the program back to start. It keeps cycling top to bottom. For the most part, when I write PLC code, I don't really have much happen in the main body, it just tests conditions and shunts it off to a subroutine and then it returns to the main body.

And yes, you just add more and more callsub commands in the start/end section for more temperature probes (or whatever other tasks you like).

And yes, Damn!! I can't help but type "testeq" instead of "tsteq". At least I didn't do it on all of them. Frankly, I can't imagine dropping that one letter shaved anything off of the code space and it foils me every time.
 
Lou,

Once again, Thanks. Do you or anybody else know of a good website that I use to get information on writing PLC code?

Thanks,

Mike
 
None that I know of. Plus PLC is not exactly the same on all devices that use it. But the cai's instructions are pretty inclusive. You just have to start writing code while keeping a copy of the cai manual next to you while you work. After a bit of practice you start thinking in plc terms.
 
Back
Top