亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? modbus_tcp_c.c

?? linux下的modbus協議,很好用,歡迎大家下載,在國外網站上獲得的
?? C
?? 第 1 頁 / 共 2 頁
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY><XMP>/* 		modbus_tcp.c 

   By P.Costigan email: phil@pcscada.com.au http://pcscada.com.au

   These library of functions are designed to enable a program send and
   receive data from a device that communicates using the Modbus tcp protocol.

   Copyright (C) 2000 Philip Costigan  P.C. SCADA LINK PTY. LTD.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.





   The functions included here have been derived from the 
   Modicon Modbus Protocol and Modbus TCP Protocol Reference Guide
   which can be obtained from Schneider at www.schneiderautomation.com.

   This code has its origins with 
   paul@pmcrae.freeserve.co.uk (http://www.pmcrae.freeserve.co.uk)
   who wrote a small program to read 100 registers from a modbus slave.
   
   I have used his code as a catalist to produce this more functional set
   of functions. Thanks paul.




*/

#include <fcntl.h>	/* File control definitions */
#include <stdio.h>	/* Standard input/output */
#include <string.h>
#include <termio.h>	/* POSIX terminal control definitions */
#include <sys/time.h>	/* Time structures for select() */
#include <unistd.h>	/* POSIX Symbolic Constants */
#include <errno.h>	/* Error definitions */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "modbus_tcp.h"

/*#define DEBUG*/	/* uncomment to see the data sent and received */









/***********************************************************************

   send_query( file_descriptor, query_string, query_length )

Function to send a query out to a modbus slave.
************************************************************************/

int send_query( int sfd, unsigned char *query, size_t string_length )
{
	int write_stat;

#ifdef DEBUG
	int i;
#endif


#ifdef DEBUG
/* Print to stderr the hex value of each character that is about to be */
/* sent to the modbus slave.					       */

	for( i = 0; i < string_length; i++ )
	{
		fprintf( stderr, "[%0.2X]", query[ i ] );
	}
	fprintf( stderr, "\n" );
#endif

	tcflush( sfd, TCIOFLUSH );	/* flush the input & output streams */
	
	write_stat = send( sfd, query, string_length, 0);

	tcflush( sfd, TCIFLUSH );	/* maybe not neccesary */

	return( write_stat );
}













/*********************************************************************

	modbus_response( response_data_array, query_array )

   Function to the correct response is returned and check for correctness.

   Returns:	string_length if OK
		0 if failed
		Less than 0 for exception errors

	Note: All functions used for sending or receiving data via
	      modbus return these return values.

**********************************************************************/

int modbus_response( unsigned char *data, unsigned char *query, int fd )
{
	int response_length;


	/* local declaration */
	int receive_response( unsigned char *received_string, int sfd );



	response_length = receive_response( data, fd );
	if( response_length > 0 )
	{
		
		/********** check for exception response *****/

		if( response_length && data[ 7 ] != query [ 7 ] )
		{
			/* return the exception value as a -ve number */
			response_length = 0 - data[ 8 ];
		}
	}

	return( response_length );
}








/***********************************************************************

	receive_response( array_for_data )

   Function to monitor for the reply from the modbus slave.
   This function blocks for timeout seconds if there is no reply.

   Returns:	Total number of characters received.
***********************************************************************/

