Multithread inside class method
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I would like to implment a project for teleoperation task in robotics. In C++, I can create a class with method that keeps listening to TCP/IP packets in an independent thread. I would like to create same scenario with matlab. I couldn't find guidelines for this in Matlab. See the following simple class. I would like isDataReceived() to be run in an independent thread. In the main thread, the robot does other stuff. I've seen the callback function in events block but this needs to be triggered. I need this because the received data needs to be maninpulated. Any suggestions?
classdef Robot < handle
properties
m_server = tcpserver("127.0.0.1",5000);
m_buffer = [];
end
methods (Access = private)
function flag = isDataReceived(this)
if this.m_server.NumBytesAvailable
this.m_buffer = read(this.m_server,this.m_server.NumBytesAvailable,"string");
flag = true;
else
flag = false;
end
end
end
end
2 comentarios
Respuestas (1)
Mohammad Sami
el 29 de Jun. de 2021
Editada: Mohammad Sami
el 29 de Jun. de 2021
You can use a combination of parfeval and data queue object to run your TCP server in parallel. You can write a function that reads data from TCP server object. You can then have this function process the data and put the processed output into the data queue. On the data queue you can set a callback which will then do something with the processed output. You can then call this function using parfeval so that it runs in a separate thread. If you want the reading function to keep running, you can use a while loop inside your function. The other option is to parfeval the reading function inside the callback. So once the processed data is received. The callback starts the next read before it does other things.
You can see my earlier answer which gives an example of how you can build something like above. https://www.mathworks.com/matlabcentral/answers/793972-send-and-recieve-data-between-client-and-workers?s_tid=srchtitle
0 comentarios
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!