How can I receive and display data using TCP\IP client

Hello there, I have a Simunlink model with the TCP\IP client blockset. I would like to modify the client in a way that I can display the data from the server. My project has to be in such as way that once the communication is established both end can send and receive data. Regards

Risposte (2)

Hi Walter,
Thanks for the help. I'm sorry for my duplicated question I have deleted the other one. Actually I don't have the intrument control toolbox. That's why I have to manage with the S-function block. I have only seen a client be used to send the data only is there a way to modify it to be able to receive a data from the server? I mean as soon as there is a connection it can not only send the data but also receive it from the server. Regards,

8 Commenti

Hi Walter, Thanks for the link I wasn't using that one I got a model form my supervisor. Anyway I have tried the link but it is not working with the latest version of Matlab. I am using 2015b version. I have the client model I create in Visual Studio and it's working well I would like to convert it as a MEX file for and S-function block do you know how I can convert it? Many Thanks
If you are not using the Instrument Control Toolbox and are not using the blocks from the File Exchange then we have no idea how your model is using TCP.
I have no experience with using Microsoft compilers. I have read a small amount about Mex files, but I have never constructed any.
You might find it easier to use loadlibrary()
Or, considering that you are using MS Windows, you might find it easier to use ActiveX.
If the goal is to deploy to a device, then you need to use blocksets provided by Mathworks for code generation to that target, or you need to do a whole bunch of work to create blocks compatible with the target.
Hi Walter,
Thanks again for your answer. I have used the template cpp file from matlab to create the MEX file. Unfortunately I have this errors
Error using mex
Creating library UseFunc.lib and object UseFunc.exp
UseFunc.obj : error LNK2019: unresolved external symbol "void __cdecl UseFun_SentData(struct SimStruct_tag *,double const *,int)"
(?UseFun_SentData@@YAXPEAUSimStruct_tag@@PEBNH@Z) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)"
(?mdlOutputs@@YAXPEAUSimStruct_tag@@H@Z)
UseFunc.obj : error LNK2019: unresolved external symbol "void __cdecl UseFun_CloseSock(struct SimStruct_tag *)"
(?UseFun_CloseSock@@YAXPEAUSimStruct_tag@@@Z) referenced in function "void __cdecl mdlTerminate(struct SimStruct_tag *)"
(?mdlTerminate@@YAXPEAUSimStruct_tag@@@Z)
UseFunc.obj : error LNK2019: unresolved external symbol "void __cdecl UseFun_StartSock(struct SimStruct_tag *)"
(?UseFun_StartSock@@YAXPEAUSimStruct_tag@@@Z) referenced in function "void __cdecl mdlStart(struct SimStruct_tag *)"
(?mdlStart@@YAXPEAUSimStruct_tag@@@Z)
UseFunc.mexw64 : fatal error LNK1120: 3 unresolved externals
Do you have any idea how I can fix it? I have been trying for long
Many thanks
Sorry, I am not familiar with that source code.
Hi Walter,
I have managed to send and receive the data from the server with the S-function block. My problem now is how can I declare this data at the output port of the S-function this is the part to receive data :
int sRead(std::string str)
{
int DataIn;
char recvbuf[]= "";
int recvbuflen = '\0';
DataIn = recv(ConnSock, recvbuf, recvbuflen, 0);
if (DataIn > 0)
ssPrintf("Bytes received: %d\n", DataIn);
else if (DataIn == 0)
ssPrintf("Connection closing...\n");
else {
ssPrintf("recv failed: %d\n", WSAGetLastError());
closesocket(ConnSock);
WSACleanup();
}
return DataIn;
}
For the mdloutput how to write y to show the value receive?
Thanks for the help:
/* Outputs of the Sfunction */
static void mdlOutputs(SimStruct *Sim, int_T timed)
{
int_T itim;
InputRealPtrsType pointers = ssGetInputPortRealSignalPtrs(Sim, 0);
int_T nuSam = ssGetInputPortWidth(Sim, 0);
// OutputRealPtrsType pointers = ssGetOutputPortRealSignalPtrs(Sim, 0);
// The y output should be showing the DataIn form the code above
int_T y = ssGetOutputPortWidth(Sim, 0);
Hello again,
This is what I tried to do after going through some examples:
/* Outputs of the Sfunction */
static void mdlOutputs(SimStruct *Sim, int_T timed)
{
int_T itim;
InputRealPtrsType pointers = ssGetInputPortRealSignalPtrs(Sim, 0);
int_T nuSam = ssGetInputPortWidth(Sim, 0);
// Writing the data on the output port
real_T *y = ssGetOutputPortRealSignal(Sim,0);
int_T DataIn = ssGetOutputPortWidth(Sim, 0);
int_T myMessage = ssGetOutputPortWidth(Sim, 0);
int_T fp = ssGetOutputPortWidth(Sim, 0);
for (int i = 0; i < DataIn/sizeof(float); i++){
y[i]= myMessage.fp[i];
}
The error message now read:
Building with 'Microsoft Windows SDK 7.1 (C++)'.
Error using mex
client64sr.cpp
C:\Users\MfeIss\Documents\Simp_Client\client64sr.cpp(109) : warning
C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of
data
C:\Users\MfeIss\Documents\Simp_Client\client64sr.cpp(229) : error C2228:
left of '.fp' must have class/struct/union
type is 'int_T'
Pleease guys any help will be appreciated.
Many thanks
Why are you taking the output port width of the same thing multiple times? If you had varied the 0 to something else I might have understood.
You made myMessage a number which is just the width of some port, but you are trying to access a "fp" field of that width. Perhaps you need another ssGetOutputPortRealSignal or something similar?

Accedi per commentare.

Richiesto:

il 25 Gen 2016

Commentato:

il 14 Apr 2016

Community Treasure Hunt

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

Start Hunting!

Translated by