Real time module TCP communication cannot receive the correct signal through the command line

조회 수: 1 (최근 30일)
I use the TCP communication module in Real Time, but before sending signals, I must package them into uint8 type signals. "I want to receive these signals in the form of a command line and convert them to the original double type. I have tried using double () to coerce the type conversion, but the im2double () function has failed to obtain the original signal correctly.". Is there any way to obtain it?
t=tcpclient('192.168.7.1',8001);
while(1)
data=read(t,8);
write(t,data);
u=double(data);
end

답변 (2개)

Gaurav
Gaurav 2024년 9월 16일
If you're dealing with converting uint8 data back to double and the im2double() function doesn't work for your scenario, you might need to manually handle the conversion process, especially if the data represents binary-encoded floating-point numbers.
Here's a step-by-step approach to convert uint8 data back to double:
  1. Assume Data Encoding: Determine how the double data is encoded into uint8. A common method is using IEEE 754 double-precision floating-point format, which uses 8 bytes (64 bits).
  2. Reconstruct the Double: Use MATLAB's typecast function to convert the uint8 array back into a double. This function treats the uint8 data as raw bytes and reinterprets them as another data type.
Here's the modified code:
t = tcpclient('192.168.7.1', 8001);
while true
data = read(t, 8); % Read 8 bytes from the TCP connection
write(t, data); % Echo the data back
% Convert uint8 data to double
if length(data) == 8
u = typecast(uint8(data), 'double');
else
warning('Received data is not 8 bytes long.');
end
end
You can read more about typecast function here: https://com.mathworks.com/help/matlab/ref/typecast.html
Hope it helps!

Deep
Deep 2024년 9월 17일
Hi星河,
It sounds like you're trying to convert a sequence of uint8 values received via TCP communication back into a double type in MATLAB. The issue you're facing is due to the way data types are handled in MATLAB. When you use double(data), it simply converts each uint8 value to a double, rather than interpreting the entire sequence as a scalar double value.
To achieve the desired conversion, you should use the typecast function, which allows you to reinterpret the uint8 data as a double without changing the underlying byte representation.
Here's how you can modify your code to correctly convert the received uint8 data into a double:
t = tcpclient('192.168.7.1', 8001);
while true
data = read(t, 8);
write(t, data);
u = typecast(uint8(data), 'double'); % Convert the uint8 vector to a scalar double
end
This approach should correctly convert your uint8vector into a scalar double value, as intended. For more detailed information on this conversion method, you can refer to following resources:
I hope this helps!

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by