Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

代码分享,请高手指点

Posted by peter125521 at 2011-10-09 02:33:41 on Problem 3754
#include<stdio.h>
#include<stdlib.h>

#define htons( val ) ( ( (val & 0xFF00) >> 8) | ( (val & 0xFF) << 8 ) )
#define htonl( val ) ( ( (val & 0xFF) << 24 ) | ( val & 0xFF00 ) << 8 | ( val & 0xFF0000 ) >> 8 | ( val & 0xFF000000 ) >> 24 )

/* calculate CRC checksum */
unsigned short crc16( unsigned char *str, int len )
{
	unsigned short crc = 0xFFFF;
	unsigned char *p = str, flag, c;
	for( ; len--; p++ ) {
		crc ^= *p;
		for( c=8; c--; ) {
			flag = 0;
			if( crc & 0x1 )
				flag = 1;
			crc >>= 1;
			if( flag )
				crc ^= 0xA001;
		}
	}
	return crc;
}

/*

The Modbus send:

<SlaveAddress, 1 Byte> <Function, 1 Byte> <Start Address, 2 Bytes> <NumberofBytes, 2 Bytes> <Checksum, 2 Bytes> 

*/
#define MODBUS_MAX_XMIT		100
int modbus_xmit( unsigned char slave_addr, unsigned char func, unsigned short start_addr, unsigned short bytesnum )
{
	static unsigned char buff[ MODBUS_MAX_XMIT ];
	//printf( "%x,%x,%x,%x\n", slave_addr, func, start_addr, bytesnum );
	unsigned char *p_uint8 = buff;
	unsigned short *p_uint16;
	*p_uint8++ = slave_addr;
	*p_uint8++ = func;
	p_uint16 = ( unsigned short * ) p_uint8;
	*p_uint16++ = htons(start_addr);
	*p_uint16++ = htons(bytesnum);
	*p_uint16++ = crc16( buff, 6 );
	//printf( "\nCRC:%X\n", crc16( buff, 6 ) );
	p_uint8 = ( unsigned char * ) p_uint16;
	*(p_uint8) = '\0';
	for( p_uint8=buff; p_uint8<buff+8; p_uint8++ ){
		printf( "%02X", *p_uint8 );
	}
	printf( "\n" );
	return 0;
}

/*

Modbus receive:

<SlaveAddress,1Byte> <Function,1Byte> <NumberofBytes,1Byte> <DataIEEE32,xByte> <Checksum,2Bytes> 
02040841CC0000477F2100DF85
*/
#define MODBUS_MAX_RECV		100
int modbus_recv( unsigned char *str )
{
	static unsigned char data[MODBUS_MAX_RECV];
	unsigned char *p;
	unsigned char i, len=0;
	char tmp[3] = { 0, };
	for( i=0; i<128 && str[i]; i+=2 ){
		tmp[0] = str[i];
		tmp[1] = str[i+1];
		data[len++] = strtoul( tmp, (char **)NULL, 16 );
	}
	p = data;
	unsigned short crc = * ((unsigned short * ) (p+len-2) );

	if( crc != crc16( p, len-2 ) ){
		printf( "CRC_ERROR\n" );
		return -1;
	}
	unsigned char numofbytes = *(p+2);
	numofbytes /= 4;
	float *pf;
	p += 3; // move to floats
	pf = ( float * ) p;
	for( i=0; i<numofbytes; i++, pf++, p+=4 ){
		int *val = ( int * ) p;
		*val = htonl( *val );
		pf = ( float * ) val;
		if( i ) 
			printf( "," );
		printf( "%.1f", *pf );
	}
	printf( "\n" );
	return 0;
}


int main()
{
	int slave_addr, func, start_addr, bytesnum;
	char buff[128];
	while( scanf( "%d,%d,%d,%d", &slave_addr, &func, &start_addr, &bytesnum ) == 4 ){
		modbus_xmit( slave_addr, func, start_addr, bytesnum );
		scanf( "%s", buff );
		modbus_recv( buff );
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator