Serial Port Real-Time Windows Target
Show older comments
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
Nguyen Huy
on 1 Nov 2018
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
Accepted Answer
More Answers (0)
Categories
Find more on Simulink Desktop Real-Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!