LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to wirte a message to devices through modbus interface

Hi,

 

Now i am using labview 8.2 to wirte some codes to control the VG IG3 gauge which is used to measure gas pressure. And the interface is modbus.

 

the message structure is "frame start", "device adress", "function code", "data package", "crc", "end indicator"

 

i have download modbus lib ,use modbus serial master comminication.vi and get an example on your web

 

but i am wondering what place i can wirte the 'data package" and "CRC"?

 

the function code is 17hex?

 

which vi should i use slave or master?

 

thank you very  much.

 

 

0 Kudos
Message 1 of 6
(2,891 Views)

Usually, the device you are communicating with is the slave.  So you will want to use the Master Query VI.  The CRC is calculated automatically as part of the modbus VI's.

 

The key information you will need is the serial parameters for your device such as the com port it is on, baud rate, parity, stop bits.  The slave address for the device, the register address you want to read or write, and how many registers in a row.

 

Start with the serial Mobus Master example.

Message Edited by Ravens Fan on 12-08-2008 11:40 PM
Message 2 of 6
(2,890 Views)

thank you very much

 

 

so is the number of registers given by the companies?

 

and should be entered in "quantity" after "starting adress"? or starting adress is the adress of the register?

0 Kudos
Message 3 of 6
(2,873 Views)

The number of registers and their addresses are given by the company making the device.  Since function code 17 is for read/write multiple registers, they are holding registers and will have values such as 40001, 40002,.  If you want register 40001, You would enter the starting address as either 0 or 1, without the 40,000 part of it which is assumed.  Whether it is 0 or 1 you would have to play with as sometimes the register is shifted by 1 in memory (zero-based or not).

 

It sounds like you are unfamiliar with the modbus protocol.  I would highly recommed reading the specifications Modbus organization puts out.   http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf

 

If you want only 1 register, then yes you put 1 in the quantity.  If you wanted 2 registers such as 40008 and 400009, then put the starting address as 7 (or 8), and the quantity as 2.  Then you would get an array where each element is one register.

 

In general modbus command 17 isn't used all that often.  I find that I am either using code 4 (read holding registers) or code 10hex (16 dec) (for write multiple registers).

 

 

Message 4 of 6
(2,859 Views)

Thank you and i get what is wrong

 

but the company's construction says that the device just surpport write/read mutiregisters. And i had try just read holding registers and read input registers and got no response.

 

 

From the website,i find this for write/read muti

 Function code 1 Byte 0x17
Read Starting Address 2 Bytes 0x0000 to 0xFFFF
Quantity to Read 2 Bytes 0x0001 to 0x007D
Write Starting Address 2 Bytes 0x0000 to 0xFFFF
Quantity to Write 2 Bytes 0x0001 to 0X0079
Write Byte Count 1 Byte 2 x N

 

If i use modbus query.vi, how to set reading starting address and write starting address.

 

thanks very much.

0 Kudos
Message 5 of 6
(2,841 Views)

Jamiechuang wrote:

Thank you and i get what is wrong

 

but the company's construction says that the device just surpport write/read mutiregisters. And i had try just read holding registers and read input registers and got no response.

 

 

From the website,i find this for write/read muti

 Function code 1 Byte 0x17
Read Starting Address 2 Bytes 0x0000 to 0xFFFF
Quantity to Read 2 Bytes 0x0001 to 0x007D
Write Starting Address 2 Bytes 0x0000 to 0xFFFF
Quantity to Write 2 Bytes 0x0001 to 0X0079
Write Byte Count 1 Byte 2 x N

 

If i use modbus query.vi, how to set reading starting address and write starting address.

 

thanks very much.


The company should have written their code so that it would use the other common public modbus commands such as read registers and writing individual registers.

 

Now that I can look more closely at the NI modbus library, I see you are going to have a problem.  NI's library didn't include hex command 17.  (As I said before, most people probably wouldn't need it).  And the Modbus command cluster doesn't include the extra elements needed for the write starting address and quantity.

 

It would be possible to modify the library so you can do it.  Enter a code 23 (decimal) in the list of commands for the Modbus data unit.  Use the modbus data unit when you use the VI's.  Then you can build an array on your own that contains the elements in the same order as listed above.

 

See the attached.  I can't guarantee it works perfectly because I have no way to test it.  But this was done based on looking at the modbus spec, your info above, and referencing other modbus codes as they were done in the NI library.

 

 

 

Message Edited by Ravens Fan on 12-10-2008 10:02 AM
Download All
Message 6 of 6
(2,823 Views)