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

📄 main(bl2100).c

📁 rabbit现场采集
💻 C
📖 第 1 页 / 共 3 页
字号:
		  	default:
   				break;
		}
	}

	//for(i=0;i<len;i++)
	//{
	//	printf("%d ",buf[i]);
	//	state->cmdlen = 0;
	//}

	return 0;
}

int mbtcp_init(int port)
{
	mbtcpstate->s = &mbtcpstate->sock;
	mbtcpstate->lport = port;
	mbtcpstate->cmdlen = 0;
	mbtcpstate->cmdoffset = 0;
	mbtcpstate->byteswritten = 0;

	//maybe append some code
	mbtcpstate->state = MBTCP_INIT;
	return 0;
}

root void mbtcp_handler(void)
{
	auto int retval;
	static unsigned long timeout;
	retval = 0;
	//tcp_tick(mbtcpstate->s);
	if(tcp_tick(mbtcpstate->s)==0 && mbtcpstate->state!=MBTCP_INIT&& mbtcpstate->state!=MBTCP_WAITCLOSE)
	{
		mbtcpstate->state = MBTCP_WAITCLOSE;
		printf("\r\n tick reset \r\n");
	}

	switch(mbtcpstate->state)
	{
		case MBTCP_INIT:
			tcp_listen(mbtcpstate->s,mbtcpstate->lport, 0, 0, NULL, 0);
			mbtcpstate->state = MBTCP_LISTEN;
			printf("\nInitializing socket.\n");
			timeout = SEC_TIMER;
			//break; //add by zhangqiang on 20070103

		case MBTCP_LISTEN:
			if(sock_established(mbtcpstate->s)||(sock_bytesready(mbtcpstate->s) != -1))
			{
				printf("Connection Established.\n");
				mbtcpstate->state = MBTCP_GETREQ;
 				timeout = SEC_TIMER;
 			}
			break;

		case MBTCP_GETREQ:
			if(!sock_established(mbtcpstate->s)&&(sock_bytesready(mbtcpstate->s) == -1))
			{
				/* connection died; reset */
				printf("Connection lost.\n\n");
				mbtcpstate->state = MBTCP_INIT;
				break;
			}

			if(SEC_TIMER-timeout>20)
			{
				/* connection died; reset */
				printf("Connection time out\n\n");
				mbtcpstate->state = MBTCP_WAITCLOSE;
				break;
			}
			mbtcpstate->cmdlen = 0;
			mbtcpstate->cmdoffset = 0;
			mbtcpstate->byteswritten = 0;

			retval = sock_aread(mbtcpstate->s, mbtcpstate->buf, 12);
			//retval = sock_fastread(mbtcpstate->s, mbtcpstate->buf, MB_TCP_MAX_BUFLEN);
			if(retval == 12)
			{
				/* we got data */
				receive_mbtcp_data(mbtcpstate->buf, retval); /* give it to the user */
				mbtcpstate->state = MBTCP_SENDRSP;
				timeout  = SEC_TIMER;
				//printf("\r\nin MBTCP_GETREQ\r\n");
			}
			break;

		case MBTCP_SENDRSP:
			//printf("\r\nin MBTCP_SENDRSP\r\n");
			if(mbtcpstate->byteswritten<0)
			{
				sock_close(mbtcpstate->s);
       			mbtcpstate->state=MBTCP_INIT;
				printf("Closing Socket\n");
				break;
			}

	  		if (mbtcpstate->cmdlen>0 && mbtcpstate->cmdoffset<mbtcpstate->cmdlen && mbtcpstate->byteswritten>=0)
			{
				mbtcpstate->byteswritten=sock_fastwrite(mbtcpstate->s,mbtcpstate->cmdbuf + mbtcpstate->cmdoffset,mbtcpstate->cmdlen - mbtcpstate->cmdoffset);
				//printf("\r\ncmmd len %d\r\n",mbtcpstate->cmdlen);
				mbtcpstate->cmdoffset +=mbtcpstate->byteswritten;
   				break;
			}

 /*
  *		if there is an error or we have written out the
  *		entire my_response.
  *
  */
			mbtcpstate->state = MBTCP_GETREQ;
			break;
		case MBTCP_WAITCLOSE:
   			sock_abort(mbtcpstate->s);
     		mbtcpstate->state = MBTCP_INIT;

   			break;
		default:
			/* shouldn't ever get here */
			/* reset the conroller */
			mbtcpstate->state = MBTCP_INIT;
			printf("in default !");
			break;
		//exit(-1);
	}
}

