⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main(bl2100).c

📁 rabbit现场采集
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * Coil Status Register Buffer
 * 0~31->portB;32~63->portC;64~95->portD
 *
 * 96~99 digital output buffer
 * OUT0-OUT9-standard digital outputs, 0~40 V DC
 */
unsigned int Port_CoilStat[100];//0~31->portB;32~63->portC;64~95->portD

/*
 * Input Status Register Buffer
 * 0~31->portB;32~63->portC;64~95->portD
 *
 * 96~99 digital intput buffer
 */
unsigned int Port_InputStat[100];//0~31->portB;32~63->portC;64~95->portD

CoData cd_portB,cd_portC,cd_portD;

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

void get_parameters(char *buffer,int FORMSpecSize)
{
	int i;
	for(i=0; i<FORMSpecSize; i++)
	{
		if(http_scanpost(FORMSpec[i].name, buffer, FORMSpec[i].value,MAX_FORMSIZE))
		FORMSpec[i].value ="";
	}
	strcpy(T1,FORMSpec[0].value);
	strcpy(T2,FORMSpec[1].value);
	strcpy(T3,FORMSpec[2].value);
	strcpy(T4,FORMSpec[3].value);
	strcpy(T5,FORMSpec[4].value);
	strcpy(T6,FORMSpec[5].value);

	strcpy(D1,FORMSpec[6].value);
	strcpy(D2,FORMSpec[7].value);
	strcpy(D3,FORMSpec[8].value);
	strcpy(D4,FORMSpec[9].value);
	strcpy(D5,FORMSpec[10].value);
	strcpy(D6,FORMSpec[11].value);
	strcpy(D7,FORMSpec[12].value);
	strcpy(D8,FORMSpec[13].value);
	strcpy(D9,FORMSpec[14].value);

	strcpy(R1,FORMSpec[15].value);
	strcpy(R2,FORMSpec[16].value);
	strcpy(R3,FORMSpec[17].value);
	strcpy(R4,FORMSpec[18].value);
	strcpy(R5,FORMSpec[19].value);
	strcpy(R6,FORMSpec[20].value);
	strcpy(R7,FORMSpec[21].value);
    return;
};
/*
 * parse the url-encoded POST data into the FORMSpec struct
 * (ie: parse 'foo=bar&baz=qux' into the struct
 */
int parse_post(HttpState* state)
{
	auto int retval;
	auto int i;
	// state->s is the socket structure, and state->p is pointer
	// into the HTTP state buffer (initially pointing to the beginning
	// of the buffer).  Note that state->p was set up in the submit
	// CGI function.  Also note that we read up to the content_length,
	// or HTTP_MAXBUFFER, whichever is smaller.  Larger POSTs will be
	// truncated.
	retval = sock_aread(&state->s, state->p,(state->content_length < HTTP_MAXBUFFER-1)?(int)state->content_length:HTTP_MAXBUFFER-1);

	if (retval < 0)
	{
		// Error--just bail out
		return 1;
	}

	// Using the subsubstate to keep track of how much data we have received
	state->subsubstate += retval;

	if (state->subsubstate >= state->content_length)
	{
		// NULL-terminate the content buffer
		state->buffer[(int)state->content_length] = '\0';

		//for (i=0;i<state->subsubstate-1;i++){
		//printf("%c ",state->buffer[i]);
		//}
		//printf("\r\n");
		// Scan the received POST information into the FORMSpec structure
		//get_parameters(state->buffer,sizeof(FORMSpec)/sizeof(FORMType));
		//for (i =0;i<21;i++) printf("FORMSpec[%d]:%s\r\n",i,FORMSpec[i].value);

		// Finished processing--returning 1 indicates that we are done
		return 1;
	}
	// Processing not finished--return 0 so that we can be called again
	return 0;
}

/* initialized http variable*/
void init_FORMSpecName(void)
{
	int i;
	/* init FORM searchable names - must init ALL FORMSpec structs! */
	//point5
    for(i=0;i<22;i++)
	{
        FORMSpec[i].name = FORMSpecName[i];
		/*
		FORMSpec[0].name = "T1"; 		//host ip
		FORMSpec[1].name = "T2"; 		//SUBNET MASK
		FORMSpec[2].name = "T3"; 		//DEFAULT GATEWAY
		FORMSpec[3].name = "T4"; 		//UNIT INDEX
		FORMSpec[4].name = "T5"; 		//UPDATE INTERVAL
		FORMSpec[5].name = "T6"; 		//REPLY TIMEOUT

		FORMSpec[6].name = "D1"; 		//port A baud rate
		FORMSpec[7].name = "D2"; 		//port A parity
		FORMSpec[8].name = "D3"; 		//port A protocol select
		FORMSpec[9].name = "D4";  		//port B baud rate
		FORMSpec[10].name = "D5";  	//port B parity
		FORMSpec[11].name = "D6";  	//port B protocol select
		FORMSpec[12].name = "D7"; 		//port C baud rate
		FORMSpec[13].name = "D8"; 		//port C parity
		FORMSpec[14].name = "D9"; 		//port C protocol select

		FORMSpec[15].name = "R1";  	//ethernet protocol type
		FORMSpec[16].name = "R2"; 		//port A data bits
		FORMSpec[17].name = "R3"; 		//port A stop bits
		FORMSpec[18].name = "R4";  	//port B data bits
		FORMSpec[19].name = "R5";  	//port B stop bits
		FORMSpec[20].name = "R6"; 		//port C data bits
		FORMSpec[21].name = "R7"; 		//port C stop bits
		//	FORMSpec[22].name = "C1";  //unsolicited message select
		*/
	}
	return;
};

