Code covered by the BSD License  

Highlights from
Eurotherm Modbus RS232 Control

5.0

5.0 | 1 rating Rate this file 19 Downloads (last 30 days) File Size: 444 KB File ID: #24696

Eurotherm Modbus RS232 Control

by

 

11 Jul 2009 (Updated )

Reads and writes information to Eurotherm controllers via Modbus RTU protocols.

| Watch this File

File Information
Description

Reads and writes information to Eurotherm controllers via Modbus RTU protocols. Currently only tested with serial RS232, but should work the same with RS485 if wired correctly. So far only tested with Eurotherm 2416 and 3216 temperature controllers but they all use the same language so it should be widely applicable. The code is well commented so you should be able to modify it easily. I've currently only implemented it using read and write n words (codes 3 and 16), but I'd be happy to implement the other features, such as fast status read etc. (code 7). This code is derived from that in CO2gui, but is implemented using the native MATLAB serial interface only.
To use it:
1. Generate the serial object (this one is done on COM7)
serialObject = tempobj(7);

2. Connect to the object:

tempobjconnect(serialObject)

3. Start sending commands!

format:
tempobjcomm(commandType, serialObject, deviceAddress, parameterAddress, values)

e.g. to get the set temperature and the current temperature (process variable) in one go, do:

(assuming a default device address of 1)

data = tempobjcomm('read', serialObject, 1, 1, 2);

where data(1) is the current temperature and data(2) is the set temperature. Note that if your temperature controller has decimal places on the front panel, the outputs using 'read' will be scaled. E.g. if 1 decimal place, 24.2 will be returned as 242.

Higher resolution can be obtained using 'readfullres' instead, and also avoids any scaling issues.

To set a new set temperature:

tempobjcomm('write', serialObject, 1, 2, newTemperature)

'writefullres' can be used again if required for higher resolution. The same scaling issues apply as above for reading temperatures.

UPDATE

A deviceAddress can included in the initial serial object (see notes) for persistence' sake.

A host of sample settings files for Eurotherm controllers are included (with a device address of 1, but these can be easily modified).

MATLAB release MATLAB 7.1.0 (R14SP3)
Other requirements Should be fine on most platforms but there may be issues with byte ordering on non-x86 platforms using readfullres and writefullres - if it doesn't work for you let me know and I can add in a more systematic way on deciding on the byte ordering.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
18 May 2013 Geoffrey Akien

In response to Richard's command (FEX didn't notify me):

1) This is a deliberate feature related to how Modbus works. If you provide a deviceAddress of 0, then this is a broadcast, and modbus devices do not respond to it. So if you wanted to broadcast then then (~~0) would evaluate as true and this section (encapsulating all of the code to do with receiving data) would be run. This does not impact the sending of commands because this is run before the if ~deviceAddress section. I agree that on reflection using a double negative is not the most readable way to have done it, but it does work :).

2. I think what you've done to get round that problem is definitely one way to do it. If all the devices on that two-wire bus are using modbus then you could filter out only the responses from that particular deviceAddress. The whole deviceAddress thing is designed into Modbus exactly for multidrop instruments on the same bus. More info here: https://wiki.univie.ac.at/download/attachments/7503909/2kcomms.pdf

24 Sep 2012 Richard

I have this code working with a Eurotherm 3216 over two-wire RS-485. Two things needed to be fixed. Neither has been rigorously debugged but it should at least be a helpful start.

1) The NOT operator (~) needs to be removed from the line "if ~deviceAddress" in tempobjcomm.m". I don't really see how anything could have worked in its original state ...

2) As it is a two-wire bus, Matlab receives all outgoing commands. I circumvented that by adding the line " "fread(serialObject, length(command));" immediately after the line described in 1). There may be a way to do this in the serial-port settings but this way seems to work fine.

28 Mar 2012 Geoffrey Akien

I didn't test it on RS422 so you may have to edit the code to make it work for you.

The message is for your information, and not an error. Modbus allows you to send more than one command per communication, and different Eurotherm devices have different maximum amounts of commands. When it connects tempobjconnect tries to ask what device it is so it knows what the maximum is for future communications (it has a list of known devices). In your case it is either an unknown device, or it didn't respond at all to the request.

28 Mar 2012 Yu Wang

I'm using the RS422. But it seems like the program is not working for RS422.

when I type "tempobjconnect(serialObject)" in the command window.

It shows this:
"Warning: Could not retrieve Eurotherm controller type - read and write commands will be limited to the minimum allowed for series 2000 controllers for this serialObject."

Thank you sir!!

11 Mar 2012 Geoffrey Akien

If you *only* want to measure temperature and nothing else then you could use a forever loop:

http://www.mathworks.com/help/techdoc/learn_matlab/f4-1931.html#brbss8u-1

for i = 1:100
temperature(i) = tempobjcomm('read', serialObject, 1, 1, 2);
wait(5)
end

This does not scale very well at all - so learn to use the timer objects if you intend to build something more complicated:
http://www.mathworks.com/help/techdoc/matlab_prog/f9-38055.html

11 Mar 2012 Yu Wang

Your entry is really helpful to me.

Now I want read temperature every 5 seconds, in other words, I wanna monitor how the temperature goes up. I'm thinking using a forever loop and put a "timeout" function in it. Could you give a hint on this?

Thank you very much.

01 Sep 2011 Geoffrey Akien

"split" is a subfunction within tempobjcomm for converting 16 bit integers into 8 bit integers in the right format for modbus communication. I wasn't aware that it was also the name for a FileExchange submission (searching for "split" turns up 96 results), and this is only a subfunction, which means that only tempobjcomm can see that it exists, and if split exists as an M-file or whatever in your own path, subfunctions take precedence (within tempobjcomm.m) so that it will not interfere with its operation ( http://www.mathworks.com/help/techdoc/matlab_prog/f4-70666.html ).

01 Sep 2011 Trae

You use 'split' function in your command that you send to your device. 'split' is not part of the standard MATLAB library and your usage doesn't follow the 'split' on FileExchange and it's usage. Can you fill in some information there?

Updates
20 May 2013

Cleaned up commenting and some MLint messages (2012b), abstracted some functions for future use, and added capability to specify deviceAddress in tempobj. Also bundled a range of m.files for reading specific parameters.

21 May 2013

Updated description.

14 Mar 2014

tempobjcomm.m bugfix: isvaliddeviceaddress is now isvalidtempobjdeviceaddress.

24 Mar 2014

Bugfix.

01 Apr 2014

Added attribution to CRC append (used to be there, not quite sure where it went).

03 Apr 2014

Bugfix, now allows parameter addresses of 0 (formally not supported by Eurotherm, but other modbus devices e.g. Omron do use it).

Contact us