logo elektroda
logo elektroda
X
logo elektroda

ESP8266 Integer Type 32 Bits vs 16 Bits for PLC Communication

madiz08 1083 7
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17940774
    madiz08
    Level 14  
    ESP8266 Integer Type 32 Bits vs 16 Bits for PLC Communication .

    It deals with the communication of my ESP8266 with a PLC. From the PLC the value of an integer type variable is taken, which is 16 bits. The reading on the serial monitor was ok, until I wanted to display negative numbers. Wanting to check the number of bits, I rigidly typed -1 into the variable and after displaying it in binary form, I noticed that the variable of type INT has 32 bits. This is a big problem for me because when I have a -1 on the PLC, the microcontroller reads it as 65535.
    I tried to declare the variable as int16_t, but the effect is the same. I am using the Settimino.h library to connect to the PLC, it is covered there:
    Code: C / C++
    Log in, to see the code
    .
    What is the problem? I know that the ESP8266 is 32-bit, but I need, to have a 16-bit variable
  • ADVERTISEMENT
  • Helpful post
    #2 17940839
    khoam
    Level 42  
    madiz08 wrote:
    Wanting to check the number of bits, I rigidly typed -1 into the variable and after displaying it in binary form, I noticed that the INT type variable has 32 bits.
    .
    That's right, on the ESP8266 the variable int will be 32 bits - the C standard specifies int as a type of no less than 16 bits, but can be larger depending on the processor architecture.

    madiz08 wrote:
    I tried declaring the variable as int16_t, but the effect is the same
    .
    int16_t on the ESP8266 is certainly a 16-bit variable with sign (-32,768 .. 32,767), and uint16_t without sign (0 .. 65,535).
  • ADVERTISEMENT
  • #3 17940862
    madiz08
    Level 14  
    ESP8266 Integer Type 32 Bits vs 16 Bits for PLC Communication .
    Please see that I still have 32 bits
    ESP8266 Integer Type 32 Bits vs 16 Bits for PLC Communication .
    But when I entered a numerical value greater than 16 bits the displayed entry was not complete. What can I do to make the 16 bit negative value read correctly on the ESP8266, as the value -1 is read as 65355

    Added after 3 [minutes]:

    Immediately....... already ESP8266 Integer Type 32 Bits vs 16 Bits for PLC Communication I don't understand
    now that I have used a write as in the above picture and sent from PLC -1 it reads correctly. I have to go out now but will get back to the subject when I return. I think it is ok :) .
  • ADVERTISEMENT
  • Helpful post
    #4 17940894
    khoam
    Level 42  
    The function Serial.print() from Arduino HAL takes as argument a variable of type int , which is 32-bit in ESP8266, So in this function there is a negative conversion from type int16_t to type int . It is therefore further already displayed as a 32-bit value.
    Code: C / C++
    Log in, to see the code
    .
    If you actually want to see what the variable int16_t looks like in binary format, then you can use a sequence of functions:
    Code: C / C++
    Log in, to see the code
    .
  • ADVERTISEMENT
  • Helpful post
    #5 17941842
    Anonymous
    Level 1  
  • Helpful post
    #6 17943312
    kaczakat
    Level 34  
    In ESP all INTs will be 32bit, In Arduino (surprisingly 8 bit) all INTs will be 16 bit. As you want to be sure, in many platforms the universal designation for a variable is uint8_t, int8_t, uin16_t, int16_t, uint32_t, int32_t, etc. The same is true for INT in various x86 or x64 PC operating systems - if you're getting on with a new one, always on a "DAILY" basis use the sizeof(int) function. Watch, watch, watch!
    Helpful post? Buy me a coffee.
  • #7 17943329
    Anonymous
    Level 1  
  • #8 17943337
    kaczakat
    Level 34  
    Thank you stmx, I have actually never read the C standard. You've probably been reading standards since A or pre-C, but I've been messing around with programming for a while and just did a quick comparison between the 8 and 32 bit systems and it seemed like fun - thanks again.
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around the communication between an ESP8266 microcontroller and a PLC, specifically addressing the handling of integer types. The user initially encountered issues with negative values, as the ESP8266 interprets the int type as 32 bits, leading to incorrect readings of negative integers (e.g., -1 displayed as 65535). Despite declaring the variable as int16_t, the problem persisted until the user discovered that using the write function allowed for correct readings of negative values. Responses clarified that while int16_t is indeed a 16-bit signed integer, the Serial.print() function converts it to a 32-bit int for display, which can lead to confusion. The discussion also touched on the C standard's specifications for integer types and the importance of using specific data types like uint8_t and int8_t for clarity across different platforms.
Summary generated by the language model.
ADVERTISEMENT