Shamil
Shamil

Reputation: 1

Inconsistent Float Values When Sending Data via TCP/IP from Simulink to Raspberry Pi

I'm experiencing two distinct issues with TCP/IP data transmission from Simulink to a Raspberry Pi. I'm using the same model and code to send data, but the output differs based on the input values. Case 1: Sending Constant Values When I send a constant float value of 2.0, I receive an alternating pattern of correct and zero values on the Raspberry Pi:

Received data: (2.0,)
Received data: (0.0,)
Received data: (2.0,)
Received data: (0.0,)

Case 2: Sending Various Float Values With the same setup, when I send various float values, I occasionally receive extremely high values that do not match the expected output:

Received data: (1.23,)  // Expected value
Received data: (6.812718356601242e+17,)  // Erroneous high value
Received data: (2.34,)  // Expected value

Setup Details: Simulink Configuration: The model sends data via a TCP/IP block configured for BigEndian byte order. Expected Behavior: The Raspberry Pi should consistently receive the float values sent from Simulink. Actual Behavior: Depending on the input, I get alternating zeros or occasional very high values.

import socket
import struct
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('192.168.x.x', 47899)) 
server_socket.listen(1)
client_socket, addr = server_socket.accept()
try:
    while True:
        data = client_socket.recv(4)
        if not data:
            break
        unpacked_data = struct.unpack('>f', data)
        print(f"Received data: {unpacked_data}")
finally:
    client_socket.close()

I've verified that the data is correct immediately before the TCP/IP block in Simulink. The issues occur regardless of the endianness configuration or the constant vs. variable nature of the transmitted values.

the attached images concerns the simulink configuration for both cases

Has anyone faced similar issues or can suggest potential causes and solutions? Insights into either case would be greatly helpful. enter image description here enter image description here

Upvotes: 0

Views: 43

Answers (0)

Related Questions