base.c

来自「该程序实现N4F接口程序」· C语言 代码 · 共 558 行 · 第 1/2 页

C
558
字号


#include "..\\inc\\comm.h"
#include "..\\inc\\n4f.h"


extern char     sendbuf[BUFSIZE];
extern char     rcvbuf[BUFSIZE];
extern int      rcvlen;
extern HANDLE   hCom;
extern float    f_value, ff_value;
extern char     scan_type_id;
extern int      soe_flag;

void Endproc()
{ CloseHandle( hCom );
  printf(" ---------  rcomm.dxe stop. --------\n"); 
  UnMountDB(); Lan_finish();
  exit(0);
}


void sleep(int seconds)
{ 
  Sleep(seconds*1000); 
}


int Get_ms(ABS_TIME abs_time)  /* int=32 bit */
{ int             t_ms;
  t_ms=(abs_time.hour*3600+abs_time.minute*60+abs_time.second)*1000+abs_time.ms;
  return(t_ms);
}

int Disp_time( char *ID )
{ struct tm           *datetime;
  long t;
  t=time(NULL); datetime=localtime(&t);
  if( strcmp(ID,"DISP")==0 )
  { printf("(%d/%d/%d %d:%d:%d)\n",datetime->tm_year+1900,datetime->tm_mon+1,
            datetime->tm_mday,datetime->tm_hour,datetime->tm_min,datetime->tm_sec);
  }
  else
  {
    return( datetime->tm_min );
  }
}
          
                        
void Bufs_merge(int position, char *assemble_buf, int assemble_buf_len)
{ 
  memcpy( sendbuf+position, assemble_buf, assemble_buf_len );
}


int Port_init()	   
{ DCB            dcb;
  COMMTIMEOUTS   timeout;
  BOOL           fSuccess;
  
  /* 打开串口 */
  hCom=CreateFile
  (  COMM_PORT,GENERIC_READ | GENERIC_WRITE,
     0,    /* comm device must be opened with exclusive access */
     NULL, /* no security attrs */
     OPEN_EXISTING, /* comm device must use OPEN_EXISTING */
     0,    /* not overlapped I/O */
     NULL  /* hTemplate must be NULL  for comm device */
  );
  if(hCom==INVALID_HANDLE_VALUE)
  { printf("%s can't open!\n",COMM_PORT); return( FAILURE ); }
  /* 读串口设置 */
  fSuccess=GetCommState(hCom,&dcb);
  if( !fSuccess )
  { printf("can't get %s state!\n",COMM_PORT); return( FAILURE ); }
  /*Fill in the DCB: baud=300,8 data bits,even party,1 stop bit*/
  dcb.BaudRate=1200;
  dcb.ByteSize=8;
  dcb.Parity  =NOPARITY;
  dcb.StopBits=ONESTOPBIT;
  /*设置串口新的参数值 */
  fSuccess=SetCommState(hCom, &dcb);
  if( !fSuccess )
  { printf("can't set %s state!\n",COMM_PORT); return( FAILURE ); }
  /* 读串口原始超时参数设置 */
  fSuccess=GetCommTimeouts(hCom,&timeout);
  if(!fSuccess)
  { printf("GetCommTimeouts() error !\n"); return( FAILURE ); }
  timeout.ReadIntervalTimeout       = 500;//2000;      /* ms, max time between two bytes received */
  timeout.ReadTotalTimeoutMultiplier= TIMEOUT_SECONDS;        
  timeout.ReadTotalTimeoutConstant  = 1000;//1000*60*1; /* ms */
  fSuccess=SetCommTimeouts(hCom,&timeout);
  if(!fSuccess)
  { printf("SetCommTimeouts() error !\n"); return( FAILURE ); }
  printf(">>>> %s INIT OK.\n",COMM_PORT); return( OK );
}


/*-------  读串口 --------*/
int Read_in()
{ int    i, j, num;
  BOOL   fSuccess;
  FILE   *test_fp;
  	   		 
  memset(rcvbuf,0,BUFSIZE); 
  printf("\nWAITING_FOR_RECEIVE ....\n\n");

#ifdef RCV_TEST //调试时,从pd_test.txt文件中读网调命令
  num=0;
  while((test_fp=fopen("c:\\users\\ems\\system\\n4f\\v3.0a\\src\\pd_test.txt","r"))==NULL){ sleep(3); }
  for(;;)
  { fscanf(test_fp,"%x",&i );
	if( (rcvbuf[num]=(char)i)==0x77 )
	{ fclose(test_fp);   break; } 
    else 
	{ num++;   continue;        }    
  }
  system("del c:\\users\\ems\\system\\n4f\\v3.0a\\src\\pd_test.txt");
  if( (rcvbuf[1]==0x05)||(rcvbuf[1]==0x1a) )
  { Get_crc8( /*1020num+1*/num,rcvbuf );  num=num+1; }
  else
  { Get_crc16( num,rcvbuf ); num=num+2; }
#else
  fSuccess=ReadFile(hCom,rcvbuf,BUFSIZE,&num,NULL);
  //printf("!!!!!!!!!!num =%d\n",num);
  if( !fSuccess )
  { printf("ReadFile() error. \n"); 
    CloseHandle( hCom );  exit(0); 
	//zm010608 Sleep(100); return(FAILURE);    
  } 
  if( num==0 )   
  { printf("\n#########  READ_TIMEOUT  #########\n"); 
    CloseHandle( hCom ); exit(0); 
	//zm010608 return(TIMEOUT);
  } 
#endif

  printf("\nFROM_%s:%d bytes received. ",COMM_PORT,num);Disp_time("DISP"); 
  rcvlen=num;  
  j=0; 
  while( j<num )
  { for(i=0;i<8;i++)
	{ 
	  if(j<num){ printf("%8x ",rcvbuf[j]); j++; } 
	}	
    printf("\n");
  }	

  if( Check_crc()!=OK )
  { /*Send_NCK( 0x00 );*/return( FAILURE ); }
   
  if(  ( rcvbuf[0]!=(char)RTU_LOCAL_ADDR )
	 &&( rcvbuf[0]!=(char)RTU_BROADCAST_ADDR )  )
  { printf("RTU_ADDR_ERROR !\n"); return( FAILURE ); }
  
  return( OK );
}

int Write_out( int n )
{ char   tmpbuf[BUFSIZE],*tmpbuf_p;
  BOOL   fSuccess;
  int    i, j, num;
  memcpy( tmpbuf, sendbuf, BUFSIZE ); 
  tmpbuf_p=tmpbuf; 
  Wait_before_send();
  fSuccess=WriteFile(hCom,tmpbuf_p,n,&num,NULL);
  //printf("num =%d\n",num);
  if( !fSuccess ) return( FAILURE );
  printf("\n======>>> WriteFile(): without crc len=%d (TYPE_%02xH)\n",num,(sendbuf[1] & 0x3f) );
#ifdef RCV_TEST
  j=0; 
  while( j<num )
  { printf("======>>> "); 
    for(i=0;i<6;i++)
	{ 
	  if(j<num){ printf("%8x ",*(tmpbuf+j)); j++; } 
	}	
    printf("\n");
  }	
#endif  
  return( OK ); 
}


/*------- 发送数据 带8位CRC效验 --------*/
int Write_out_crc8( int n )
{ char   tmpbuf[BUFSIZE],*tmpbuf_p;
  BOOL   fSuccess;
  int    i, j, num;
  memcpy( tmpbuf, sendbuf, BUFSIZE ); 
  tmpbuf_p=tmpbuf; 
  //1020 Get_crc8( n+1, tmpbuf_p );
  //1020 fSuccess=WriteFile(hCom,tmpbuf_p,n+1,&num,NULL);
  Get_crc8( n, tmpbuf_p );
  fSuccess=WriteFile(hCom,tmpbuf_p,n+1,&num,NULL);
  if( !fSuccess ) return( FAILURE );
  printf("\n======>>> WriteFile(): with crc8 len=%d (TYPE_%02xH)\n",num,(sendbuf[1] & 0x3f) );
#ifdef RCV_TEST
  j=0; 
  while( j<num )
  { printf("======>>> "); 
    for(i=0;i<6;i++)
	{ 
	  if(j<num){ printf("%8x ",*(tmpbuf+j)); j++; } 
	}	
    printf("\n");
  }	
#endif    
  return( OK ); 
}


/*------- 发送数据 带16位CRC效验 --------*/
int Write_out_crc16( int n )
{ char   tmpbuf[BUFSIZE],*tmpbuf_p;
  BOOL   fSuccess;
  int    i, j, num;
  memcpy( tmpbuf, sendbuf, BUFSIZE ); 
  tmpbuf_p=tmpbuf;
  Get_crc16( n, tmpbuf_p );
  Wait_before_send();
  fSuccess=WriteFile(hCom,tmpbuf_p,n+2,&num,NULL);
  if( !fSuccess ) return( FAILURE );
  printf("\n======>>> WriteFile(): with crc16 len=%d (TYPE_%02xH)\n",num,(sendbuf[1] & 0x3f) );
//#ifdef RCV_TEST
  j=0; 
  while( j<num )
  { printf("======>>> "); 
    for(i=0;i<6;i++)
	{ 
	  if(j<num){ printf("%8x ",*(tmpbuf+j)); j++; } 
	}	
    printf("\n");
  }	
//#endif  
  return( OK ); 
}

void Wait_before_send()
{ Sleep(1); }

//=========================================================================//

/*-------- 存放在INT_STRU结构中的整型数变浮点数 --------*/
void Int_to_float( INT_STRU *int_stru )
{
  switch( int_stru->format )
  { case 0:   f_value = (float)(int_stru->value/1e0); break;
    case 1:   f_value = (float)(int_stru->value/1e1); break;
    case 2:   f_value = (float)(int_stru->value/1e2); break;
    case 3:   f_value = (float)(int_stru->value/1e3); break;
    default:  f_value = (float)(int_stru->value/1e0); break;
  }
  if(int_stru->sign!=POSITIVE){ f_value = f_value*( (float)-1 ); }
}


/*-------- 浮点数变整型数,存放在INT_STRU结构中 --------*/
extern int_stru;
void Float_to_int(INT_STRU *int_stru)
{ float     z, value;
  int       i, j, k;
 
  value=ff_value;
  if(value>=0)
  { int_stru->sign=DATA_POSITIVE; }
  else
  { int_stru->sign=DATA_NEGATIVE; value=value*( (float)-1 ); }

  if( value<(1e1) ){j=3;}
  else if( value<(1e2) ){j=2;}
       else if( value<(1e3) ){j=1;}
            else if( value<(1e4) ){j=0;}
                 else {                j=0;}
  z=value; 
  for(i=0;i<j;i++){ z=z*10; }  k=(int )z; 
  for(i=j;i>0;i--){ if((k%10)!=0)break; k=k/10; }
  switch( i )
  { case 1: int_stru->format=XXX_X;int_stru->value=(int)(value*1e1);break;

⌨️ 快捷键说明

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