Matlab integration

242 views
Skip to first unread message

C Diaz

unread,
Feb 8, 2012, 12:31:02 PM2/8/12
to NModbus
Hi there,

Currently I use matlab to control a big system for pressure control.
I'm updating all my software to 64x and one of the drivers is only
available for 32x. The provider of this piece of equipment suggested
integration of the NModbus protocol.
Right now I'm still working on windows xp 32x. I downloaded the
package NModbus_net-3.5_1.11.0.0-source.zip and
NModbus_net-3.5_1.11.0.0.zip from https://code.google.com/p/nmodbus/downloads/list,
and have been trying to load the dll's in there to Matlab... but every
attempt has been unsuccessful. I've been trying to find the headers
(.h) required by Matlab, and even write it myself... but I haven't
been able to load the library and use the functions.

Any help here would be very appreciated.

Thanks,

Camilo

Victor Rocha

unread,
Feb 9, 2012, 5:53:16 AM2/9/12
to nmodbus...@googlegroups.com
Hello dear C Diaz

Notice that this dll´s from NModbus are .Net assemblies written in C#, also known as "managed libraries".
Header (.h) files are used normally for "native libraries" which are written in C/C++ etc.

Have you studyied integration of .Net libraries to Matlab?
I do not personally know if it is possible, but you should look for it.
I googled "loading .net assembly matlab" and there are many hits, please take some time to look for it.
This can be some starting point:
http://www.mathworks.com/help/techdoc/matlab_external/brpcay8-1.html

Get back to us if you cannot succeed loading this .Net assembly, we could work around other integration methods.
If milisseconds-performance is not 100% required you could try alternative integration methods, assuming you are a software programmer or can learn how to do it:
- wrap NModnus as webservices and load as SOAP
- write a program to integrate via SQL
- write a program to integrate via TXT
- etc.

wish you good luck (or better, good studying)!
best regards



--
You received this message because you are subscribed to the Google Groups "NModbus" group.
To post to this group, send email to nmodbus...@googlegroups.com.
To unsubscribe from this group, send email to nmodbus-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nmodbus-discuss?hl=en.


Victor Rocha

unread,
Feb 9, 2012, 5:56:50 AM2/9/12
to nmodbus...@googlegroups.com

Camilo Diaz-Botia

unread,
Feb 9, 2012, 12:18:49 PM2/9/12
to nmodbus...@googlegroups.com
Hey Victor,

Thank you very much for your help!! I really appreciate it.
I'm not familiar with the .NET assemblies yet, I also figured out yesterday that should be my starting point of studying if I want to have this working.

I know that the latest versions of matlab support managed libraries.

I'll keep you posted,

Thanks again,

Camilo

Camilo Diaz-Botia

unread,
Feb 9, 2012, 6:26:34 PM2/9/12
to nmodbus...@googlegroups.com
Hi Victor,

So I've been trying to create the connection to my device, but I keep getting the error

??? Message: No connection could be made because the target machine actively refused it
192.168.1.1:502

when I try to create the tcpClient. I've tried a bunch of things already but nothing works.

Any idea?

Thanks!!!

Camilo

Victor Rocha

unread,
Feb 10, 2012, 5:44:31 AM2/10/12
to nmodbus...@googlegroups.com
Hello Camilo,

The TcpClient is from .NET framework and not from Nmodbus, so it is a more general class of errors. You should be 100% confident that the device is indeed working and able to communicate, and also debug around your OS, network etc. I don´t know if Matlab would have any restriction to TCP, please check mathworks´ documentation.

What I can understand is that if you follow the hint in the error message, you should check by any means that your computer really can connect to the external device.

I mean:
- is the device in Modbus mode, (not using manufacturer´s proprietary driver)?
- can you try the same computer, device, setup etc. to check it with another Modbus simpler program (like modpoll for example) and get the data?
- are windows and matlab 100% unblocked by firewalls, network policies etc ?

if we have "yes" for all above, I suggest you look for different examples for Matlab+TCP using TcpClient, maybe there are subtle differences.

good luck, post back :-)

Camilo Diaz-Botia

unread,
Feb 10, 2012, 8:15:03 PM2/10/12
to nmodbus...@googlegroups.com
Hi Victor,

It's me again. So, I was able to create the tcpClient using the comand System.Net.Sockets.TcpClient.

Then I created the slave and tried to make the connection but didn't work. I'm afraid I'm not using the proper functions in the Modbus assembly. Here is what I'm doing:

modbusObj = NET.addAssembly('C:\...\bin\net\Modbus.dll');
systemObj = NET.addAssembly('System');

%%
% Create Tcp Client
echotcpip('on',5500);
tcpClient = System.Net.Sockets.TcpClient(System.Net.Dns.GetHostName(), 5500);

%%
% Create Modbus Tcp Slave
% Create tcp Listener
ipAddress = System.Net.IPAddress.Parse('192.168.1.10');
tcpListener = System.Net.Sockets.TcpListener(ipAddress, 502);

tcpSlave = Modbus.Device.ModbusTcpSlave.CreateTcp(2, tcpListener);

%%
% Create Modbus Tcp Master Connection
modbusMasterConnectionObj = Modbus.Device.ModbusMasterTcpConnection(tcpClient, tcpSlave);


The port 502 is the port for modbus mode of my device, and it's address is 192.168.1.10.

Am I using the correct functions?

Thanks a lot for your help!!!

Camilo

Camilo Diaz-Botia

unread,
Feb 13, 2012, 6:24:39 PM2/13/12
to nmodbus...@googlegroups.com
Hi Victor,

I finally managed to communicate with the device!! so cool.

Now I just need the Modbus.dll compiled for a 64bits system. Could you help me out with that?

Thanks a lot,

Camilo

Victor Rocha

unread,
Feb 11, 2012, 4:41:54 AM2/11/12
to nmodbus...@googlegroups.com
Camilo,

Don't create the TCP Slave. It is used when you want the PC to be a slave, to an external Master.
In this case you must create a Master; once yoy have it instantiated, you can already issue modbus commands (read register, write registers etc.)
This is an old sample, which syntax maybe changed a little, but it will give you the overall Idea:

using (TcpClient client = new TcpClient("127.0.0.1", 502))
{
	ModbusIpMaster master = ModbusIpMaster.CreateTcp(client);

	// read five input values
	ushort startAddress = 100;
	ushort numInputs = 5;
	bool[] inputs = master.ReadInputs(startAddress, numInputs);

	for (int i = 0; i < numInputs; i++)
		Console.WriteLine("Input {0}={1}", startAddress + i, inputs[i] ? 1 : 0);
}

// output: 
// Input 100=0
// Input 101=0
// Input 102=0
// Input 103=0
// Input 104=0



Try to get it adapted to Matlab...
see you
Victor

Victor Rocha

unread,
Feb 14, 2012, 7:07:36 PM2/14/12
to nmodbus...@googlegroups.com
Hi Camilo,

it is great that things worked.
.NET Assemblies (like Modbus.dll) are platform-independent.
That means, just install .NET framework in your target 64-bit system and that should be fine (no "32-bit vs 64-bit" Modbus.dll).
Anything different than that, should be addressed to Matlab tech support.

that's it for now, i guess! :-)
best regards.
Reply all
Reply to author
Forward
0 new messages