Using a websocket connection in Matlab
조회 수: 167 (최근 30일)
이전 댓글 표시
Hi
I'm trying to connect via the LAN to a piece of software which can receive commands via a HTML5 websocket connection. Is there any straight forward way to send commands to a websocket command line using Matlab?
UPDATE:
I've found WeSocket4Net which seems like it should do what I need to: http://websocket4net.codeplex.com/documentation
... but struggling to use it. I've added the assembly like this: asm = NET.addAssembly('c:\Reference\WebSocket4Net.dll');
But when I try to create an instance of the WebSocket Class I get an error:
webSockInstance = WebSocket4Net.WebSocket Error using WebSocket4Net.WebSocket No constructor 'WebSocket4Net.WebSocket' with matching signature found.
Adam
댓글 수: 0
답변 (2개)
Luca Malgo
2018년 8월 24일
Hi All! I found a nice workarround to comunicate with a websocket from MatLab! Python (i'm currently using the version 3.6) can comunicate easily with websockets due to the the websocket-client package (pip3 install websocket-client). As much as easily python can stand a websocket, Matlab can call python functions!
Here a bit of exemplary code:
1) Create a function.py file with the function for the websocket comunication
import websocket as ws_lib
def Connection_send_Recieve(WebSocket_Ip,Port,Command):
Str="ws://"+WebSocket_Ip+":"+Port
ws = ws_lib.create_connection(Str)
ws.send(Command)
RecivedData=ws.recv_data()
ws.close()
return RecivedData
You have to save the python file in your working folder, and than you can call the function from matlab, as in the following exemplary code:
Ws_Ip='192.168.0.1';
Port='1234';
Command='CommendFormattedAsYourWebSocketWantToReciveIt'
Result=py.function.Connection_send_Recieve(Ws_Ip,Port,Command);
Now you have an output in python string format. To work with standard matlab string you should convert the result with one of the following commands
Results=string(Results);
Results=char(Results);
For further information you should look at the matlab reference for caling python function Python and Matlab and the python websocket-client references Python Websocket
Good coding to all! Luca M.
댓글 수: 2
Rik
2018년 8월 29일
Comment posted as flag:
I solve the problem using just the matlab code and a simple python library. Easier and cross-platform compatible (other solution are not especially in the 2018 matlab versions)
Rajeswar Reddy G CB.EN.P2AEL16018
2019년 7월 27일
Hi Luca,
I am trying to achieve same functionality.
But when i run the function, i am getting an error.
>> Ws_Ip='192.168.0.1';
Port='1234';
Command='CommendFormattedAsYourWebSocketWantToReciveIt'
Result=py.function.Connection_send_Recieve(Ws_Ip,Port,Command);
Command =
'CommendFormattedAsYourWebSocketWantToReciveIt'
Undefined variable "py" or class "py.function.Connection_send_Recieve".
One more question. I have to use sockets with simulink. Also need to support code generation. Will this method work even with code generation. I use simulink coder or embedded coder or rtwbuild for code generation.
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!