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

📄 zlp.cpp

📁 基于PC104平台
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                 File name: zlp.cpp
//  This file define the functions used in new bb
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "zlp.h"
#define READWORD 0x03 // modbus command don't modify
void CZlp::initial(BYTE portno)
{
	iportno=portno;  //set current port no
	dataque.start=0;
	dataque.next=0;
	dataque.count=0;
	dataque.size=256;

	isatimer[iportno].querycount=0;
	isatimer[iportno].querylimit=port_set[iportno].query_gap;
	isatimer[iportno].querylimit*=1000;
	isatimer[iportno].querylimit/=220;

	for(int j=0; j<32; j++) zlp_yc[j] = 0;
	query_flag = TRUE;
	modbusaddr = 0;
}

void CZlp::putdata(BYTE data)
{
  dataque.buf[dataque.next++]=data&0xff;
  if(dataque.next>=dataque.size)dataque.next=0;
  dataque.count++;
  if(dataque.count>dataque.size) {
	dataque.count=dataque.size;
	dataque.start++;
	if(dataque.start>=dataque.size)dataque.start=0;
  }
}

void CZlp::dropdata()
{
  if(dataque.count>0) {
	dataque.start++;
	if(dataque.start>=dataque.size)dataque.start=0;
	dataque.count--;
  }
}

void CZlp::getzlpinfo()
{
  int i;
  BYTE temp_buf[256];
  unsigned int temp,temp1;
  unsigned int templen;
  while( get_in( iportno ) ) {
	putdata( getchin[ iportno ] );
	if( displaymode == TRUE )
	disptbuf( getchin[ iportno ] & 0xff , iportno );
  }

  while( ( dataque.count >= 2 )
	&& ( ( dataque.buf[dataque.start] != modbusaddr )
	|| ( dataque.buf[dataque.start+1] != currentCMD ) ) )
	dropdata();

  templen = dataque.buf[ dataque.start + 2 ] + 5;

  if( dataque.count < templen )	return;

  for( i = 0; i < templen; i++ ) {
	temp_buf[ i ] = dataque.buf[ dataque.start ];
//	disptbuf( temp_buf[i], iportno );  //tmp
	dropdata();
  }

  temp = crc16( temp_buf , templen - 2 );
  temp1 =( ( temp_buf[templen - 2] & 0xff ) << 8 ) | temp_buf[templen - 1] & 0xff;
   if( temp == temp1 )
   {
	if (displaymode == FALSE)
   for( i = 0; i < templen; i++ ) {	disptbuf( temp_buf[ i ], iportno );}
	 switch (currentCMD)
	 {
	  case 0x03:  process_zlp( temp_buf ); break;
	  case 0x04:  process_ABB_yc( temp_buf ); break;
	  case 0x02:  process_ABB_yx( temp_buf ); break;
	  default:                              break;
	 }
       }
}


void CZlp::process_zlp(BYTE* pointer)
{
  zlp_yx[0] = ((*(pointer + 4) <<8) & 0xff00) + (*(pointer + 3) & 0xff );
  zlp_yc[0] = (((*(pointer + 5) <<8) & 0xff00) + (*(pointer + 6) & 0xff ))/2;
  zlp_yc[1] = ((*(pointer +7) <<8) & 0xff00) + (*(pointer + 8) & 0xff );
  zlp_yc[2] = (((*(pointer + 9) <<8) & 0xff00) + (*(pointer + 10) & 0xff ))/2;
  zlp_yc[3] = ((*(pointer + 15) <<8) & 0xff00) + (*(pointer + 16) & 0xff );
  zlp_yc[4] = (((*(pointer + 17) <<8) & 0xff00) + (*(pointer + 18) & 0xff ))/2;
  zlp_yc[5] = (((*(pointer + 21) <<8) & 0xff00) + (*(pointer + 22) & 0xff ))/2;
 // zlp_yc[6] = ((*(pointer + 33) <<8) & 0xff00) + (*(pointer + 34) & 0xff );
}
void CZlp::process_ABB_yx(BYTE* pointer)
{
  unsigned int test1,test2,test3,temp,tempadd,temp1,temp2,temp3;
  unsigned int connected,close,charged;
  connected = 0x0020;
  close = 0x0008;
  charged = 0x0040;
  tempadd = *pointer;
  temp = ((*(pointer + 4) <<8) & 0xff00) + (*(pointer + 3) & 0xff );
  temp1 = temp & close;
  temp2 = temp & connected;
  temp3 = temp & charged;
  test1 = 0x0001; test2 = 0x0001; test3 = 0x0001;
  test1 = test1 << (tempadd-2) * 3;
  test2 = test2 << ((tempadd-2) * 3 + 1);
  test3 = test3 << ((tempadd-2) * 3 + 2);
  if( temp1 ) zlp_yx[1] |= test1;
  else zlp_yx[1] &= ~test1;
  if( temp2 ) zlp_yx[1] |= test2;
  else zlp_yx[1] &= ~test2;
  if( temp3 ) zlp_yx[1] |= test3;
  else zlp_yx[1] &= ~test3;
}

