How to Eliminate Trailing Zeros in Twincat 3 String Transmission via TCP/IP

Question:

I am utilizing Twincat 3 to transmit strings via TCP/IP. In this setup, the server functions as a sensor while my PLC acts as the client. Upon noticing that the sensor was unresponsive to my commands, I decided to create a Python server to examine the format of the transmitted strings. An observation I made was the presence of trailing zeros when the strings were displayed in Python. However, when I re-routed the received data from Python back to Twincat, the trailing zeros were absent. Is there a method to eliminate these trailing zeros before transmitting the string in Twincat? I attempted using the sLRTrim function like so: sLRTrim := F_RTrim( F_LTrim('string')), but it did not yield the desired result. Furthermore, I attempted defining the variable as a String(22), but doing so caused the last string to be replaced by a trailing zero.

Top Replies

One way to approach this is through brute force: check if the string ends with a zero, and if so, decrease the string length by 1. This can be done in a loop by checking "while length(string) > 1". It is likely that TwinCAT offers string-handling functions such as MID and LENGTH that can be utilized for this purpose.

Oops, the loop needs to be adjusted. It should be written as "While length of string is greater than 1 and the second-to-last character of the string is '0'...".

It sounds like you're running into some quirks with string handling in Twincat. Instead of trying to trim the string with sLRTrim, you might want to use the `F_Trim` function, which is designed to handle both leading and trailing spaces or zeros more effectively. Also, make sure that when you're defining the string length, you're not including those trailing zeros in your memory allocation. If you're still getting those trailing zeros in Twincat, try checking how the string is being formatted before it's sent; sometimes, issues can arise from how the string is built or serialized. Good luck!

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. Why are trailing zeros appearing in the transmitted strings when using Twincat 3 for TCP/IP communication?

Answer: Trailing zeros may appear due to the data format or processing methods used in the transmission. It's important to understand how the data is being handled and formatted at each stage of the communication process.

FAQ: 2. Is there a specific method to eliminate trailing zeros before transmitting a string in Twincat 3?

Answer: One method to eliminate trailing zeros is by using string manipulation functions like sLRTrim. However, it's essential to ensure that the function is applied correctly and that the data type and length of the string variable are set appropriately to avoid unexpected results.

FAQ: 3. Why did defining the variable as String(22) cause the last string to be replaced by a trailing zero?

Answer: Defining the variable as String(22) may result in unexpected behavior if the length specified does not match the actual length of the string being transmitted. Ensure that the variable length is sufficient to accommodate the entire string without truncation or adding extra characters such as trailing zeros.

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  â†’