Serial Port Real-Time Windows Target

Hello,
I wrote a s-function to work with the Serial Port. I can open and close the Serial Port and read and write. The reading and writing have to be improved and formatted, but that is not such a big problem and has no hurry.
I have never written an s-function and worked with RTWT before. My knowledge in Matlab/Simulink is not so good, but I give my best.
I'm working on a project at college in Germany. The project is to build a communication between an autonomous vehicle and a PC. The PC shall regulate the vehicle using SIMULINK. The radio communications happens via XBee-Modules. The part with the higher priority is to develop the regulation.
The regulation have to be on Real Time Windows-Target. I'm using Windows 7 Professional x64 and MATLAB R2013a.
Here is my code:
*****************************************************
#define S_FUNCTION_NAME sfun_comport
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "windows.h"
#include "Strsafe.h"
#define MDL_START
#if defined(MDL_START)
/* Function: mdlStart =================================================*/
static void mdlStart(SimStruct *S)
{
HANDLE hCom;
DCB dcb;
COMMTIMEOUTS uCtm;
char pcCommPort[10];
uint8_T comport = 4;
//-------------------------------------------------------------------
StringCbPrintfA((STRSAFE_LPSTR)&pcCommPort,
sizeof(pcCommPort),
"COM%d",
comport);
//-------------------------------------------------------------------
hCom = CreateFileA (pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
ssSetErrorStatus(S,"COM-Port konnte nicht geoeffnet werden!");
return;
}
//-------------------------------------------------------------------
SecureZeroMemory(&dcb, sizeof(dcb));
memset(&dcb, 0, sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = (uint16_T)9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
//-------------------------------------------------------------------
uCtm.ReadIntervalTimeout = MAXDWORD;
uCtm.ReadTotalTimeoutMultiplier = 0;
uCtm.ReadTotalTimeoutConstant = 0;
uCtm.WriteTotalTimeoutMultiplier = 0;
uCtm.WriteTotalTimeoutConstant = 0;
//-------------------------------------------------------------------
if ((!SetCommState(hCom, &dcb)) | (!SetCommTimeouts(hCom, &uCtm)))
{
CloseHandle(hCom);
ssSetErrorStatus(S,"Konfigurationsfehler des COM-Ports!");
return;
}
//-------------------------------------------------------------------
ssSetPWorkValue(S, 0, hCom);
}
#endif
/* Function: mdlOutputs =================================================*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
const real_T *u0 = (const real_T*) ssGetInputPortSignal(S,0);
real_T *y0 = ssGetOutputPortSignal(S,0);
HANDLE hCom = (HANDLE*)ssGetPWorkValue(S, 0);
uint32_T read_cnt;
uint32_T write_cnt;
char getbuffer[] = "test";
char putbuffer[] = "Dies ist ein Test\r";
int i;
//---------------------------------------------------------------------
read_cnt = sizeof(putbuffer)-1;
ReadFile(hCom, putbuffer, read_cnt, &read_cnt, NULL);
//---------------------------------------------------------------------
write_cnt = sizeof(putbuffer)-1;
WriteFile(hCom, putbuffer, write_cnt, &write_cnt, NULL);
//---------------------------------------------------------------------
Sleep(3000);
}
#define MDL_UPDATE
#if defined(MDL_UPDATE)
/* Function: mdlUpdate ================================================*/
static void mdlUpdate(SimStruct *S, int_T tid)
{
}
#endif
/* Function: mdlTerminate ===============================================*/
static void mdlTerminate(SimStruct *S)
{
CloseHandle((HANDLE*)ssGetPWorkValue(S, 0));
}
*****************************************************
It would be great, if anybody can help me to work the code in RTWT.
Many thanks and many greetings
Martin

1 Comment

Hello Martin, I'm student in Vietnam, i am also interested in your project. Would you be able to send this project to me by mail. sorry for my english :)) Thanks very very much !!! Email: leningrat206@gmail.com

Sign in to comment.

 Accepted Answer

Jan Houska
Jan Houska on 14 Aug 2013
You cannot use Windows functions in real-time code. This includes CreateFile, ReadFile, WriteFile, CloseHandle, and such. You should never include windows.h in your real-time code.
To access serial port in Real-Tine Windows Target, please use the Packet Input and Packet Output blocks and the Serial Port driver.

6 Comments

Martin M.
Martin M. on 14 Aug 2013
Moved: Stefanie Schwarz on 27 Apr 2023
Thank you for these information. Our group didn't know that about Serail Port and RTWT. So, the written s-function could be discarded?
We have the Real-Time Windows Target installed. You said, that I have to use the Packet Input block, the Packet Output block and the Serial Port driver. I find the both blocks, but I can't find the Serial Port driver. Can you say me where it is? Or is that the "serial configuration" in Instrument Control Toolbox?
And is there any instructions how I can build the complete Serial Port in RTWT, so that I can configurate the port and the baudrate and also read and write?
Is there in one of the examples an example with the Serial Port?
It would be great if you can help me.
Many, many thanks for your information.
Jan Houska
Jan Houska on 15 Aug 2013
Moved: Stefanie Schwarz on 27 Apr 2023
In the Packet Input or Packet Output block, select Install New Board and choose Standard Devices, Serial Port. You will get a dialog allowing you to set baud rate etc. You can then just select the installed board in all other blocks that need to use the same port with the same parameters.
Martin M.
Martin M. on 16 Aug 2013
Moved: Stefanie Schwarz on 27 Apr 2023
Ah, ok. The configuration of the Serial Port isn't difficult. The composition is so that I am testing with two PCs. There are XBee-modules connected at both PCs. So I can transmit and receive data.
One PC has a terminal-software and the other PC has Simulink. It is only for testing purposes. Now it is so that I want to send a string, "hello" for example, from Simulink-PC to the terminal-PC. But I don't know how I can do it. How is the configuration? I think, that I have to use the Stream Output block for this test. What should I do with the input of the Stream Output block? Or is it possible to fill the line "Format String" (Block Parameters of Stream Output) with the to transmitting text? I can't connect the input with a constant block, or is it wrong?
When I understand that all, then I can programm the real important thing.
It is so, that I must receive formatted data like this string: 1.23;4.44;8.90; Then I have to decompose this string in three double-results and send it in the regulation. The regulated result is than an double too. This result have to transmitted to the other XBee-module.
Sorry for my English, it isn't best.
There seems to be a problem with the Packet output block while working in RTWT. When I try to send 7 bytes as a packet through Packet output block(sample rate 0.01 and Baud Rate:9600) to a micro controller which then prints the data to a Terminal,everything works fine. The same thing when done for 164 odd bytes with an increased sample time(1 sec) and keeping the baud rate the same, some bytes are missing on the terminal. why is that, is there a limitation on the number of bytes in a packet which can be sent correctly ?
I think there is a buffer in the UART chip used for serial communication. The buffer might overflow when you try to send too many bytes. Try to break up the message into several groups and wait a while between each group.
BTW, I don't think the serial communication is truly real time in the RTWT. I posted a question few days ago to describe what I observed.
Now there is an interesting phenomena happening. When I output the Serial data directly to the terminal, everything works fine, no byte is missed. But the same thing with the micro controller in between plays the spoilsport. In another experiment, I used fwrite(from MATLAB command space) to send the same serial data(array of 164 bytes) to the micro controller, some bytes got missed. Then I sent each byte separately, using a for loop,and this time it worked. Now the problem I suppose is with the micro controller itself.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!