How to Read and Manipulate Raw Data in WinCC using C-script

Question:

Hello, I have a database containing 1000 integers, represented in WinCC as a set of 5x 400 Bytes of raw data. I am using a script to read this data and convert it into integers successfully. My goal is to multiply these integers by 1000 and write them back into the database without changing its structure in WinCC. I am using the following code snippet to achieve this: ``` GetTagRawWait(Kurve1, &Yb[0], 400); GetTagRawWait(Kurve2, &Yb[400], 400); GetTagRawWait(Kurve3, &Yb[800], 400); GetTagRawWait(Kurve4, &Yb[1200], 400); GetTagRawWait(Kurve5, &Yb[1600], 400); for (i=0; i < 1000; i++) { Y = (Yb[2 * i] << 8) + Yb[2 * i +1]; Ymul =(unsigned int) Y*100; printf("Before: %d", Y); printf("After: %d", Ymul); printf("\n"); if (Ymul >= 32768) Ymul = Ymul - 65536; Yb[2*i] = (Ymul <<8); Yb[2 * i +1] = Ymul &0xFF; } ``` I am able to successfully read the values and see the correct results, but I am facing issues when writing them back. Despite my attempts, the written data seems incorrect. Can you suggest what might be going wrong? Thank you for any guidance. *Note: If I attempt to write the values back without making changes (as shown in "Write3 image"), my database remains unchanged, adding to my confusion.

Top Replies

After some independent problem-solving, I successfully resolved the issue. The code below is what I came up with: Yb[2*i] = (Ymul / 256) & 0xFF; // Storing INT as 2 bytes Yb[2 * i + 1] = Ymul & 0xFF;

It seems the issue may come down to how you're handling the multiplication operation. One potential problem is that you're multiplying the values by 100, but you mentioned that your goal is to multiply by 1000. That could be the source of the discrepancy. On the other hand, considering the problem persists even when you're not making changes, it is also a possibility that the issue lies in your write-back operation, rather than with the multiplication. You should check whether your write-back function correctly handles the data you're trying to write back; maybe it doesn't effectively write back negative integers or integers beyond a certain range. Remember, byte-wise operations can be particularly tricky, and it's not always obvious where problems might creep in. You could also consider adding additional error checking to ensure the integrity of the data throughout the process, this will give you more context to where the issue arises.

It looks like you're on the right track with your script, but there are a couple of potential issues with how you're writing the modified integers back to the database. One thing that stands out is in this line: `Yb[2 * i] = (Ymul << 8);`. You're setting the first byte to the shifted value, but you're missing setting the second byte correctly in the next line where you've written `Yb[2 * i + 1] = Ymul & 0xFF;`. Essentially, the issue could be stemming from how you're handling the sign of the multiplied integers, or possibly a pointer dereferencing issue if the buffer isn't being written correctly. It might also be worth checking the data types of the variables you're using—ensure that they fit the expected data sizes in WinCC. You could also try debugging by verifying the values in `Yb` before and after your write operations to confirm what's actually being sent back to the database!

More Replies →

Streamline Your Asset Management
See How Oxmaint Works!!

✅   Work Order Management

✅   Asset Tracking

✅   Preventive Maintenance

✅   Inspection Report

We have received your information. We will share Schedule Demo details on your Mail Id.

To add a comment, please sign in or register if you haven't already..   

Frequently Asked Questions (FAQ)

FAQ: 1. How can I manipulate raw data in WinCC using C-script?

Answer: - You can manipulate raw data in WinCC using C-script by reading the data using functions like GetTagRawWait, performing calculations on the data, and then writing the modified data back to the database.

FAQ: 2. Why am I facing issues when writing back the manipulated data in WinCC?

Answer: - Issues when writing back the manipulated data in WinCC could be due to incorrect handling of data types, improper conversion back to raw data format, or errors in the write operation itself.

FAQ: 3. How can I ensure the correct writing of manipulated data back into the database in WinCC?

Answer: - To ensure the correct writing of manipulated data back into the database in WinCC, make sure to correctly convert the modified integers back to raw data format, handle any data type conversions properly, and ensure that the write operation is performed correctly according to the structure of the database in WinCC.

Ready to Simplify Maintenance?

Join hundreds of satisfied customers who have transformed their maintenance processes.
Sign up today and start optimizing your workflow.

Request Demo  â†’