void CZlp::process_ABB_yc(BYTE* pointer)
{
  int tempadd = *pointer;
  if(tempadd>5) return;
  if(tempadd<2) return;
  zlp_yc[tempadd*3+0] = ((*(pointer + 3) <<8) & 0xff00) + (*(pointer + 4) & 0xff );  //Ia
  zlp_yc[tempadd*3+1] = ((*(pointer + 5) <<8) & 0xff00) + (*(pointer + 6) & 0xff );  //Ib
  zlp_yc[tempadd*3+2] = ((*(pointer + 7) <<8) & 0xff00) + (*(pointer + 8) & 0xff );  //Ic
}

unsigned int CZlp::crc16(BYTE *msg,BYTE len)
{
  unsigned char uindex;
  unsigned char uchhi=0xff;
  unsigned char uchlo=0xff;
  while(len--) {
	uindex = uchhi ^ *msg++;
	uchhi = uchlo ^ auchhi[uindex] ;
	uchlo = auchlo[uindex] ;
  }
  return (uchhi << 8 | uchlo) ;
}

void CZlp::sendquery()
{
  for(int i=0;i<8;i++) {   // modbus query standard only 8 byte
	if(i==7) quick_send=TRUE; //must send quickly
	comput(askbuf[i],iportno);
	disprbuf(askbuf[i],iportno);
  }//for(i=0;i<7;i++)
}



void CZlp::queryinfo()
{
  unsigned int temp;
  if( ++modbusaddr > 5 ) modbusaddr = 1;
  if(modbusaddr==1)  {
    currentCMD = 0x03;
    askbuf[0] = modbusaddr;      //modbusaddr;
    askbuf[1] = currentCMD;
    askbuf[2] = 0x00; // data address
    askbuf[3] = 0x63; // data address
    askbuf[4] = 0x00; // high byte
    askbuf[5] = 0x0c; // read word number
    temp = crc16 ( askbuf , 6 );
    askbuf[6] = ( temp >> 8 ) & 0xff;
    askbuf[7] = temp & 0xff;
  }
  else{
      if(query_flag==TRUE){
	currentCMD = 0x02;
	askbuf[0] = modbusaddr;      //modbusaddr;
	askbuf[1] = currentCMD;
	askbuf[2] = 0x00; // data address
	askbuf[3] = 0x00; // data address
	askbuf[4] = 0x00; // high byte
	askbuf[5] = 0X14; // read byte number
	temp = crc16 ( askbuf , 6 );
	askbuf[6] = ( temp >> 8 ) & 0xff;
	askbuf[7] = temp & 0xff;
	if (modbusaddr == 5) query_flag=FALSE;
	}
       else{
	currentCMD = 0x04;
	askbuf[0] = modbusaddr;      //modbusaddr;
	askbuf[1] = currentCMD;
	askbuf[2] = 0x00; // data address
	askbuf[3] = 0x64; // data address
	askbuf[4] = 0x00; // high byte
	askbuf[5] = 0x04; // read byte number
	temp = crc16 ( askbuf , 6 );
	askbuf[6] = ( temp >> 8 ) & 0xff;
	askbuf[7] = temp & 0xff;
	if (modbusaddr == 5 ) query_flag=TRUE;
       }
  }
  sendquery();
}

void CZlp::deal()
{
  if(isatimer[iportno].querycount>isatimer[iportno].querylimit) {
	if ( port_free( iportno ) ) {
	  isatimer[iportno].querycount=0;
	  isatimer[iportno].querylimit=port_set[iportno].query_gap*1000L/220L;
	  queryinfo();
	}
  }
  getzlpinfo();
}





⌨️ 快捷键说明

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