int receive_response(  unsigned char *received_string, int sfd )
{

	int rxchar = -1;
	int data_avail = FALSE;
	int bytes_received = 0;
	int read_stat;

	int timeout = 1;			/* 1 second */
	int char_interval_timeout = 10000; 	/* 10 milliseconds. */

	int max_fds = 32;
	fd_set rfds;

	struct timeval tv;

	tv.tv_sec = timeout;
	tv.tv_usec = 0;

	FD_ZERO( &rfds );
	FD_SET( sfd, &rfds );


#ifdef DEBUG
	fprintf( stderr, "Waiting for response.\n");
#endif	

	/* wait for a response */
	data_avail = select( max_fds, &rfds, NULL, NULL, &tv );
	

	if( !data_avail )
	{
		bytes_received = 0; 
#ifdef DEBUG
		fprintf( stderr, "Comms time out\n" );
#endif
	}

	tv.tv_sec = 0;
	tv.tv_usec = char_interval_timeout;

	FD_ZERO( &rfds );
	FD_SET( sfd, &rfds );
	
	while( data_avail )
	{

		/* if no character at the buffer wait char_interval_timeout */
		/* before accepting end of response			    */

		if( select( max_fds, &rfds, NULL, NULL, &tv) > 0 )
		{
			read_stat = recv( sfd, &rxchar, 1, 0);

			if( read_stat < 0 )
			{
				bytes_received = SOCKET_FAILURE;
				data_avail = FALSE;
			}
			else
			{
				
				rxchar = rxchar & 0xFF;
				received_string[ bytes_received ++ ] = rxchar;
			}


			if( bytes_received >= MAX_RESPONSE_LENGTH )
			{
				bytes_received = SOCKET_FAILURE;
				data_avail = FALSE;
			}
#ifdef DEBUG
			/* display the hex code of each character received */
			fprintf( stderr, "<%0.2X>", rxchar );
#endif

		}
		else
		{
			data_avail = FALSE;
		}

	}	

#ifdef DEBUG
	fprintf( stderr, "\n" );
#endif

	if( bytes_received > 7 )
	{
		bytes_received -= 7;
	}


	return( bytes_received );
}







/***********************************************************************

	The following functions construct the required query into
	a modbus query packet.

***********************************************************************/

#define REQUEST_QUERY_SIZE 12	/* the following packets require          */
#define CHECKSUM_SIZE 2		/* 6 unsigned chars for the packet plus   */
				/* 2 for the checksum.                    */

void build_request_packet( int slave, int function, int start_addr,
			   int count, unsigned char *packet )
{
	int i;


	for( i = 0; i < 5 ; i++ ) packet[ i ] = 0;

	packet[ i++ ] = 6;
	packet[ i++ ] = slave;
	packet[ i++ ] = function;
	start_addr -= 1;
	packet[ i++ ] = start_addr >> 8;
	packet[ i++ ] = start_addr & 0x00ff;
	packet[ i++ ] = count >> 8;
	packet[ i ] = count &0x00ff;

}






/************************************************************************

	read_IO_status

	read_coil_stat_query and read_coil_stat_response interigate
	a modbus slave to get coil status. An array of coils shall be
	set to TRUE or FALSE according to the response from the slave.
	
*************************************************************************/

int read_IO_status( int function, int slave, int start_addr, int count, 
			    int *dest, int dest_size, int sfd )
{
	/* local declaration */
	int read_IO_stat_response( int *dest, int dest_size, int coil_count,
					unsigned char *query, int fd );

	int status;


	unsigned char packet[ REQUEST_QUERY_SIZE + CHECKSUM_SIZE ];
	build_request_packet( slave, function, start_addr, count, packet );

	if( send_query( sfd, packet, REQUEST_QUERY_SIZE ) > -1 )
	{
		status = read_IO_stat_response( dest, dest_size, 
						count, packet, sfd );
	}
	else
	{
		status = SOCKET_FAILURE;
	}



	return( status );
}



/************************************************************************

	read_coil_status_tcp

	reads the boolean status of coils and sets the array elements
	in the destination to TRUE or FALSE

*************************************************************************/

int read_coil_status_tcp( int slave, int start_addr, int count,
				int *dest, int dest_size, int sfd )
{
	int function = 0x01;
	int status;

	status = read_IO_status( function, slave, start_addr, count, 
						dest, dest_size, sfd );

	return( status );
}





/************************************************************************

	read_input_status_tcp

	same as read_coil_status but reads the slaves input table.

************************************************************************/

int read_input_status_tcp( int slave, int start_addr, int count,
				int *dest, int dest_size, int sfd )
{
	int function = 0x02;	/* Function: Read Input Status */
	int status;

	status = read_IO_status( function, slave, start_addr, count,
						 dest, dest_size, sfd );



	return( status );
}




/**************************************************************************

	read_IO_stat_response

	this function does the work of setting array elements to TRUE
	or FALSE.

**************************************************************************/

