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

1 view (last 30 days)
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,

Answers (1)

SaiDileep Kola
SaiDileep Kola on 24 Dec 2020
check if this helps, link for the post is here.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!