📄 libmodbus.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <TITLE>Programming the LibModbus</TITLE> <META NAME="GENERATOR" CONTENT="StarOffice/5.1 (Linux)"> <META NAME="AUTHOR" CONTENT="Laurent LOPES"> <META NAME="CREATED" CONTENT="20010508;23245200"> <META NAME="CHANGEDBY" CONTENT="Laurent LOPES"> <META NAME="CHANGED" CONTENT="20010509;23395400"></HEAD><BODY BGCOLOR="#ffffff"><P ALIGN=CENTER STYLE="margin-top: 0.42cm; page-break-after: avoid"><FONT COLOR="#0000ff"><FONT FACE="times, serif"><FONT SIZE=7 STYLE="font-size: 32pt"><B>Programmingthe LibModbus</B></FONT></FONT></FONT></P><PRE STYLE="margin-bottom: 0.5cm; text-align: left"><FONT FACE="times, serif"><FONT SIZE=4>May, 09 2001</FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>Table of content :</FONT></FONT></PRE><OL> <LI><P STYLE="margin-bottom: 0cm"><A HREF="#1"><FONT SIZE=4>Open & Close</FONT></A></P> <LI><P STYLE="margin-bottom: 0cm"><A HREF="#2"><FONT SIZE=4>Master</FONT></A></P> <LI><P STYLE="margin-bottom: 0cm"><A HREF="#3"><FONT SIZE=4>Slave</FONT></A></P> <LI><P STYLE="margin-bottom: 0cm"><A HREF="#4"><FONT SIZE=4>Misc</FONT></A></P> <LI><P STYLE="margin-bottom: 0cm"><A HREF="#5"><FONT SIZE=4>Author</FONT></A></P></OL><PRE><A NAME="1"></A><FONT COLOR="#dc2300"><FONT FACE="times, serif"><FONT SIZE=6 STYLE="font-size: 28pt"><B>1. Open & Close</B></FONT></FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>Before using master and slave functions you need to initialise the serial port with the function :</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int Mb_open_device(char device[],</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int speed,</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int parity,</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int data_bits_length,</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int stop_bits_length);</FONT></FONT></PRE><TABLE WIDTH=1137 BORDER=1 CELLPADDING=4 CELLSPACING=3> <COL WIDTH=253> <COL WIDTH=857> <THEAD> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>device</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>string who contain the device to open : /dev/ttyS0 for COM1, /dev/ttyS1 for COM2, etc</FONT></FONT></PRE> </TD> </TR> </THEAD> <TBODY> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>speed</FONT></FONT></PRE> </TD> <TD WIDTH=857> <P><FONT FACE="times, serif"><FONT SIZE=4>indicate the speed in baud rate. Each values are : 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400. If you set another value, the function use automatically 9600 bauds.</FONT></FONT></P> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>parity</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>the parity off the frame :</FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4> 0 indicate don't use parity, </FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>1 indicate EVEN parity</FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>-1 indicate ODD parity.</FONT></FONT></PRE> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>data_bits_length</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>length of the data bits. You can set 7 or 8 bits but in modbus RTU mode only 8 bits work.</FONT></FONT></PRE> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>data_bits_stop</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>indicate how many bits to send at the end of the frame. You can set 1 or 2 bits.</FONT></FONT></PRE> </TD> </TR> </TBODY></TABLE><PRE><FONT FACE="times, serif"><FONT SIZE=4>The serial port is open in bidirectional asynchronous mode. </FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4><FONT FACE="courier, monospace">Mb_open_device()</FONT> save oldest parameters of the serial port to restore them when dial is terminate.</FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>For more information about serial port configuration please read man pages : </FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>man 2 open, man 2 read, man 2 write and the serial programming HOWTO.</FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>At the end of your program you can close and restore the oldest parameters with :</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> void Mb_close_device(int device);</FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4><FONT FACE="courier, monospace">device </FONT>indicate the device descriptor returned by the <FONT FACE="courier, monospace">Mb_open_device()</FONT> function.</FONT></FONT><A NAME="2"></A><FONT COLOR="#dc2300"><FONT FACE="times, serif"><FONT SIZE=6 STYLE="font-size: 28pt"><B>2. Master</B></FONT></FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>Before send a packet you need to configure it. There is a struct made for it : <FONT FACE="courier, monospace">Mbm_trame</FONT>. The declaration of the Mbm_trame is declared as below :</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> struct {</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int device;</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int slave;</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int function;</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int address;</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int length;</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> int timeout;</FONT></FONT><FONT FACE="courier, monospace"><FONT SIZE=4> } Mbm_trame;</FONT></FONT></PRE><TABLE WIDTH=1137 BORDER=1 CELLPADDING=4 CELLSPACING=3> <COL WIDTH=253> <COL WIDTH=857> <THEAD> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>device</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>indicate the device descriptor returned by <FONT FACE="courier, monospace">Mb_open_device().</FONT></FONT></FONT></PRE> </TD> </TR> </THEAD> <TBODY> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>slave</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>indicate the slave to call.</FONT></FONT></PRE> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>function</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>indicate the function to send.</FONT></FONT></PRE> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>address</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>indicate the address for functions 0x03, 0x04, 0x06, 0x0a. For 0x07 and 0x08 functions, this value isn't use.</FONT></FONT></PRE> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=253> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>length</FONT></FONT></PRE> </TD> <TD WIDTH=857> <PRE><FONT FACE="times, serif"><FONT SIZE=4>indicate how many data to read or to write to the slave. </FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>This setting is only use for 0x03, 0x04, 0x0a modbus functions.</FONT></FONT></PRE> </TD> </TR> </TBODY></TABLE><PRE><FONT FACE="times, serif"><FONT SIZE=4>When <FONT FACE="courier, monospace">Mbm_trame </FONT>done, you can call send the request with </FONT></FONT><FONT COLOR="#000000"><FONT FACE="courier, monospace"><FONT SIZE=4> int Mb_master(Mbm_trame packet,</FONT></FONT></FONT><FONT COLOR="#000000"><FONT FACE="courier, monospace"><FONT SIZE=4> int data_in[],</FONT></FONT></FONT><FONT COLOR="#000000"><FONT FACE="courier, monospace"><FONT SIZE=4> int data_out[],</FONT></FONT></FONT><FONT COLOR="#000000"><FONT FACE="courier, monospace"><FONT SIZE=4> void *ptr_function_send,</FONT></FONT></FONT><FONT COLOR="#000000"><FONT FACE="courier, monospace"><FONT SIZE=4> void *ptr_function_receive);</FONT></FONT></FONT></PRE><TABLE WIDTH=1127 BORDER=1 CELLPADDING=4 CELLSPACING=3> <COL WIDTH=252> <COL WIDTH=848> <THEAD> <TR VALIGN=TOP> <TD WIDTH=252> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>packet</FONT></FONT></PRE> </TD> <TD WIDTH=848> <PRE><FONT FACE="times, serif"><FONT SIZE=4>the struct <FONT FACE="courier, monospace">Mbm_trame</FONT> set above</FONT></FONT></PRE> </TD> </TR> </THEAD> <TBODY> <TR VALIGN=TOP> <TD WIDTH=252> <PRE><FONT FACE="courier, monospace"><FONT SIZE=4>data_in[]</FONT></FONT></PRE> </TD> <TD WIDTH=848> <P STYLE="margin-bottom: 0cm"><FONT FACE="times, serif"><FONT SIZE=4>data to send for writing functions. </FONT></FONT> </P> <PRE><FONT FACE="times, serif"><FONT SIZE=4>Modbus function 0x06 (send one byte) send only <FONT FACE="courier, monospace">data_in[0]</FONT>.</FONT></FONT><FONT FACE="times, serif"><FONT SIZE=4>Modbus function 0x0a (send n bytes) send from <FONT FACE="courier, monospace">data_in[0] </FONT>to <FONT FACE="courier, monospace">data_in[packet.length-1]</FONT> byte.</FONT></FONT></PRE> </TD> </TR> <TR VALIGN=TOP>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -