0

I'm using OPC DA Automation Wrapper to connect to an AllenBradley PLC.

I now have the requirement to set a bit within a word, independently to any other bits.

Is there any way in which I can achieve this function rigorously?

The two options I have tried are:

Setup the word as seperate bits in the OPC server (Kepware). So in other words I have 16 tags, where each one is a bit in the word, the tag names then look like:

[FULLTAGNAME].0
[FULLTAGNAME].1
[FULLTAGNAME].2

However this means I need to keep many seperate OPCItem objects

The other method is to read the entire word, convert it into bits using BitConverter, change the bit I need to alter and then write the hold word back to the PLC.

However during testing I found that thread 1 could read the word. Thread 2 would then write into the word, changing say bit 2. Thread 1 would then change bit 4 and write the whole word back and the change to bit 2 would be overwritten.

Does anyone know a way in which I can have a single OPCItem and write to a specific bit?? Or am I stuck with having a lot of OPCItems which relate to a certain bit??

Thanks

eglease
  • 2,445
  • 11
  • 18
  • 28
Nick Williams
  • 1,237
  • 6
  • 19
  • 40
  • 1
    I think you've essentially laid out the options. Personally, I feel that bundling bits into words is bad practice. Keep them separate - it's cleaner and easier to read, work with, and debug. Yes, you need more tags, but if you design sensibly from the start you can generate the tag file programatically and it makes no extra work. see : http://stackoverflow.com/a/1168196/327083 – J... Jun 05 '13 at 11:37
  • Fair enough. I agree with you that building bits into words is a bit nasty. I was just hoping there was something I didn't know about. I have used separate bits a lot before but I never like the look of something like `OPCItem[] Word1Bits = GenerateOpcItmes();` or whatever. Thanks for your help. – Nick Williams Jun 05 '13 at 12:17
  • This isn't just an OPC question, but also the underlying PLC protocol question. The "OPC driver" for this PLC's protocol may just implement modifying a single bit of a WORD as a read-modify-write operation (i.e. it reads the whole word, tweaks the bit being "written to", then writes back the whole word, so your multi thread concerns still exist). You THINK it's just performing a bit-write, but it is emulating a bit-write. I would test it out, unless you know the details of the actual protocol and the OPC driver for that protocol. – franji1 Jun 10 '13 at 02:43

1 Answers1

0

Set up the OPC driver to use an array of bits. The PLC itself will accept a reference to memory as an array of bits with an offset. After that it is only a question of how the OPC driver actually implements this. I'm pretty sure Kepware will accept it this way.