root unsigned int sample_ad(int channel, int num_samples)
{
	auto unsigned long rawdata;
	auto unsigned int samples;
	auto unsigned char adc_cmd;
	auto unsigned char numbits;

	samples  = num_samples;
	rawdata  = 0;
	numbits  = OUTPUTBITS;	// ADC resolution
	adc_cmd 	= (channel<<4) + (numbits<<2);

	//Do conversion of channel X which will be valid on the next A/D cycle
	anaInDriver(adc_cmd, OUTPUTBITS);

	//Sample AD channel per the number of samples requested
	while(samples-- > 0)
	{	//data is for the A/D channel that was in sent in the previous adc_cmd
		rawdata += (long) anaInDriver(adc_cmd,  OUTPUTBITS); 	// execute low level A/D driver
	}
	return((unsigned int) (rawdata/num_samples));
}

root int read_io(void)
{
    auto unsigned int channel,i;
    auto unsigned char inbit;
    auto unsigned int avg_sample;
    auto int rawvalue;
	// display the input status for all channels
	for(channel = 0; channel < 16; channel++)	//read channels 0 - 15
	{
   	i = channel/16;
      inbit = digIn(channel);
      if (inbit)
      {	//read channel
	     	RES(Port_InputStat+96+i,channel%16);
			//printf("bit:%d value:%d\r\n",channel%16,inbit);
      }
      else
      {
      	SET(Port_InputStat+96+i,channel%16);
			//printf("bit:%d value:%d\r\n",channel%16,inbit);
      }
	}
	for(channel = 0; channel < 8; channel++)	//read channels 16 - 23
	{
   	i = channel/16;
      inbit = digIn(channel+16);
      if (inbit)
      {	//read channel
	   	RES(Port_InputStat+97+i,channel%16);
			//printf("bit:%d value:%d\r\n",channel%16+16,inbit);
      }
      else
      {
      	SET(Port_InputStat+97+i,channel%16);
			//printf("bit:%d value:%d\r\n",channel%16+16,inbit);
      }
	}
	for(channel = 0; channel < NUM_CHANNELS; channel++)	//read channels 0 - 10
	{
		// sample each channel 10 times...
	   avg_sample = sample_ad(channel, 100);
		rawvalue = (int)(((_adcCalib[channel][0] * (_adcCalib[channel][1] - avg_sample)))*1000);

      switch(cur_route)
      {
      	case 1:
         	Port_InputReg[1536+channel] = rawvalue;
            break;
         case 2:
        		Port_InputReg[1536+channel+15] = rawvalue;
        		break;
         case 3:
        		Port_InputReg[1536+channel+30] = rawvalue;
        		break;
         default:
        		Port_InputReg[1536+channel] = rawvalue;
        		break;
      }
	}
	return 0;
}

root int Write_IO(void)
{
	auto int channel,output_level;

	for(channel = 0; channel < 16; channel++)
	{
		// Set the high-current outputs to be OFF, for both sinking
		// and sourcing type outputs.
		if((int)(Port_HoldingReg[99+channel]) && 0x01)
		{
			digOut(channel, 1);
		}
		else
		{
      		digOut(channel, 0);
		}
	}
	return 0;
}

