男女午夜激情_www.天堂av.com_亚洲综合小说区_亚洲欧美自拍视频_成年精品_久久www免费人成—看片_激情综合五月天_久久高潮视频_最新国产成人ab网站_国产精品卡一

當前位置:機電之家首頁 >> PLC技術>> PLC手冊 >> PLC編程手冊 >> 三菱PLC編程口通信C語言源代碼
三菱PLC編程口通信C語言源代碼

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)
 {
  enable();
  return(FALSE);
 }
 uReceive[0]=inportb(DATA_PORT);
 if(uReceive[0]!=2)
 {
  enable();
  return(FALSE);
 }
 for(i=1;i<number*4+4;i++)
 {       for(lTmp=0L;lTmp<DELAY_TIMES;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<number*4+2;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<number;j++)
 { for(i=j*4+1;i<j*4+5;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<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)
 {
  enable();
  return(FALSE);
 }
 uReceive[0]=inportb(DATA_PORT);
 if(uReceive[0]!=2)
 {
  enable();
  return(FALSE);
 }
 for(i=1;i<number*4+4;i++)
 {       for(lTmp=0L;lTmp<DELAY_TIMES;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<number*4+2;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<number;j++)
 { for(i=j*4+1;i<j*4+5;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<number;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<DELAY_TIMES;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<DELAY_TIMES;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<DELAY_TIMES;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<DELAY_TIMES;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<number;i++)uRead_value[i]=0;
  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<DELAY_TIMES;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<DELAY_TIMES;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<number*2+4;i++)
 {       for(lTmp=0L;lTmp<DELAY_TIMES;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<number*2+2;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<number;j++)
 { for(i=j*2+1;i<j*2+3;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;
}


作者:未知 點擊:5610次 [打印] [關閉] [返回頂部]
本文標簽:三菱PLC編程口通信C語言源代碼
* 由于無法獲得聯系方式等原因,本網使用的文字及圖片的作品報酬未能及時支付,在此深表歉意,請《三菱PLC編程口通信C語言源代碼》相關權利人與機電之家網取得聯系。
電子樣本

SN系列樣冊
:鞏經理
:13915946763
:南京塞姆泵業有限公司
個人求購

王經理 【求購】  西湖輪胎  2026-3-20
蘇紅慶 【求購】  工業除濕機  2026-3-20
金喜 【求購】  線纜  2026-3-20
林杰 【求購】  自粘結鐵芯  2026-3-20
任建波 【求購】  鋰電鉆  2026-3-20
劉經理 【求購】  吊具電纜總...  2026-3-20
潘梟 【求購】  高強鋼焊絲  2026-3-19
李經理 【求購】  丹弗斯液壓...  2026-3-18
VIP公司推薦

主站蜘蛛池模板: 河池市| 巢湖市| 义马市| 怀集县| 海丰县| 丰城市| 安岳县| 林甸县| 九台市| 台前县| 德清县| 观塘区| 体育| 阳春市| 高碑店市| 东阿县| 水富县| 历史| 仁化县| 百色市| 阳朔县| 彩票| 山东| 民勤县| 桐城市| 烟台市| 桓台县| 开江县| 潜江市| 滨州市| 沅江市| 星座| 汕尾市| 鹤峰县| 扶风县| 琼中| 井研县| 江油市| 长宁县| 霍林郭勒市| 酉阳|