Sign in to follow this  
Followers 0
callelundin

Read bit from Word CITECT

6 posts in this topic

Hey, im cheep so i dont wanna spend tags on digital signals. So i vounder how i read a specified bit from a word. On my PLC i set the bit %MW54.1 and then i whant to read that bit in Citect with Cicode. I have a tag with %MW54 setup in the Variable list. Somthing like TAG1 = TAGMW54.1 ? Anybody have a clue, i looked att BitAND function but didnt understand it from the helpfiles!

Share this post


Link to post
Share on other sites
Hello Here is what I use: To read bit 1 in Tag1 use: DigitalBit = ReadTagBit(Tag1,1) To write 1 to bit 1 in Tag1 WriteTagBit("Tag1",1,1) To write 0 to bit 1 in Tag1 WriteTagBit("Tag1",1,0) //////////// INT FUNCTION ReadTagBit(INT Tag, INT Bit) //Bits numbered 0-15 INT TagVal, Power; Power = Pow(2, Bit); IF Tag BITAND Power THEN TagVal = 1; ELSE TagVal = 0; END RETURN TagVal; END FUNCTION WriteTagBit(STRING Tag, INT Bit, INT Val) //Bits numbered 0-15 INT TagVal, Power, NewVAL, Stat; STRING STagVal; STagVal = TagRead(Tag); TagVal = StrToInt(STagVal); Power = Pow(2, Bit); Stat = ReadTagBit(TagVal,Bit); IF Val = 1 AND Stat = 0 THEN NewVal = TagVal + Power; TagWrite(Tag,NewVal); END IF Val = 0 AND Stat = 1 THEN NewVal = TagVal - Power; TagWrite(Tag,NewVal); END END

Share this post


Link to post
Share on other sites
tag1 = TAGMW54.1 bitand 1 you read the first bit to read the second tag1 = TAGMW54.1 bitand 2 the third tag1 = TAGMW54.1 bitand 4 i save as label the value 1 as bit0, 2 as bit1, 4 as bit2 etc etc. so i can simply write tag1 = TAGMW54.1 bitand bit0 Edited by valerio81

Share this post


Link to post
Share on other sites
Automation, sledgehammer to crack a nut ? As far as i am aware internal disk devices are not counted as points, correct ? So create a disk device called 'Decode' or whatever, read the word from the PLC or real world device, in this case %MW54 which we will call 'Real_Variable', in your internal Disk device have a %MW54 configured which we will call 'Decode_Variable' (can be anything but its easier to work with if you have the same address) Then create tags against the internal Disk device word at the bit level: Internal_Variable_1 - %MW54.1 Internal_Variable_2 - %MW54.2 Internal_Variable_3 - %MW54.3 Internal_Variable_4 - %MW54.4 etc. etc. Now run a piece of code at a time interval of your choosing to basically do: Decode_Varable = Real_Variable Job done, unless of course Citect now treats disk device variables as points !!

Share this post


Link to post
Share on other sites
I dont know but i cant get this simpel thing to work! I must be stupid?! This is my code FUNCTION readbit() IF (ReadTagBit(Nu,2)=1) THEN tag1=1 ELSE tag1=0 END END It should work lite this... When the bit 2 on Integer variabel "Nu" is 1 i whant digital "tag1" to be 1. Whats wrong?? ARGH! :D

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
Sign in to follow this  
Followers 0