int read_IO_stat_response( int *dest, int dest_size, int coil_count,
				unsigned char *query, int fd )
{

	unsigned char data[ MAX_RESPONSE_LENGTH ];
	int raw_response_length;
	int temp, i, bit, dest_pos = 0;
	int coils_processed = 0;

	raw_response_length = modbus_response( data, query, fd );
	

	if( raw_response_length > 0 )
	{
		for( i = 0; i < ( data[8] ) && i < dest_size; i++ )
		{
			/* shift reg hi_byte to temp */
			temp = data[ 9 + i ] ;
			for( bit = 0x01; bit & 0xff && 
				coils_processed < coil_count; )
			{
				if( temp & bit )
				{
					dest[ dest_pos ] = TRUE;
				}
				else
				{
					dest[ dest_pos ] = FALSE;
				}
				coils_processed++;
				dest_pos++;
				bit = bit << 1;
			}
		}
	}

	return( raw_response_length );
}





/************************************************************************

	read_registers

	read the data from a modbus slave and put that data into an array.

************************************************************************/

int read_registers( int function, int slave, int start_addr, int count, 
			  int *dest, int dest_size, int sfd )
{
	/* local declaration */
	int read_reg_response( int *dest, int dest_size, 
					unsigned char *query, int fd );

	int status;


	unsigned char packet[ REQUEST_QUERY_SIZE + CHECKSUM_SIZE ];
	build_request_packet( slave, function, start_addr, count, packet );

	if( send_query( sfd, packet, REQUEST_QUERY_SIZE ) > -1 )
	{
		status = read_reg_response( dest, dest_size, packet, sfd );
	}
	else
	{
		status = SOCKET_FAILURE;
	}
	
	return( status );

}



/************************************************************************

	read_holding_registers

	Read the holding registers in a slave and put the data into
	an array.

*************************************************************************/

