串口实时窗口的目标

6视图(30天)
马丁·M。
马丁·M。 2013年8月8日
移动: 斯蒂芬妮施瓦兹 2023年4月27日
你好,
我写了一个函数来处理串行端口。我可以打开和关闭串口,读和写。阅读和写作需要改进和格式化,但这不是这样的一个大问题,不急。
我从来没有写一个函数,使用过RTWT。我的知识在Matlab / Simulink是万博1manbetx不太好,但是我给我最好的。
我在做一个项目在德国上大学。这个项目是建立一个自主车辆和个人电脑之间的通信。PC应当规范车辆使用仿真软件。万博1manbetx通过XBee-Modules无线电通信发生。优先级越高的部分是开发的监管。
规定必须在实时Windows-Target。我使用Windows 7专业x64和MATLAB R2013a。
这是我的代码:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#定义S_FUNCTION_NAMEsfun_comport
#定义S_FUNCTION_LEVEL2
#包括“simstruc.h”
#包括“windows.h”
#包括“Strsafe.h”
#定义MDL_START
#如果定义(MDL_START)
/ *功能:mdlStart = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /
静态空白mdlStart (SimStruct *年代)
{
处理hCom;
DCB DCB;
COMMTIMEOUTS uCtm;
char pcCommPort [10];
uint8_T相称= 4;
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StringCbPrintfA (&pcCommPort (STRSAFE_LPSTR),
sizeof (pcCommPort),
“COM % d”,
一致);
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
hCom = CreateFileA (pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
空,
OPEN_EXISTING,
0,
零);
如果(hCom = = INVALID_HANDLE_VALUE)
{
ssSetErrorStatus(年代,“com端口konnte永远geoeffnet了!”);
返回;
}
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SecureZeroMemory (dcb sizeof (dcb));
memset (dcb 0 sizeof (dcb));
dcb。DCBlength = sizeof (dcb);
dcb。波特率= (uint16_T) 9600;
dcb。ByteSize = 8;
dcb。平价= NOPARITY;
dcb。StopBits = ONESTOPBIT;
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uCtm。ReadIntervalTimeout = MAXDWORD;
uCtm。ReadTotalTimeoutMultiplier = 0;
uCtm。ReadTotalTimeoutConstant = 0;
uCtm。WriteTotalTimeoutMultiplier = 0;
uCtm。WriteTotalTimeoutConstant = 0;
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
如果((! SetCommState (hCom dcb)) | (!SetCommTimeouts (hCom &uCtm)))
{
CloseHandle (hCom);
ssSetErrorStatus(年代,“Konfigurationsfehler des com港口!”);
返回;
}
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ssSetPWorkValue(年代,0,hCom);
}
#endif
/ *功能:mdlOutputs = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /
静态空白mdlOutputs (SimStruct * S, int_T tid)
{
const real_T *情况= (const real_T *) ssGetInputPortSignal (S, 0);
real_T * y0 = ssGetOutputPortSignal (S, 0);
处理hCom =(处理*)ssGetPWorkValue (S, 0);
uint32_T read_cnt;
uint32_T write_cnt;
char getbuffer [] =“测试”;
char putbuffer [] =“死亡是静脉测试\ r”;
int我;
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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);
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
睡眠(3000);
}
#定义MDL_UPDATE
#如果定义(MDL_UPDATE)
/ *功能:mdlUpdate = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /
静态空白mdlUpdate (SimStruct * S, int_T tid)
{
}
#endif
/ *功能:mdlTerminate = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /
静态空白mdlTerminate (SimStruct *年代)
{
CloseHandle((处理*)ssGetPWorkValue(年代,0));
}
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
这将是伟大的,如果有人可以帮助我工作RTWT中的代码。
许多感谢和问候
马丁
1评论
阮驾车
阮驾车 2018年11月1日
你好马丁,我是学生在越南,我也对你的项目很感兴趣。你能通过邮件发送这个项目给我。对不起,我的英语:)非常感谢! ! !电子邮件: leningrat206@gmail.com

登录置评。

接受的答案

Jan Houska
Jan Houska 2013年8月14日
你不能使用Windows功能实时代码。这包括 它仅仅 , ReadFile , WriteFile , CloseHandle 等。你不应该包括 windows.h 在实时代码。
访问串口Real-Tine Windows目标,请使用 数据包输入 包输出 块和 串行端口 司机。
6个评论
纳文
纳文 2014年7月23日
现在有一个有趣的现象发生。当我直接输出串行数据终端,一切工作正常,没有错过字节。但同样的微控制器之间的扫兴。在另一项实验中,我使用写入文件(从MATLAB命令空间)发送相同的串行数据(164字节的数组)的微控制器,错过了一些字节。然后我每个字节分别发送,使用一个for循环,这次工作。现在我想的问题是随着微控制器本身。

登录置评。

更多的答案(0)