安全生产
首页资讯供应求购招商招聘展会社区
长期信息合作请联系:QQ66821730
PLC技术首页-->PLC基础plc应用PLC文案资料PLC手册国标规程plc资料下载技术培训人才招聘职业认证产品商情论坛
PLC搜索
我 要 找
标题 内容 作者
PLCRSS订阅PLC技术信息
电工技术资料 您的位置: 机电之家-->plc技术资料栏目首页-> PLC基础 -> PLC通信 --> 三菱FX系列PLC编程口通信源代码
阅读plc技术相关资料
三菱FX系列PLC编程口通信源代码
本文作者 不详 摘自 机电之家

fx_comm.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   { if((inportb(STATS_PORT)&0x20)!=0)
         break;
   }
   if(lTmp>=DELAY_TIMES)
      return(FALSE);
   outportb(DATA_PORT,5);
   disable();
   for(lTmp=0L;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      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      { if((inportb(STATS_PORT)&0x20)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         
         return(FALSE);
      }
      outportb(DATA_PORT,uSend[i]);
   }
   disable();
 
   for(lTmp=0;lTmp   { if((inportb(STATS_PORT)&1)!=0)
         break;
   }
   if(lTmp>=DELAY_TIMES)
   {
      enable();
      return(FALSE);
   }
   uReceive[0]=inportb(DATA_PORT);
   if(uReceive[0]!=2)
   {
      enable();
      return(FALSE);
   }
   for(i=1;i   { for(lTmp=0L;lTmp      { if((inportb(STATS_PORT)&1)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         enable();
         return(FALSE);
      }
      uReceive[i]=inportb(DATA_PORT);
   }
   enable();
   uSum=0;
   for(i=1;i      uSum=uSum+(unsigned int)uReceive[i];
   uTmp=uSum&0xf;
   uTmp=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 
   if((unsigned char)uTmp!=uReceive[number*4+3]) return(FALSE);
   uTmp=(uSum>>4)&0xf;
   uTmp=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 
   if((unsigned char)uTmp!=uReceive[number*4+2]) return(FALSE);
   for(j=0;j   {   for(i=j*4+1;i         uReceive[i]=(uReceive[i]>0x39)?uReceive[i]-0x41+0xa:uReceive[i]-0x30;
      uRead_value[j]=(((((uReceive[j*4+3]<<4)+uReceive[j*4+4])<<4)+uReceive[j*4+1])<<4)+uReceive[j*4+2];
   }
   return TRUE;
}
//返回顶部

int read_mdata_register(unsigned int uAddress,unsigned int number)
{   int i;
   for(i=0;i<3;i++)
      if(_read_mdata_register(uAddress,number)==TRUE)
         return TRUE;
   return FALSE;
}
//返回顶部


int _read_mdata_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      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      { if((inportb(STATS_PORT)&0x20)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         
         return(FALSE);
      }
      outportb(DATA_PORT,uSend[i]);
   }
   disable();
 
   for(lTmp=0;lTmp   { if((inportb(STATS_PORT)&1)!=0)
         break;
   }
   if(lTmp>=DELAY_TIMES)
   {
      enable();
      return(FALSE);
   }
   uReceive[0]=inportb(DATA_PORT);
   if(uReceive[0]!=2)
   {
      enable();
      return(FALSE);
   }
   for(i=1;i   { for(lTmp=0L;lTmp      { if((inportb(STATS_PORT)&1)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         enable();
         return(FALSE);
      }
      uReceive[i]=inportb(DATA_PORT);
   }
   enable();
   uSum=0;
   for(i=1;i      uSum=uSum+(unsigned int)uReceive[i];
   uTmp=uSum&0xf;
   uTmp=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 
   if((unsigned char)uTmp!=uReceive[number*4+3])return(FALSE);
   uTmp=(uSum>>4)&0xf;
   uTmp=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 
   if((unsigned char)uTmp!=uReceive[number*4+2])return(FALSE);
   for(j=0;j   {   for(i=j*4+1;i         uReceive[i]=(uReceive[i]>0x39)?uReceive[i]-0x41+0xa:uReceive[i]-0x30;
      uRead_value[j]=(((((uReceive[j*4+3]<<4)+uReceive[j*4+4])<<4)+uReceive[j*4+1])<<4)+uReceive[j*4+2];
   }
   return TRUE;
}
//返回顶部


int write_data_register(unsigned int uAddress,unsigned int number)
{   int i;
   for(i=0;i<3;i++)
      if(_write_data_register(uAddress,number)==TRUE)
         return TRUE;
   return FALSE;
}
//返回顶部


int _write_data_register(unsigned int uAddress,unsigned int number)
{ unsigned char uSend[111];
   unsigned int uTmp,uSum,num;
   long lTmp;
   int i;
   if(TESTING==1)return TRUE;
 
   init_plc();
   uSend[0]=2;
   uSend[1]=0x31;
   uSend[number*4+8]=3;
   num=(number*2)/16;
   if(num>=10)uSend[6]=num+0x41-10;
   else    uSend[6]=num+0x30;
   num=(number*2)%16;
   if(num>=10)uSend[7]=num+0x41-10;
   else uSend[7]=num+0x30;
   uAddress=0x1000+2*uAddress;
   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);
   for(i=0;i   {
      uTmp=uWrite_value[i]&0x000f;
      uSend[i*4+9]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
      uTmp=(uWrite_value[i]>>4)&0x000f;
      uSend[i*4+8]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
      uTmp=(uWrite_value[i]>>8)&0x000f;
      uSend[i*4+11]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
      uTmp=(uWrite_value[i]>>12)&0x000f;
      uSend[i*4+10]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   }
   uSum=0;
   for(i=1;i<9+number*4;i++)
      uSum+=uSend[i];
   uTmp=uSum&0x000f;
   uSend[number*4+10]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   uTmp=(uSum>>4)&0x000f;
   uSend[number*4+9]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   for(i=0;i<11+number*4;i++)
   { for(lTmp=0L;lTmp      { if((inportb(STATS_PORT)&0x20)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         /*enable();*/
         return(FALSE);
      }
      outportb(DATA_PORT,uSend[i]);
   }
   disable();
   for(lTmp=0L;lTmp   { if((inportb(STATS_PORT)&1)!=0)
         break;
   }
   if(lTmp>=DELAY_TIMES)
   {
       enable();
      return(FALSE);
   }
   if(inportb(DATA_PORT)!=6)
   {
      enable();
      return(FALSE);
   }
   else
   {
      enable();
      return(TRUE);
   }
}


int force_m_contact(unsigned uAddress,unsigned char ucOn_off)
{   int i;
   for(i=0;i<3;i++)
      if(_force_m_contact(uAddress,ucOn_off)==TRUE)
         return TRUE;
   return FALSE;
}
//返回顶部


int _force_m_contact(unsigned uAddress,unsigned char ucOn_off)
{ unsigned uSend[]={2,0x37,0x30,0x30,0x30,0x30,3,0x30,0x30};
   unsigned uTmp,uSum,i;
   long lTmp;
   if(TESTING==1)return TRUE;
   init_plc();
   uAddress=uAddress+0x800;
   uSend[1]=ucOn_off;
   uTmp=uAddress&0x000f;
   uSend[3]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   uTmp=(uAddress>>4)&0x000f;
   uSend[2]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   uTmp=(uAddress>>8)&0x000f;
   uSend[5]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   uTmp=(uAddress>>12)&0x000f;
   uSend[4]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   uSum=0;
   for(i=1;i<7;i++)
      uSum+=uSend[i];
   uTmp=uSum&0x000f;
   uSend[8]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   uTmp=(uSum>>4)&0x000f;
   uSend[7]=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
   for(i=0;i<9;i++)
   { for(lTmp=0L;lTmp      { if((inportb(STATS_PORT)&0x20)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         enable();
         return(FALSE);
      }
      outportb(DATA_PORT,uSend[i]);
   }
   disable();
   for(lTmp=0L;lTmp   { if((inportb(STATS_PORT)&1)!=0)
         break;
   }
   if(lTmp>=DELAY_TIMES)
   {
      enable();
      return(FALSE);
   }
   if(inportb(DATA_PORT)!=6)
   {
      enable();
      return(FALSE);
   }
   else
   { enable();
      return(TRUE);
   }
}
//返回顶部


int read_m_register(unsigned int uAddress,unsigned int number)
{   int i;
   for(i=0;i<3;i++)
      if(_read_m_register(uAddress,number)==TRUE)
         return TRUE;
   return FALSE;
}
//返回顶部


int _read_m_register(unsigned int uAddress,unsigned int number)
{ unsigned char uSend[]={2,0x30,0x30,0x30,0x30,0x30,0x30,0x32,3,0x30,0x30};
   unsigned char uReceive[54];
   unsigned int uTmp;
   unsigned int uSum;
   unsigned int num;
   long lTmp;
   int i,j;
   if(TESTING==1)
   { for(i=0;i      return TRUE;
   }
   init_plc();
   num=number;
   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/8+0x100;
   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      { if((inportb(STATS_PORT)&0x20)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         /*enable();*/
         return(FALSE);
      }
      outportb(DATA_PORT,uSend[i]);
   }
   disable();
 
 
   for(lTmp=0;lTmp   { if((inportb(STATS_PORT)&1)!=0)
         break;
   }
   if(lTmp>=DELAY_TIMES)
   {
      enable();
      return(FALSE);
   }
   uReceive[0]=inportb(DATA_PORT);
   if(uReceive[0]!=2)
   {
      enable();
      return(FALSE);
   }
   for(i=1;i   { for(lTmp=0L;lTmp      { if((inportb(STATS_PORT)&1)!=0)
            break;
      }
      if(lTmp>=DELAY_TIMES)
      {
         enable();
         return(FALSE);
      }
      uReceive[i]=inportb(DATA_PORT);
   }
   enable();
   uSum=0;
   for(i=1;i      uSum=uSum+(unsigned int)uReceive[i];
   uTmp=uSum&0xf;
   uTmp=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 
   if((unsigned char)uTmp!=uReceive[number*2+3]) return(FALSE);
   uTmp=(uSum>>4)&0xf;
   uTmp=(uTmp<10)?(uTmp+0x30):(uTmp+0x41-0xa);
 
   if((unsigned char)uTmp!=uReceive[number*2+2]) return(FALSE);
   for(j=0;j   {   for(i=j*2+1;i         uReceive[i]=(uReceive[i]>0x39)?uReceive[i]-0x41+0xa:uReceive[i]-0x30;
      uRead_value[j]=((uReceive[j*2+1])<<4)+uReceive[j*2+2];
   }
   return TRUE;
}



有搞不定的程序,交给机电之家吧!去悬赏竞标
 三菱FX系列PLC编程口通信源代码相关资料
  • 用三菱的FX2N PLC实现N:N网络(令牌总线)
  • PLC程序调试步骤
  • 三菱PLC的几个常见问题解答
  • PLC内置高速计数器的简单应用介绍
  • PC与FX30DUE传输数据办法
  • 变频器逆变器件的原理
  • 三菱PLC程序编写注意事项一例
  • 三菱FX,A,QnA系列PLC特殊寄存器查询软件
  • 三菱FX2N系列PLC与Profibus的连接
  • 计数器级联PLC程序梯形图 
  • ⊕这地方投资政策最优
    ⊕上千份机电行业研究报告
    ⊕机电项目招商啦
    ⊕谁把我买了?
    ⊕机电行业展会大全
    ⊕十万企业抢登行业网址大全
    机电之家会议开通
    ⊕每日最新求购信息
    ⊕电工技术资料为了谁?
    ⊕机电设备维修与管理
    机电之家(中国)plc技术资料中心资讯版权声明:
    1、凡注明“机电之家采编”字样的所有作品均系本网原创,版权归机电之家所有,任何媒体摘编或享用本作品,需注明文章来源。违反声明者,本网将追究其相关法律责任。
    2、凡本网注明“来源:XXX网(非本网)”的作品,均转载自其他媒体,目的在于传达更多资讯,本网不承担相关法律责任。

    3、如在资讯、广告等方面想与本网合作,请致电:0571-87774297。Email:donemi@hz.cn

    ·工程项目经理培训
    ·欧姆龙PLC编程维护培训
    ·杭州西门子PLC应用培训
    ·模具加工设计培训
    ·变频器维修培训
    ·安全员认证培训
    ·电工培训


    ·招聘项目管理人员
    ·首席技术执行官
    ·自控工程师
    ·数控编程学徒
    ·总工程师

    项目竞标

    最新商业情报
    代理
    [代理] 寻求地区代理
    [代理] 电工产品诚招代理..
    采购
    [采购] 电动车控制器外壳
    [采购] 高品质缓冲器
    论坛最新话题
    ·超级搞笑的安全事故
    ·电工技术资料flash大全
    ·最新的搞笑图片
    ·工控行业应用软件下载
    首页
    首页
    合作网站:
    | 中国机电网机电之家安全生产网 | 机电论文 | 机电论坛 | 机电设备贸易 | 机电网址大全 | 浙江机电网 | 陕西机电网 | 变频器技术网 |
    中国电工网 | 电工网 |环球会展网机电产品网 | 机电人才网 | 中国工控网 | 五金工具网 | 安全生产网 | 甘肃机电网 | PLC技术网 |
    友情连接:
    | 中国机电网 | 中国工控网 | 行业培训网 | 中国工程机械网 | 机电一体化网 | 行业下载网 | 行业国标网 | 商业情报站 |
    关于我们 | 联系我们 | 广告联系 | 付款方式 | 使用帮助 | PLC技术网 | 会员助手 | 友情链接
    电话:0571-87774297(杭州) 传真:0571-87774298(杭州)点击这里给我发消息66821730(技术) 点击这里给我发消息58733127(审核)
    机电之家 PLC技术网 站所共享的PLC知识,PLC技术,PLC应用,PLC行情分析,PLC学习资料,PLC国标规程,PLC维修知识,
    PLC国家标准,PLC操作规程,PLC岗位职责,PLC管理制度,PLC工作总结,PLC实习报告,PLC考试题库,
    等都是来自会员发表或 网上收集整理。如果有任何侵犯您权益的地方,请联系我们,我们将马上进行处理。
    企业登陆可自行免费发布资料,本站代发布邮箱为88ctv@163.com
    Copyright 2007 plc.jdzj.com Inc All Rights Reserved.PLC技术网
    chinaplc.net 联合建设
    技术支持:PLC技术网 mailto:88ctv@163.com
    免费发布信息主办:浙江-杭州-PLC技术网网络运营部安全生产