int read_holding_registers_tcp( int slave, int start_addr, int count,
				int *dest, int dest_size, int sfd )
{
	int function = 0x03;    /* Function: Read Holding Registers */
	int status;

	if( count > MAX_READ_REGS )
	{
		count = MAX_READ_REGS; 
#ifdef DEBUG
		fprintf( stderr, "Too many registers requested.\n" );
#endif
	}

	status = read_registers( function, slave, start_addr, count,
						dest, dest_size, sfd );

	return( status);
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩激情av在线| 秋霞电影网一区二区| 亚洲国产综合在线| 久久99热这里只有精品| 色综合久久久久| 精品卡一卡二卡三卡四在线| 一区二区免费看| hitomi一区二区三区精品| 日韩天堂在线观看| 亚洲伊人色欲综合网| 国产白丝网站精品污在线入口| 欧美三区在线观看| 日韩美女视频一区二区| 韩国欧美国产1区| 欧美精品一二三| 亚洲精品伦理在线| av中文字幕在线不卡| 国产视频一区二区在线观看| 日韩精品视频网站| 欧美三区免费完整视频在线观看| 国产精品久久久久久久裸模 | 国产精品乱子久久久久| 一区二区三区丝袜| 成人黄色电影在线| 国产亚洲欧美一级| 久久成人av少妇免费| 欧美日韩一区二区在线观看视频| 亚洲男同1069视频| 94色蜜桃网一区二区三区| 国产精品水嫩水嫩| 国产suv精品一区二区6| 久久人人爽人人爽| 国产精品996| 国产精品少妇自拍| 成人精品视频一区二区三区| 欧美国产激情二区三区| 成人精品一区二区三区中文字幕| 国产精品剧情在线亚洲| 成人h精品动漫一区二区三区| 国产精品久久久久婷婷| 色视频一区二区| 欧美性大战久久| 亚洲六月丁香色婷婷综合久久 | 午夜视黄欧洲亚洲| 欧美日韩一区二区三区四区| 偷拍一区二区三区| 欧美一区二区三区系列电影| 九九九久久久精品| 日本一区二区三区dvd视频在线| 国产高清精品久久久久| 中文字幕欧美一区| 欧美在线观看一区| 美腿丝袜亚洲三区| 久久综合色鬼综合色| 国产91对白在线观看九色| 国产精品乱人伦| 色噜噜狠狠色综合中国| 久草这里只有精品视频| 欧美丝袜第三区| 日韩成人av影视| 精品日韩在线观看| av爱爱亚洲一区| 亚洲国产美女搞黄色| 欧美v日韩v国产v| 99久久免费国产| 青青青爽久久午夜综合久久午夜| 亚洲精品在线三区| 色琪琪一区二区三区亚洲区| 青娱乐精品视频| 国产精品久久久久久久久晋中 | 国产精品亚洲人在线观看| 国产精品日韩精品欧美在线| 欧美午夜宅男影院| 国产精品一区在线观看你懂的| 亚洲精品亚洲人成人网在线播放| 宅男在线国产精品| www.久久精品| 精品在线一区二区三区| 精品久久国产97色综合| 911国产精品| 成人精品在线视频观看| 天天综合色天天综合色h| 国产精品天美传媒沈樵| 欧美一区2区视频在线观看| 99re热这里只有精品视频| 麻豆视频观看网址久久| 亚洲激情五月婷婷| 国产欧美一区二区三区在线看蜜臀| 欧美影院午夜播放| 白白色 亚洲乱淫| 黄一区二区三区| 日本美女一区二区| 亚洲综合在线视频| 亚洲超碰97人人做人人爱| 欧美国产日韩精品免费观看| 欧美一区二区三区公司| 欧美在线视频不卡| 99v久久综合狠狠综合久久| 精品一区二区久久| 日韩中文欧美在线| 亚洲国产成人91porn| 亚洲色大成网站www久久九九| 国产精品66部| 欧美大片拔萝卜| 欧美三级日韩在线| 91美女片黄在线观看91美女| 国产一区二区三区免费看| 免费成人av在线| 日韩精品一级二级 | 亚洲视频每日更新| 欧美激情一区二区三区不卡| 久久久久久久电影| 久久精品人人做人人综合| 日韩欧美的一区二区| 日韩免费性生活视频播放| 日韩午夜三级在线| 日韩亚洲欧美在线| 日韩一区二区三区视频在线观看| 欧美精品在线观看一区二区| 欧美日本一区二区| 欧美少妇bbb| 欧美精品99久久久**| 91.麻豆视频| 日韩午夜电影在线观看| 精品粉嫩aⅴ一区二区三区四区| 日韩你懂的在线播放| 久久免费偷拍视频| 欧美国产一区视频在线观看| 国产精品拍天天在线| 亚洲欧美另类小说| 狠狠网亚洲精品| 国精产品一区一区三区mba视频| 精品亚洲成a人| 大桥未久av一区二区三区中文| 国产91色综合久久免费分享| 99riav一区二区三区| 色狠狠一区二区| 欧美精品乱人伦久久久久久| 日韩欧美在线影院| 久久日韩粉嫩一区二区三区| ㊣最新国产の精品bt伙计久久| 亚洲永久免费视频| 日本不卡的三区四区五区| 麻豆国产精品777777在线| 国产麻豆视频一区二区| 91性感美女视频| 91麻豆精品国产91久久久久| 久久久久久久久久久黄色| 亚洲女人的天堂| 日韩av二区在线播放| 国产河南妇女毛片精品久久久| 91欧美一区二区| 日韩一区二区三区电影在线观看| 国产欧美一区二区精品婷婷| 亚洲一区免费在线观看| 麻豆国产精品一区二区三区| 99re这里只有精品首页| 欧美一区二区不卡视频| 亚洲天堂av老司机| 欧美bbbbb| 色吧成人激情小说| 精品国产乱码久久| 亚洲超碰97人人做人人爱| 国产成人精品一区二区三区网站观看| 91激情五月电影| 欧美激情在线一区二区三区| 日韩电影在线看| 91福利在线播放| 国产精品毛片高清在线完整版 | 91精品黄色片免费大全| 一区在线观看视频| 国产精品影视在线| 91精品国产综合久久香蕉的特点| 中文字幕一区二区三区av| 精品一区在线看| 欧美日韩高清一区| 亚洲精品高清在线| 国产成人精品免费一区二区| 日韩视频在线一区二区| 亚洲一区中文日韩| eeuss鲁一区二区三区| 精品区一区二区| 日韩精品色哟哟| 欧美日韩免费一区二区三区 | 一本一本大道香蕉久在线精品| 久久综合久久综合久久综合| 日韩精品高清不卡| 欧美少妇一区二区| 亚洲午夜三级在线| 91免费版pro下载短视频| 国产无人区一区二区三区| 精品一区二区三区视频 | 天堂成人免费av电影一区| 色综合久久久久久久久| 亚洲欧美日韩综合aⅴ视频| jiyouzz国产精品久久| 国产欧美一区二区精品婷婷| 国产成人免费在线视频| 久久精品亚洲精品国产欧美kt∨| 韩国毛片一区二区三区|