long conver_baudrate(int selectval)
{
	long bps;

	switch (selectval)
	{
		case 0:
			bps = 300;
			break;
		case 1:
			bps = 1200;
			break;
		case 2:
			bps = 2400;
			break;
		case 3:
			bps = 4800;
			break;
		case 4:
			bps = 9600;
			break;
		case 5:
			bps = 19200;
			break;
		case 6:
			bps = 38400;
			break;
		case 7:
			bps = 115200;
			break;
		default:
			bps = 19200;
			break;
	}
	return bps;
}

void hardware_subsys_init(void)
{
	long baudrate;
/*******************************************************************************
*                            initializing serial port                          *
*******************************************************************************/
	serMode(0x00);
//					Mode 	Port B      	PortC    		PortD
//					----	------			-----  			-----
//    				0		RS232 3wire		RS232 3wire   	RS485 3wire
//    				1		RS232 5wire		CTS/RTS			RS485 3wire
	//init portB
	baudrate =  conver_baudrate(atoi(D1));
	serBopen(baudrate);
	serBparity(atoi(D2));	// PARAM_EPARITY		0x01
					  		// PARAM_OPARITY		0x02
					  		// PARAM_2STOP 			0x03
					  		// PARAM_NOPARITY 		0x00   and stop 1 bit

	serBdatabits(atoi(R2));	// PARAM_7BIT	0x01
							// PARAM_8BIT	0x00
	serBwrFlush();
	serBrdFlush();

	//init portC
	baudrate =  conver_baudrate(atoi(D4));
	serCopen(baudrate);
	serCparity(atoi(D5));	// PARAM_EPARITY		0x01
					  		// PARAM_OPARITY		0x02
					  		// PARAM_2STOP 			0x03
					  		// PARAM_NOPARITY 		0x00

	serCdatabits(atoi(R4));	// PARAM_7BIT	0x01
							// PARAM_8BIT	0x00
	serCwrFlush();
	serCrdFlush();

	//init portD
	baudrate =  conver_baudrate(atoi(D7));
	serDopen(baudrate);
	serDparity(atoi(D8));	// PARAM_EPARITY		0x01
					  		// PARAM_OPARITY		0x02
					  		// PARAM_2STOP 			0x03
					  		// PARAM_NOPARITY 		0x00

	serDdatabits(atoi(R6));	// PARAM_7BIT	0x01
							// PARAM_8BIT	0x00
	serDwrFlush();
	serDrdFlush();

	//
	//initializing ethernet
	//tcp_config("MY_IP_ADDRESS","192.168.1.100");
	//tcp_config("MY_NETMASK","255.255.255.0");
	//tcp_config("MY_GATEWAY","192.168.1.1");

	tcp_config("MY_IP_ADDRESS",T1);
	tcp_config("MY_NETMASK",T2);
	tcp_config("MY_GATEWAY",T3);

	//tcp_config("MY_IP_ADDRESS","10.61.204.76");
	//tcp_config("MY_NETMASK","255.255.252.0");
	//tcp_config("MY_GATEWAY","10.61.204.50");
}

void exception_responses(char *rbuf,char*sbuf,char errcode)
{
	memcpy(sbuf,rbuf,8);
	sbuf[7] |= 0x80;
	sbuf[8] = errcode;
	sbuf[5] = 3;
	mbtcpstate->cmdlen += 9;
	//return;
}


root int parse_combin_fun1_2(char *rbuf,char*sbuf,unsigned int *realdatabuf)
{
/*
* read coil status ,Class 1 commands (FC1,FC2)
*/
	auto unsigned int offset,length,i,j,wordsoffset,bitsoffset,word_value;

	memcpy(sbuf,rbuf,8);
	offset = rbuf[8]*256;
	offset += rbuf[9];
	if (offset>=1600){exception_responses(rbuf,sbuf,0x02);return -1;} //ILLEGAL DATA ADDRESS

	length = rbuf[10]*256;
	length +=rbuf[11];
	sbuf[8] = (char)((length+7)/8);
	//sbuf[8] = (char)(length/8);
	//sbuf[8] = (length % 8)>0 ? sbuf[8]+1: sbuf[8];

	j=0;
 	for (i=0;i<length;i++)
	{
		wordsoffset = (offset+i)/16;
		bitsoffset = (offset+i) % 16;
		word_value = realdatabuf[wordsoffset];
		if(i%0x8 ==0)
		{
			j = i/0x8;
			sbuf[j+9] = 0x0;
		}
		else j= i/0x8;
		if(BIT(&word_value , bitsoffset))
		{
			SET(sbuf+j+9,i%0x8);
		}
    	sbuf[5]=j+1;
 	}
	sbuf[5]+=3;
 	mbtcpstate->cmdlen += (6 + sbuf[5]);

    return 0;
}

