How to share the same file handler between 2 mex functions. For Simulink block usage

1 回表示 (過去 30 日間)
Antonio Flores Caballero
Antonio Flores Caballero 2020 年 1 月 25 日
回答済み: SaiDileep Kola 2020 年 12 月 24 日
Greetings,
I am facing some new to me, the creation of computer runable Simulink blocks;
at this moment all blocksets that I developed are intended for external targets,
I have a specific requirement regarding a USB native media that resembles a COM Port;
it transmits and receives data on binary format, but it maintains the packet size, but sometimes
the first data are double and followed by some uint32 at both time... other the first bytes are single datatype...
this strange form of communication is already performed in two separate mex functions; I programmed
the mex functions and each one (receiver and sender) works well separately. I perfectly know how to deal with mdlStart, mdlOutputs, mdlInitialization... this is not the problem.
The problem is that these mex functions needs to share the same "handle" for the serial COM; in the original C code this is a single program; but in Simulink, each mex function acts as different ones (obviously). I know that I can develop a single block that performs opening the serial COM (I am using Windows platform and CreateFile, ReadFileEx, WriteFileEx... windows API functions), and creates the desired inputs and outputs in a block, but this is pretty annoying or ugly solution.
The ideal solution is to create a block in order to setup and open the COM port (as well to close it in the mdlTerminate with a "CloseHandle(hSerial);", another block in order to receive data and another one in order to send data packets.
I try using a single mex function that perfoms all actions, but using 3 different blocks in a library that uses the same mex function per block; but these blocks act separately and the variable "HANDLE hSerial" is different for each block,
How can I share the handle (hSerial) definition in order to properly execute these functions in separate blocks?
/* on mdlOutputs */
/* Read the serial - On USB_VCP_pcRead mex function and block */
ReadFileEx(hSerial, read_data, contar_bytes, &overlapped, ReadCb);
...
and
...
/* Writes the serial - On USB_VCP_pcWrite mex function and block */
WriteFileEx(hSerial, send_data, contar_bytes_send, &overlapped, WriteCb);
...
and
...
/* mdlStart */
/* Opening and Configure the Serial - On USB_VCP_pcSetup mex function and block */
hSerial = CreateFile(com, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
Thanks in advance for your help and attention,
Best Regards mates,

回答 (1 件)

SaiDileep Kola
SaiDileep Kola 2020 年 12 月 24 日
check if this helps, link for the post is here.

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by