Data transmission issue over TCP IP socket between python device and Matlab

3 vues (au cours des 30 derniers jours)
Hi.
I can transit data between a Raspberry with python 3.7 installed and a PC with Matlab (192.168.1.40) , last release, installed by means a TCP IP Socket and configuring the Matlab PC as server and the pyhton device as client.
Here is the Pyhton client code:
#----- A simple TCP client program in Python using send() function -----
import socket
# Create a client socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
clientSocket.connect(("192.168.1.40",9090))
# Send data to server
data = "HHH"
clientSocket.send(data.encode())
and this is the Matlab server code:
t = tcpip('0.0.0.0', 9090, 'NetworkRole', 'server')
fopen(t)
while true
data = fread(t)
end
For testing purpose I've attempted to transmit only few characters : HHH
The issue is that the transmission works but the other end (on the Matlab side) instead of the right characters (HHH) I see only their ASCII code (72, 72, 72).
If I do the same between two instances of Rasberry python (using a decode() method), I see correctly the data tranmitted over the socket.
Any idea?
Thanks in advance

Réponse acceptée

Shadaab Siddiqie
Shadaab Siddiqie le 22 Fév 2021
From my understanding you want to know why fread returns ASCII code instead of string. This is because tcpip writes data in Unicode format thus you are using fread (which reads data form binary file). To change unicode to string use char:
u = [77 65 84 76 65 66];
c = char(u)
-> c = 'MATLAB'
Also since tcpip and its object properties will be removed. Use tcpclient and its properties instead.
  2 commentaires
Cosimo Mercuro
Cosimo Mercuro le 22 Fév 2021
Hi Shadaab.
Thanks for you reply.
May I use
tcpclient
instead
tcpip
even if the code is a server code?

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by