Serial Port in RTWT (s-function)

2 views (last 30 days)
Martin M.
Martin M. on 6 Aug 2013
Hello,
I'm searching for help. 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 control is in Real Time Windows-Target.
And now to the problem: The Serial Port block in Simulink is not possible in RTWT. Is that right? So I thought, that I could write a s-function. But I have trouble with this. I'd never written a s-function before.
It would be nice, if anybody could help me. I have some code from German Forum, which I used. I'm using Windows 7x64 and MATLAB R2013a.
I compile the code with mex sfun_comport.c Then I built a model with an constant input, the s-function-block and a display. The problem are the lines with "!!" beginning. The command "ssSetPWorkValue" makes problems.
Is here anybody who can help me? I despair. When there gives a better way to communicate via serial port in rtwt, let me know. I am very grateful for any help and every tip.
Ps. sorry for my English.
Many thanks and many greetings
Martin
  1 Comment
Kaustubha Govind
Kaustubha Govind on 6 Aug 2013
Martin: Please provide more information on what the exact problem is - how do the lines that you highlighted fail?

Sign in to comment.

Answers (4)

Martin M.
Martin M. on 7 Aug 2013
I can compile the code. Then I open the model in Simulink. When I press start, the error comes (see picture).
When I uncomment the with "!!" started lines, than the error doesn't come, but the programm doesn't work.

Kaustubha Govind
Kaustubha Govind on 7 Aug 2013
Ah! I think it might be because you are not configuring the number of PWorks on the S-function as described here. Essentially, you need the following line in mdlInitializeSizes:
ssSetNumPWork(1);
Also, is there a reason you are overwriting the PWork value immediately after assigning it, or did you really mean to use two different PWorks?
ssSetPWorkValue(S, 0, &hCom);
ssSetPWorkValue(S, 0, &pcCommPort);

Martin M.
Martin M. on 7 Aug 2013
Many, many thanks for the help. That was the mistake. Now there are three other points that I have to solve.
1. The HANDLE does not close. Normally, it would have closed on "mdlTerminate" when I press stop in the simulation. I have to close every time MATLAB, so that the Serial Port 4 is free.
2. I do not know how I display the received data on the display.
3. I do not know how I can send for example a string.
The first point is but for now the most important. Can someone help me here? That would be great.
Here are the C code and the model, if it helps. http://www1.zippyshare.com/v/51091427/file.html

Martin M.
Martin M. on 8 Aug 2013
Edited: Martin M. on 8 Aug 2013
Problem solved. I can send, receive and close the HANDLER. Here the new code: http://www49.zippyshare.com/v/94711190/file.html
  3 Comments
Martin M.
Martin M. on 8 Aug 2013
Oh sorry, I didn't know it. That was not my intention. I'll be better.
Now, I can read and write and the HANDLER will be also closed. I must only format the received and transmitted data, but that is no prblem.
Now I have another question to use this code in RTWT. I don't know, how I can put it in the RTWT-mode. Is there anybody who can help me?
Is it sufficient to have only the s-function for this? Or have I to write additional?
I want to do as soon as possible so that I can to the actual project (the control/regulation).
Kaustubha Govind
Kaustubha Govind on 8 Aug 2013
Martin: Please create a new question for this.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!