unsigned int currentComRunStatus[3];
unsigned int oldComRunStatus[3];
void main()
{
    auto int i,user,page1;  // handle for a virtual watchdog
    auto unsigned long tm[4];
    auto unsigned char devicetype[4];
    auto int LEDvalue[4];
    auto char hostid[20];

//------------------------------------------------------------------------
//	Initialize the controller
//------------------------------------------------------------------------
	brdInit();			// Initialize the controller
	//Initialize board , user block (setup parameters) ,ethernet ,serial port ect.

    userblock_init();
	hardware_subsys_init();
	printf("T1:%s\nT2:%s\nT3:%s\n",T1,T2,T3);

	//Initialize auth users
	http_setauthentication(HTTP_DIGEST_AUTH);

 	user = sauth_adduser("foo", "bar", SERVER_HTTP);
    page1 = sspec_addxmemfile("/", index_html, SERVER_HTTP);
	sspec_adduser(page1, user);
	sspec_setrealm(page1, "Admin");

	//Initilize sock modbus/tcp port ,web server
    sock_init();
    digOutConfig(0xFFFF);  //config digout port
    printf("My IP address is %s\n", inet_ntoa(hostid,gethostid()));
	mbtcp_init(MB_TCP_PORT);

	http_init();
	tcp_reserveport(80);

	//Clean  modbus protocol buffers,ready for receive acquired data
    for(i=0;i<1600;i++){Port_HoldingReg[i]=0;}
    for(i=0;i<1600;i++){Port_InputReg[i]=0;}
    for(i=0;i<100;i++){Port_CoilStat[i]=0;}
    for(i=0;i<100;i++){Port_InputStat[i]=0;}
	for(i = 0;i< 3;i++)
	{
		currentComRunStatus[i] = 0;
		oldComRunStatus[i] = 1;
	}
    LEDvalue[0] = LEDvalue[1] = LEDvalue[2] = LEDvalue[3] = 0x00;

	//Initialize device send data block
	protdata_init(ComB,atoi(D3));		// portB
	protdata_init(ComC,atoi(D6));		// portC
	protdata_init(ComD,atoi(D9));		// portD

	devicetype[1] =(unsigned char) atoi(D3);
	devicetype[2] =(unsigned char) atoi(D6);
	devicetype[3] =(unsigned char) atoi(D9);
	//Initialize serial port timeout var
	tm[0] = SEC_TIMER;
	tm[1] = SEC_TIMER;
	tm[2] = SEC_TIMER;
	tm[3] = SEC_TIMER;

	rebootflag =0;
	cur_route = 0;

	while (1)
	{
		costate
	    {  //detected serial comm. if timeout then reinitialized cd_port
			if (rebootflag==1)
            {
				waitfor(DelaySec(3));
				forceSoftReset();
            }
			//-------------------------------------------
      if (SEC_TIMER-tm[1]>0x20uL)
			{
				if((oldComRunStatus[0] == currentComRunStatus[0])&&(0 != devicetype[1]))
				{
					CoReset(&cd_portB);
					tm[1] = SEC_TIMER;
					oldComRunStatus[0] = currentComRunStatus[0];
					SET(Port_InputStat+98,1);            //set portB timeout flag
					printf("serial port B timeout\r\n");
    				CoBegin(&cd_portB);
				}
				else
				{
					  tm[1] = SEC_TIMER;
			      oldComRunStatus[0] = currentComRunStatus[0];
			      RES(Port_InputStat+98,1);
				}
			}

      if (SEC_TIMER-tm[2]>0x20uL)
			{
				if((oldComRunStatus[1] == currentComRunStatus[1])&&(0 != devicetype[2]))
				{
					CoReset(&cd_portC);
					tm[2] = SEC_TIMER;
					oldComRunStatus[1] = currentComRunStatus[1];
					SET(Port_InputStat+98,2);            //set portC timeout flag
					printf("serial port C timeout\r\n");
					CoBegin(&cd_portC);
				}
				else
				{
					  tm[2] = SEC_TIMER;
			      oldComRunStatus[1] = currentComRunStatus[1];
			      RES(Port_InputStat+98,2);
				}
			}
      if (SEC_TIMER-tm[3]>0x20uL)
			{
				if((oldComRunStatus[2] == currentComRunStatus[2])&&(0 != devicetype[3]))
				{
					CoReset(&cd_portD);
					tm[3] = SEC_TIMER;
					oldComRunStatus[2] = currentComRunStatus[2];
					SET(Port_InputStat+98,3);            //set portD timeout flag
					printf("serial port D timeout\r\n");
					CoBegin(&cd_portD);
				}
				else
				{
					  tm[3] = SEC_TIMER;
			      oldComRunStatus[2] = currentComRunStatus[2];
			      RES(Port_InputStat+98,3);
				}
			}
        }
		costate
		{  //deal modbus/tcp msgs , established comm.
			mbtcp_handler();
		}
		costate
		{    //insert code for i/o port data
			waitfor(DelayMs(20uL));
          	read_io();
        }
	    costate
	    {  //deal  Web Server
			http_handler();
		}
	    costate  cd_portB always_on
	    {  // process serial comm. port B
			waitfor(DelayMs(30uL));
			wfd serial_handler[1](0x01,devicetype[1]);   //port B ,modbus protocol
      //tm[1] = SEC_TIMER;
			//oldComRunStatus[0] = currentComRunStatus[0];
			//RES(Port_InputStat+98,1);
			if(devicetype[1]==0x00)
        		LEDvalue[1] =0x00;
			else
        		LEDvalue[1] ^=0x01;
			ledOut(0x01, LEDvalue[1]);
	    }
	    costate  cd_portC always_on
	    {  // process serial comm. port C
			waitfor(DelayMs(30uL));
			wfd serial_handler[2](0x02,devicetype[2]);   //port C ,modbus protocol
      //tm[2] = SEC_TIMER;
			//oldComRunStatus[1] = currentComRunStatus[1];
			//RES(Port_InputStat+98,2);
			if(devicetype[2]==0x00)
				LEDvalue[2] =0x00;
			else
        		LEDvalue[2] ^=0x01;
			ledOut(0x02, LEDvalue[2]);
	    }
	    costate  cd_portD always_on
	    {  // process serial comm. port D
     		ser485Tx();
			waitfor(DelayMs(30uL));
			wfd serial_handler[3](0x03,devicetype[3]);   //port D ,modbus protocol
      //tm[3] = SEC_TIMER;
			//oldComRunStatus[2] = currentComRunStatus[2];
			//RES(Port_InputStat+98,3);
      if(devicetype[3]==0x00)
				LEDvalue[3] =0x00;
			else LEDvalue[3] ^=0x01;
				ledOut(0x03, LEDvalue[3]);
	    }
	}
}

⌨️ 快捷键说明

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