- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Hi everyone,

I'm working on a communication platform between Matlab and Fortran using ws2_32. I found a good working example in this link (https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/291484) and i'm changing a few things to make it work with my matlab code.

So, i can communicate my matlab code (client) with my fortran code (server), but i don't know how to get the actual values on fortran. I just get the number of bytes received. I searched a lot on the internet without any helpful result. I mean, if i write a 'hello' string in matlab, for example, i want to be able to print the same string on fortran. Can anyone help me?

I'm new on fortran, so if you need a more detailed explanation, just tell me and i can even send you the fortran code (is not very different with respect to that one uploaded on the link above)

Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Gib,

I'll take a look to onkelhotte's code, cause i was using as example lassytouton's code from the same thread (specifically this is the code, that i recommend as there is not too much information about sockets on fortran https://github.com/lassytouton/LittleDemos/tree/master/Intel%20Visual%20FORTRAN%20TCP%20IP%20Sockets... from my research is the easiest to apply code i've found).

Anyway, to convert the character variables that i've sent from matlab into fortran to real variables, i've used:

*bytesReceived = recv(connection,buffer,(size - bytesReceivedTotal),0)*

*READ(buffer,*)newDial !being buffer the variable i read, a character type variable, and newDial a real variable*

And this should do the work, maybe in a simpler way with respecto to the inkelhotte's code

I attach here the *.f90 file i'm using to read variables from matlab. There are a few things related to the solution in the complex, as *USE simulation_data* that you should ignore. It's an easy to understand code to anyone searching for an example of fortran using winsock code.

For a more complete file (reading and writing example), again, i recommend the lassytouton's code i mentioned above.

Thanks for the replies and the help! I could say my doubts are vanished now.

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

What does the send call on the Matlab side look like? It would probably be easier to start by trying to send an integer, or integer array, rather than a string.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Hey Gib,

For transmitting information **from fortran to matlab**, using the code on fortran, after stablishing the communication:

*bytesSent = send(connection,buffer,(size - bytesSentTotal),0) ! Being 'buffer' the CHARACTER type variable to transmit.*

And on matlab:

*data=fread(t,t.bytesavailable); % Being the variable 't' the tcpip port stablished on matlab*

I have realized that the variable *'data'* receives the buffer data as ascii numbers. So with the use of the function *char()* on matlab i can change it into common characters. So that problem is solved.

For transmitting information **from matlab to fortran**, i'm using the code on matlab:

*fwrite(t,str); % Being the variable 't' the tcpip port stablished on matlab, and str a char type variable*

And on fortran:

*bytesReceived = recv(connection,buffer,(size - bytesReceivedTotal),0) ! Being 'buffer', again, a CHARACTER type variable*

Summarizing, i have realizied that just char type variables can be transmitted either way, and no integer nor real variables directly (this is my final objective). But making a post processing work on each program i think i can make this work done. Using on fortran the *WRITE(charstr,*)realval* and the *READ(charstr,*)realval*, and on matlab the *char();* and *str2num(); *functions, i think i can be able to transform the variables at pleasure and then transmit them.

Any suggestion is obviously welcome.

Thanks for the response!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

This site:

https://au.mathworks.com/help/matlab/ref/tcpclient.write.html

says that the 'write' command can send an array of any data type. It is always sent as bytes (uint8) and the receiver determines how to interpret them. From onkelhotte's code in the thread you referred to above, the function recv4 would be the one to use to transfer integers. The sent bytes are assembled into an array of 4-byte integers.

Note: I haven't tested that code, I have only sent data via TCP/IP from Fortran to C++, and I don't work with Matlab.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Gib,

I'll take a look to onkelhotte's code, cause i was using as example lassytouton's code from the same thread (specifically this is the code, that i recommend as there is not too much information about sockets on fortran https://github.com/lassytouton/LittleDemos/tree/master/Intel%20Visual%20FORTRAN%20TCP%20IP%20Sockets... from my research is the easiest to apply code i've found).

Anyway, to convert the character variables that i've sent from matlab into fortran to real variables, i've used:

*bytesReceived = recv(connection,buffer,(size - bytesReceivedTotal),0)*

*READ(buffer,*)newDial !being buffer the variable i read, a character type variable, and newDial a real variable*

And this should do the work, maybe in a simpler way with respecto to the inkelhotte's code

I attach here the *.f90 file i'm using to read variables from matlab. There are a few things related to the solution in the complex, as *USE simulation_data* that you should ignore. It's an easy to understand code to anyone searching for an example of fortran using winsock code.

For a more complete file (reading and writing example), again, i recommend the lassytouton's code i mentioned above.

Thanks for the replies and the help! I could say my doubts are vanished now.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

In fact the code to receive integers or reals is almost identical to that for receiving characters. In any case you have something that works for you. Well done!

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page