Using a websocket connection in Matlab

169 ビュー (過去 30 日間)
Adam
Adam 2014 年 3 月 20 日
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

回答 (2 件)

Jeremy
Jeremy 2017 年 4 月 2 日
編集済み: Jeremy 2017 年 4 月 2 日
Check out MatlabWebSocket .
Disclaimer: I am the author.

Luca Malgo
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
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
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 ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by