7 posts in this topic

Good afternoon,

  I need to convert a UINT (16-bit unsigned integer) to a DINT ( 32-bit signed integer).

  Apparently we can't directly assign a UINT variable's value to a DINT's or vice versa. I also couldn't find a way of casting UINT as DINT.

  The reason why I need to do it is because I'm receiving data from a socket. The RXLength is a UINT but in order to scroll through  the data array (USINT[1..1])  I need to use a DINT as the array's index, but I can't compare the array index to the array's length because they are incompatible types.

  Is there a cast statement similar to:

      myDINTvar := (DINT) myUINTvar;

  I also tried the following but it didn't work:

      myDINTvar :=  (myUINTvar * 1);

  Any help will be deeply appreciated.

  

Edited by AdAstra

Share this post


Link to post
Share on other sites

As far as I know, I MOV instruction with the UINT as the source and the DINT as the destination should work.

It's bit-for-bit copy and because it wouldn't affect bit 31 of the DINT (the sign bit), the value should valid.

myDINTvar := myUINTvar;

There shouldn't be a reason to try to cast the the variables unless I'm missing something.

Why does the index have to be a DINT? How many members are in the array?

Share this post


Link to post
Share on other sites

Thank you for your reply.

The SOCKET_READ instruction block returns a USINT array with the message received (data parameter), as well as a UINT (RxAddr parameter) with the size of this message. I have to set up a loop to read the message content character by character, and to do that I have  to access each individual item in the array. Unfortunately the array indexes are DINT, therefore the variable used to address each array position needs to be DINT. In order to compare the indexer variable to the upper bound of the array I have to compare a DINT with a UINT, which apparently is not possible.

Unfortunately, the MOV instruction block is also picky about data types, so it didn't work

 Error    1    myDINTvar: Expecting a UINT/WORD type variable   

Share this post


Link to post
Share on other sites

Try the COP instruction instead of MOV. 

Share this post


Link to post
Share on other sites

Awesome! It's exactly what I was looking for.

By the way, you wouldn't happen to know how I could use that instruction from within a Structured Text Block? I only know how to use it in a Ladder diagram.

Thank you for all your help!

Share this post


Link to post
Share on other sites

Add it to a rung, select it, and hit "F1".  That will take you to the help page for that instruction.

Edited by Joe E.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now