フィルターのクリア

Serial Port Real-Time Windows Target

6 ビュー (過去 30 日間)
Martin M.
Martin M. 2013 年 8 月 8 日
移動済み: Stefanie Schwarz 2023 年 4 月 27 日
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 件のコメント
Nguyen Huy
Nguyen Huy 2018 年 11 月 1 日
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

サインインしてコメントする。

採用された回答

Jan Houska
Jan Houska 2013 年 8 月 14 日
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 件のコメント
Jason Chen
Jason Chen 2014 年 7 月 21 日
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.
Naveen
Naveen 2014 年 7 月 23 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTarget Computer Setup についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by