Learn how to use casting to save yourself all kinds of headaches when programming. This video introduces the concept and provides several examples so you can see how easy it is to use.
Casting is a great way to make your coding life so much easier. For example, suppose you want to take an IP address in the usual format and pack it into an unsigned DWord which is what you’ll need for some instructions. You could do a bunch of math and combining and re-arranging to create the properly formatted IP Address, or you can do it the easy way. Using the INIT instruction you can just say you want this to go into the upper byte of D0, this to go into the next lower byte, this into the next byte and this into the lowest byte. That colon is the casting operator and it tells Do-more exactly how we want to transform or ‘cast’ one data type into another using this code. In the SETUPIP instruction for example, you can now use D0 as the IP address. Now your IP Address is easy to maintain and the intent of your code is obvious so when you come back to look at it in the future you will see exactly what you meant to do. Suppose you want to take one bit of an unsigned word and use that to drive an output coil. You could do some math and masking and shifting, etc. But why not do it the easy way? Simply tell Do-more you want the 4th bit – numbering starts at zero – to drive the output? We’re just telling Do-more to transform – or cast - the 4th bit of the word into a bit type to use in this contact. Easy. Suppose you receive a number from some other device in Modbus holding register 10 and you know it is intended to represent a real number. You could do a bunch of math to convert that into a real number format, or, you can use casting to simply tell Do-more to do it for you. Casting is a super-fast shortcut around a lot of programming headaches. The help topic covers every possible way you can cast one thing into another so take a look at that to learn more. And check this out. In the element browser, this little button down here is awesome. Suppose I want to take a V memory element and pull one bit out of it. Look – it shows me the exact format I need to use! How about a taking signed word and pulling out a byte, the upper byte and we want it unsigned. There’s the exact format I need. So when in doubt, use the cast builder – it’s quick and easy. Once you get the hang of casting will quickly become one of your favorite programming tools. And if you need more help with casting, contact AutomationDirect’s free award winning support team during regular business hours. They will be happy to help. And don’t forget the forums. There are lots of automation professionals there that love to share their years of experience. Just don’t post any questions directed at AutomationDirect’s support team there, they don’t monitor the forums on a regular basis. Spend Less, Do More, With AutomationDirect.