root int parse_combin_fun3_4(char *rbuf,char*sbuf,int* realdatabuf){
/*
* read holding register , Class 0 command (FC3),Class 1 command (FC4)
*/

	auto union {int word_value; char byte_value[2];}hreg_value;
	auto unsigned int offset,length,i,j;

	memcpy(sbuf,rbuf,8);
	offset = rbuf[8]*256;
	offset += rbuf[9];
	if (offset>=1600){exception_responses(rbuf,sbuf,0x02);return -1;} //ILLEGAL DATA ADDRESS

	length = rbuf[10]*256;
	length +=rbuf[11];
	sbuf[8] =(char)(length*2) ;
	j=0;
	for (i=0;i<length;i++)
	{
		hreg_value.word_value = realdatabuf[offset+i];
		sbuf[j+9]= hreg_value.byte_value[1];
		sbuf[j+10]= hreg_value.byte_value[0];
		j++;
		j++;
		sbuf[5]=j;
	}
	sbuf[5]+=3;
	mbtcpstate->cmdlen += (6 + sbuf[5]);

    return 0;
}
root int poke_combin_fun6(char *rbuf,char*sbuf,int* realdatabuf){
/*
* read holding register , Class 0 command (FC6)
*/

	auto unsigned int offset,value,i,j;

	memcpy(sbuf,rbuf,8);
	offset = rbuf[8]*256;
	offset += rbuf[9];
	if (offset>=1600){return -1;} //ILLEGAL DATA ADDRESS

	value = rbuf[10]*256 + rbuf[11];
	realdatabuf[offset] = value;

	mbtcpstate->cmdlen += (6 + sbuf[5]);

    return 0;


}
root int receive_mbtcp_data(char *buf, int len)
{
	/*
	 * this is a user-function that will recieve the data
	 * as it comes in. (buf points to the buffer, len is the
	 * length of the data that was received.)
	 *
	 * Note - When this function returns, the buffer will be
	 * overwritten, so if you want the data, it should be
	 * coppied out of the buffer before the function returns.
	 *
	 * Nothing is done with it for now; it is just dropped on
	 * the floor.
	 */

	auto int i,funnum;
	auto char *lpbuf;
	auto char *lpcmdbuf;

//	printf ("recev:");
//  for(i=0;i<len;i++){printf("%d ",buf[i]);}
//  printf ("\r\n");

	lpbuf = buf;
	lpcmdbuf = mbtcpstate->cmdbuf;

	for(;(len!=0 && len>0 && (lpbuf-buf)<len);)
	{// invail modbus function number
		if(lpbuf[7]!=1 && lpbuf[7]!=2 && lpbuf[7]!=3 && lpbuf[7]!=4 && lpbuf[7]!=5 && lpbuf[7]!=6)
		{
			return -1;
		}
   		funnum = lpbuf[7];

		//if((buf[5]+6)==len && buf[4] ==0){
		switch (funnum  /*modbus function code*/)
		{
	   		case 1: //read Coil status
         		parse_combin_fun1_2(lpbuf,lpcmdbuf,Port_CoilStat);
					lpbuf +=12;
					lpcmdbuf = mbtcpstate->cmdbuf + mbtcpstate->cmdlen;
 		   		break;

 	  		case 2: //read input status
         		parse_combin_fun1_2(lpbuf,lpcmdbuf,Port_InputStat);
					lpbuf +=12;
					lpcmdbuf = mbtcpstate->cmdbuf + mbtcpstate->cmdlen;
   				break;

	   		case 3: //read holding registers
					parse_combin_fun3_4(lpbuf,lpcmdbuf,Port_HoldingReg);
					lpbuf +=12;
					lpcmdbuf = mbtcpstate->cmdbuf + mbtcpstate->cmdlen;

				//printf ("send:");
				//for(i=0;i<state->cmdlen;i++){printf("%d ",state->cmdbuf[i]);}
				//printf ("\r\n");

   				break;
	   		case 4: //read input registers
					parse_combin_fun3_4(lpbuf,lpcmdbuf,Port_InputReg);
					lpbuf +=12;
					lpcmdbuf = mbtcpstate->cmdbuf + mbtcpstate->cmdlen;
   				break;
	   		case 5: //force sigle Coil
   				break;
	   		case 6: //preset sigle holding register
				poke_combin_fun6(lpbuf,lpcmdbuf,Port_HoldingReg);
				Write_IO();
				lpbuf +=12;
				lpcmdbuf = mbtcpstate->cmdbuf + mbtcpstate->cmdlen;
   				break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -