Simulink UDP Send Callback - matlab

How can I access the callbacks for the "UDP Send" Block (from Instrument Control Toolbox) in Simulink?
I want to send UDP data as fast as it is available in Simulink. After a short amount of time running my Simulink model the Simulation stops as "an asynchronous write is already in progress".
I would like to send whenever the status of that block is '{idle}', however I haven't found a way to access this information.
I also tried to write the function myself in a matlab script within the Simulink model, but code generation is not supported for udp class.

As far as I know, the UDP Send block doesn't give you access to that.
However, you can use coder.extrinsic(function_name_1, ...) to build your own MATLAB block.

Related

Simulink Queues: "Unsupported message connection"

I am a very new Simulink user who has been thrown into the deep end of using Simulink + Embedded Coder to generate embedded code for a custom board with an STM32 with CAN communication.
I am desperately attempting to create CAN send/receive queues. The application only transmits CAN messages in a direct 1-to-1 response with a received message: each received CAN packet generates a single CAN packet response. So, my general format is as follows: CAN Rx Interrupt handler collects the message into a Bus, uses a Send block to put the message into a Queue, which is an input to a subsystem which runs from a function generator every 50ms. Inside, I process 3 messages using a do-while loop, each iteration generating its message message. Inside the do-while subsystem is another queue for the created response packets.
When I build the model, I get the following error, which has ZERO results in Google:
"Unsupported message connection between block [path]\Queue and Outport block [path]\TxQueue. Move block [path]\Queue to outside subsystem [path]."
Now, my understanding of Simulink is still shaky at best, but I presume that moving the "queue" block, which I presume DOES the actual enqueuing, to outside the do-while subsystem will result in the first 2 response packets being dropped, right? So why is this block unsupported? Am I completely wrong about how queues work in this accursed "programming language"? My expertise is largely in embedded C, and Simulink is so new and apparently so niche that finding answers to issues like this is bafflingly difficult. Hoping someone here can help me out.

Creating a TCP/IP Server block in simulink

I'd like to have a block in simulink that serves as a tcp server. Ideally the simulation would block when it hits this block, wait until data was transfered to it by the client, and than pass that data out to be used in that step of the simulation. I'd also like the connection to persist across function calls, if possible.
I got this to work in Matlab pretty easily, using the myConnectionObject=tcpip(.) and fread(..) functions. So I figured I could just initialize the connection in the workspace, wrap my freads() into a MATLAB function block in Simulink and I'd be on my way.
However, I'm having trouble getting the tcpip connection object into the function to be visible in Simulink so I can use it with fread(). Passing it through a "From Workspace" block gives an "Unsupported input format..." error.
Thanks,
The Matlab Function block is great for quick simple mathematical functions, but when it comes to initialization or some cleanup at the end of a simulation, a
s function is the right tool. With Start and Terminate you got your places to setup and close down the server within the block.

How can I invoke UART_Receive_IT() automatically when I receive a data?

I am new to STM32 and freertos. I need to write a program to send and receive data from a module via UART port. I have to send(Transmit) a data to that module(for eg. M66). Then I would return to do some other tasks. once the M66 send a response to that, my seial-port-receive-function(HAL_UART_Receive_IT) has to be invoked and receive that response. How can I achieve this?
The way HAL_UART_Receive_IT works is that you configure it to receive specified amount of data into given buffer. You give it your buffer to which it'll read received data and number of bytes you want to receive. It then starts receiving data. Once exactly this amount of data is received, a callback function HAL_UART_RxCpltCallback gets called (from IRQ) where you can do whatever you want with this data, e.g. add it to some kind of queue for later processing in the task context.
If I was to express my experiences related to working with HAL's UART module is that it's not the greatest one for generic use where you don't know the amount of data you expect to receive in advance. In the case of M66 modem you mention, this will happen all the time.
To solve this you have two choices:
Simply don't use HAL functions at all in case of UART, other than the initialization functions. Implement your own UART interrupt handler (most of the code can be copied from handler in HAL) where upon receiving data you place received bytes in a receive byte queue handled in your RTOS task. In this task you implement protocol parsing. This is the approach I use personally.
If you really want to use HAL but also work with a module that sends varying amount of data, call HAL_UART_Receive_IT and specify that you want to receive 1 byte each time. This will work, but will be (potentially much) slower than the first approach. Assuming you'll later want to implement some tcp/ip communication (you mentioned M66 GPRS module) you probably don't want to do it this way.
You should try the following way.
Enable UARTX Rx interrupt in NVIC.
Set Interrupt priority.
Unmask Interrupt request in EXTI.
Then use USARTX Interrupt Handler Function Define in you Vector.
Whenever the data is received from USARTX this function get automatically called and you can copy data from USARTX Receive Data Register.
I would rather suggest another approach. You probably want to archive higher speeds (lets say 921600 bods) and the interrupt way is fat to slow for it.
You need to implement the DMA transmition with the data end detection features. Run your USART in the DMA mode in the circular mode. You will have two events to serve. The first one is the DMA end of thransmition interrupt (then you copy the data from the current tail pointer to the end of the buffer to avoid data override) and USART IDLE interrupt - this will detect the end of the receive.

Read simulink signal data into matlab during simulation

I want to continuously read simulink signal data into the command line while the simulation is running. get_param() seems to be blocking so that doesn't quite work when put in an infinite while loop.
I'm now trying to use a UDP send block but I can't seem to receive data. My UDP block sends data to localhost over remote port 25000 and local port 25001.
In matlab I use the following code but it simply times out with no data
u=udp('127.0.0.1', 25001,'LocalPort',25000);
fopen(u)
fread(u)
fclose(u)
delete(u)
What are my options to continuously read out simulink signal data into Matlab CLI?
Control Simulation Using the "set_param()" command like follows:
set_param('sys','SimulationCommand','WriteDataLogs')
For a working example, type "sldemo_varsize_basic" in the matlab command window. Then above command becomes
set_param('sldemo_varsize_basic','SimulationCommand','WriteDataLogs')
If you set the simulation time to sufficiently large and start the simulation, the "simout, simout1", "tout" and "xout" variables are created/updated in the workspace every time you issue the command above.
Unfortunately, I was not able to find a good quality documentation of this feature.
Are you trying to store the value of your model outports DURING simulation? This is not possible because the variables 'simout, simout1", "tout","xout" etc are created only once simulation is OVER.
In order to read/store the value of outports during simulation, you will have to attach an 'Runtime Object' to the outports.
Refer 'Access Block Data During Simulation' in the Simulink documentation or see this link: http://in.mathworks.com/help/simulink/ug/accessing-block-data-during-simulation.html?s_tid=gn_loc_drop
Hope it helps :)
This question has already been answered here using RunTime Objects as I have described above:
https://stackoverflow.com/a/17006419/6580313

MATLAB/Simulink serial send

I am using Simulink to communicate with a serial device. I am trying to use the Serial Send block to send a value to the device. If I try to use a 'traditional' source (such as the Constant block) to send data, I get the following error.
The block 'Serial Send' cannot be assigned a continuous sample time.
How can I send a non-continuous signal?
You have to convert signal to discreate. First put your data to Quantizer than output from Quantizer connect "Zero-Order hold " now you send data to serial send
Per a discussion on the MATLAB mailing list (source):
You can set a sample time on the
Constant block. If you double click on
it you will see the sample time is set
to -1. You can also drag in a sample
and hold block. You may want to turn
on sample time colours to make
debugging easier.
Update:
Also, you may want to try using the To Instrument block instead of the Serial Send block. I've never used either, but the main difference I see is that the To Instrument block is designed for sending "simulation data", which might be more compatible with the Constant block. Maybe there's a reason that you can't use the To Instrument block, though.