OPC Write values - string?

Hi I am investigating writing to OPC with system.opc.writeValues(opcServer, itemPaths, values) function, I have had success writing values but now I am looking at writing strings to tags and have not had much success. I am not sure if it is because of my OPC(Kepware) tag types or if this function was not meant to write strings.

any help would be appreciated!

You’ll need to provide examples, and identify your Kepware version and driver. There is a great deal of variability by driver in Kepware data type support.

I know this is very old but I figured it out. In my case, I type casted to a byte array. Must be how the data is represented at a lower level. I am dealing with an Allen Bradley PLC.

image

Here is my test script:

tankSerialLength = 14

serialNumberLenTagPath = '[Line11_Charger_PLC]Incoming_Serial_AC2.LEN'
serialNumberDataTagPath = '[Line11_Charger_PLC]Incoming_Serial_AC2.DATA[0]'
testStringa = bytearray('ILOVEDONUTS123')

tagList = []

for indexes in range(tankSerialLength):
temp = '[Line11_Charger_PLC]Incoming_Serial_AC2.DATA[{}]'.format(indexes)
tagList.append(temp)

system.opc.writeValue('Ignition OPC UA Server', serialNumberLenTagPath, tankSerialLength)
test = system.opc.writeValues('Ignition OPC UA Server', tagList, testStringa)

Your solution is very dependent on the PLC and OPC driver that you're using. The original poster was using a Kepware server, so you were not having the same problem that he was having.

Also, don't the Allen Bradley drivers support Strings? I'm not sure why you would need to cast a byte array. It's likely the driver is coercing this into a String anyway. This is just my educated guess, I am not that familiar with Allen Bradley PLCs or the drivers.

Edit: I didn't see that you were writing individual bytes

Hi Cody,

I wasn’t aware Kepware would have its own special needs. I posted the screenshot to help explain the code. To write to the strings in the PLC from ignition. I had to define the length, then send each character individually. At first I tried type casting to a list() but in the PLC I was getting some weird unwanted extra values. I had a hunch it was character encoding so I tried type casting it to a byte array and it worked.

I posted it here because it sounded like his problem was close to mine. Here is more information about my setup in case anyone else shares my issues:

Ignition Version: 8.1.35

PLC type: 1769-L24ER-QBFC1B CompactLogix 5370

PLC Firmware Version: 32.015

PLC Vendor: Allen-Bradly/Rockwell Automation

This complication should not be necessary. Just write the string to [Line11_Charger_PLC]Incoming_Serial_AC2.

1 Like

You’re absolutely right! I must have been lost in the sauce. I swear I tried this earlier and ran into casting errors but maybe I was doing something else wrong. I will remove my post LOL

Please don't. It is instructive to future searchers.

2 Likes

Now it’s time for me to do a refactor :rofl: