网站标志
导航菜单
当前日期时间
当前时间:
购物车
购物车中有 0 件商品 去结算 我的订单
商品搜索
商品搜索:
文章正文
三菱PLC编程口通信C语言源代码
作者: 来源:中国机电工程网    发布于:2019-06-05 19:10:55    文字:【】【】【

fxnew.h

 

#define DELAY_TIMES 30000L

#define TRUE 1
#define FALSE 0

#define TRUE 1
#define FALSE 0

#define FORCE_ON 0x37
#define FORCE_OFF 0x38

void init_plc(void);
int check_plc(void);
int _read_data_register(unsigned int uAddress,unsigned int number);
int _read_mdata_register(unsigned int uAddress,unsigned int number);
int _write_data_register(unsigned int uAddress,unsigned int number);
int _force_m_contact(unsigned int uAddress,unsigned char ucOn_off);
int read_data_register(unsigned int uAddress,unsigned int number);
int read_mdata_register(unsigned int uAddress,unsigned int number);
int write_data_register(unsigned int uAddress,unsigned int number);
int force_m_contact(unsigned int uAddress,unsigned char ucOn_off);
int _read_m_register(unsigned int uAddress,unsigned int number);
int read_m_register(unsigned int uAddress,unsigned int number);

int TESTING=0;

unsigned int uRead_value[25];
unsigned int uWrite_value[25];

unsigned int COMM_PORT=1;
unsigned int STATS_PORT=0x2fd;
unsigned int DATA_PORT=0x2f8;

void init_plc(void)
{       _AX=0xfa;
 _DX=COMM_PORT;
 geninterrupt(0x14);
 while((inportb(STATS_PORT)&1)!=0)inportb(DATA_PORT);


}
int check_plc(void)
{       long lTmp;

 if(TESTING==1)return TRUE;

 init_plc();
 for(lTmp=0L;lTmp<DELAY_TIMES;lTmp++)
 {       if((inportb(STATS_PORT)&0x20)!=0)
   break;
 }

 if(lTmp>=DELAY_TIMES)
  return(FALSE);

 outportb(DATA_PORT,5);
 disable();
 for(lTmp=0L;lTmp<DELAY_TIMES;lTmp++)
 {       if((inportb(STATS_PORT)&1)!=0)
   break;
 }
 if(lTmp>=DELAY_TIMES)
 { enable();
  return(FALSE);
 }

 if((lTmp=inportb(DATA_PORT))==6)
 {  enable();
  return(TRUE);
 }
 else
 {  enable();
  return(FALSE);
 }
}
int read_data_register(unsigned int uAddress,unsigned int number)
{ int i;
 for(i=0;i<3;i++)
  if(_read_data_register(uAddress,number)==TRUE)
   return TRUE;
 return FALSE;
}
int _read_data_register(unsigned int uAddress,unsigned int number)
{       unsigned char uSend[]={2,0x30,0x30,0x30,0x30,0x30,0x30,0x32,3,0x30,0x30};
 unsigned char uReceive[104];
 unsigned int uTmp;
 unsigned int uSum;
 unsigned int num;
 long lTmp;
 int i,j;

 if(TESTING==1)
 {       for(i=0;i<number;i++)uRead_value[i]=0;
  return TRUE;
 }

 init_plc();
 num=number*2;
 if((num/16)>=10)
  uSend[6]=(unsigned char)(num/16+0x41-10);
 else
  uSend[6]=(unsigned char)(num/16+0x30);
 if((num%16)>=10)
  uSend[7]=(unsigned char)((num%16)+0x41-10);
 else
  uSend[7]=(unsigned char)((num%16)+0x30);

 uAddress=uAddress*2+0x1000;

 uTmp=uAddress & 0x000f;
 uSend[5]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 uTmp=(uAddress>>4) & 0x000f;
 uSend[4]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 uTmp=(uAddress>>8) & 0x000f;
 uSend[3]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 uTmp=(uAddress>>12)&0x000f;
 uSend[2]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);

 uSum=0;
 for(i=1;i<9;i++)
  uSum=uSum+(unsigned char)uSend[i];
 uTmp=uSum&0x000f;
 uSend[10]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 uTmp=(uSum>>4)&0x000f;
 uSend[9]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);


 for(i=0;i<11;i++)
 {       for(lTmp=0L;lTmp<DELAY_TIMES;lTmp++)
  {       if((inportb(STATS_PORT)&0x20)!=0)
    break;
  }
  if(lTmp>=DELAY_TIMES)
  {
   
   return(FALSE);
  }

  outportb(DATA_PORT,uSend[i]);
 }
 disable();


 for(lTmp=0;lTmp<DELAY_TIMES;lTmp++)
 {       if((inportb(STATS_PORT)&1)!=0)
   break;
 }
 if(lTmp>=DELAY_TIMES)

浏览 (42) | 评论 (0) | 评分(0) | 支持(0) | 反对(0) | 发布人: 来源:中国机电工程网
将本文加入收藏夹
新闻详情
脚注栏目
|
脚注信息
机电工程网(C) 2015-2020 All Rights Reserved.    联系我们