langless28 (Customer) asked a question.

Converting bytes to words with masking

I have an iolink pressure sensor and the process data is 2 bytes long. The first two bits are for process output status and the the remainder 14 bits are for the process value (numeric). These are mapped to "testin206 and testin207" (unsigned bytes) of data on my BRX. I think the correct term is bit offset, so how do I take the two bytes of data and copy them to a single data spot and offset it so it does not factor in the first two bits of testin206?

 

For what its worth, I guess i could map all the "testin" memory spots as words or double words (instead of the current byte). That way i would not have this data across two different memory spots, if that makes things easier.


  • HOST_BobO (AutomationDirect)

    Which is what my initial post did: "MATH N0 = Testin206:SW/4"

     

    I was using a cast to get to a signed word, but my initial concern was endianness. Still not sure whether that is an issue, but the OP said what I suggested didn't work.

    Selected as Best
  • HOST_franji1 (HOST Engineering)

    Please describe "first two bits". Are they the 2 LEAST significant bits or the 2 MOST significant bits? In which BYTE?

     

    Best thing to do is TELL ME what would be the hexadecimal values of the RAW 2 bytes if the ACTUAL Process value is decimal 16, and the Output Status 0 is OFF and the Output Status 1 is ON.

     

    • langless28 (Customer)

      So as I am reading the process data card, its the first two LSB, in the first byte.

       

      Screenshot 2024-10-07 154709

  • ADC_PLC_ENG (AutomationDirect)

    It looks l like you just want to copy 2 bytes to a word and shift the lowest 2 bits out. If so it can be done with MEM copy

    image 

    The lowest 2 bits are not copied and the data starting at Byte0: Bit 2 is copied to Word0:Bit0

     

    image

    Expand Post
      • HOST_BobO (AutomationDirect)

        Partially, maybe. The spec you showed above gave the range as being negative. I don't think this will work with negative values.

      • langless28 (Customer)

        Would it have to be shifted to the MSB range of the new word?

      • HOST_BobO (AutomationDirect)

        For most things, signed values are represented as two's complement, so 11111111_11111111 is -1. Copying 14 bits to the LSB won't preserve that, so yes, data needs to be copied to the MSB, then shifted or divided. The PLC does a signed shift by default, but dividing eliminates the distinction.

      • ADC_PLC_ENG (AutomationDirect)

        BobO is correct. Sorry I did not see your post that you have negative values.

        What is the IO link device part number you are using?

        How the data is represented in bits 2-15 is what is important to know.

      • langless28 (Customer)

        Sorry, yes I may add that i was using two sensors, one was a capacitive sensor with a 0-1625 range. That sensor worked with your statement. the original sensor still does not work like you described. its an IFM PQ3834 sensor. is the info of the data type NOT in the screenshot i sent?

  • HOST_BobO (AutomationDirect)

    Assuming little endian, this should work:

    MATH N0 = Testin206:SW/4

     

    Your data is word aligned, so you can can cast to a signed word, then divide by 4 (rather than shift) to preserve the sign bits in the 15th and 16th bits, then store in a signed word location.

     

    If it's actually big endian, then it'll take more work.

10 of 18