三菱PLC编程口通信C语言源代码三菱plc.docx

上传人:b****1 文档编号:13217185 上传时间:2023-06-12 格式:DOCX 页数:20 大小:15.70KB
下载 相关 举报
三菱PLC编程口通信C语言源代码三菱plc.docx_第1页
第1页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第2页
第2页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第3页
第3页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第4页
第4页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第5页
第5页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第6页
第6页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第7页
第7页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第8页
第8页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第9页
第9页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第10页
第10页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第11页
第11页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第12页
第12页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第13页
第13页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第14页
第14页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第15页
第15页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第16页
第16页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第17页
第17页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第18页
第18页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第19页
第19页 / 共20页
三菱PLC编程口通信C语言源代码三菱plc.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

三菱PLC编程口通信C语言源代码三菱plc.docx

《三菱PLC编程口通信C语言源代码三菱plc.docx》由会员分享,可在线阅读,更多相关《三菱PLC编程口通信C语言源代码三菱plc.docx(20页珍藏版)》请在冰点文库上搜索。

三菱PLC编程口通信C语言源代码三菱plc.docx

三菱PLC编程口通信C语言源代码三菱plc

三菱PLC编程口通信C语言源代码-三菱plc

fxnew.h

#defineDELAY_TIMES30000L

#defineTRUE1

#defineFALSE0

#defineTRUE1

#defineFALSE0

#defineFORCE_ON0x37

#defineFORCE_OFF0x38

voidinit_plc(void);

intcheck_plc(void);

int_read_data_register(unsignedintuAddress,unsignedintnumber);

int_read_mdata_register(unsignedintuAddress,unsignedintnumber);

int_write_data_register(unsignedintuAddress,unsignedintnumber);

int_force_m_contact(unsignedintuAddress,unsignedcharucOn_off);

intread_data_register(unsignedintuAddress,unsignedintnumber);

intread_mdata_register(unsignedintuAddress,unsignedintnumber);

intwrite_data_register(unsignedintuAddress,unsignedintnumber);

intforce_m_contact(unsignedintuAddress,unsignedcharucOn_off);

int_read_m_register(unsignedintuAddress,unsignedintnumber);

intread_m_register(unsignedintuAddress,unsignedintnumber);

intTESTING=0;

unsignedintuRead_value[25];

unsignedintuWrite_value[25];

unsignedintCOMM_PORT=1;

unsignedintSTATS_PORT=0x2fd;

unsignedintDATA_PORT=0x2f8;

voidinit_plc(void)

{_AX=0xfa;

_DX=COMM_PORT;

geninterrupt(0x14);

while((inportb(STATS_PORT)1)!

=0)inportb(DATA_PORT);

}

intcheck_plc(void)

{longlTmp;

if(TESTING==1)returnTRUE;

init_plc();

for(lTmp=0L;lTmpDELAY_TIMES;lTmp++)

{if((inportb(STATS_PORT)0x20)!

=0)

break;

}

if(lTmp=DELAY_TIMES)

return(FALSE);

outportb(DATA_PORT,5);

disable();

for(lTmp=0L;lTmpDELAY_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);

}

}

intread_data_register(unsignedintuAddress,unsignedintnumber)

{inti;

for(i=0;i3;i++)

if(_read_data_register(uAddress,number)==TRUE)

returnTRUE;

returnFALSE;

}

int_read_data_register(unsignedintuAddress,unsignedintnumber)

{unsignedcharuSend[]={2,0x30,0x30,0x30,0x30,0x30,0x30,0x32,3,0x30,0x30};

unsignedcharuReceive[104];

unsignedintuTmp;

unsignedintuSum;

unsignedintnum;

longlTmp;

inti,j;

if(TESTING==1)

{for(i=0;inumber;i++)uRead_value[i]=0;

returnTRUE;

}

init_plc();

num=number*2;

if((num/16)=10)

uSend[6]=(unsignedchar)(num/16+0x41-10);

else

uSend[6]=(unsignedchar)(num/16+0x30);

if((num%16)=10)

uSend[7]=(unsignedchar)((num%16)+0x41-10);

else

uSend[7]=(unsignedchar)((num%16)+0x30);

uAddress=uAddress*2+0x1000;

uTmp=uAddress0x000f;

uSend[5]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress4)0x000f;

uSend[4]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress8)0x000f;

uSend[3]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress12)0x000f;

uSend[2]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uSum=0;

for(i=1;i9;i++)

uSum=uSum+(unsignedchar)uSend[i];

uTmp=uSum0x000f;

uSend[10]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uSum4)0x000f;

uSend[9]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

for(i=0;i11;i++)

{for(lTmp=0L;lTmpDELAY_TIMES;lTmp++)

{if((inportb(STATS_PORT)0x20)!

=0)

break;

}

if(lTmp=DELAY_TIMES)

{

return(FALSE);

}

outportb(DATA_PORT,uSend[i]);

}

disable();

for(lTmp=0;lTmpDELAY_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;inumber*4+4;i++)

{for(lTmp=0L;lTmpDELAY_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;inumber*4+2;i++)

uSum=uSum+(unsignedint)uReceive[i];

uTmp=uSum0xf;

uTmp=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

if((unsignedchar)uTmp!

=uReceive[number*4+3])return(FALSE);

uTmp=(uSum4)0xf;

uTmp=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

if((unsignedchar)uTmp!

=uReceive[number*4+2])return(FALSE);

for(j=0;jnumber;j++)

{for(i=j*4+1;ij*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];

}

returnTRUE;

}

intread_mdata_register(unsignedintuAddress,unsignedintnumber)

{inti;

for(i=0;i3;i++)

if(_read_mdata_register(uAddress,number)==TRUE)

returnTRUE;

returnFALSE;

}

int_read_mdata_register(unsignedintuAddress,unsignedintnumber)

{unsignedcharuSend[]={2,0x30,0x30,0x30,0x30,0x30,0x30,0x32,3,0x30,0x30};

unsignedcharuReceive[104];

unsignedintuTmp;

unsignedintuSum;

unsignedintnum;

longlTmp;

inti,j;

if(TESTING==1)

{for(i=0;inumber;i++)uRead_value[i]=0;

returnTRUE;

}

init_plc();

num=number*2;

if((num/16)=10)

uSend[6]=(unsignedchar)(num/16+0x41-10);

else

uSend[6]=(unsignedchar)(num/16+0x30);

if((num%16)=10)

uSend[7]=(unsignedchar)((num%16)+0x41-10);

else

uSend[7]=(unsignedchar)((num%16)+0x30);

/*uAddress=uAddress*2+0x1000;*/

uTmp=uAddress0x000f;

uSend[5]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress4)0x000f;

uSend[4]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress8)0x000f;

uSend[3]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress12)0x000f;

uSend[2]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uSum=0;

for(i=1;i9;i++)

uSum=uSum+(unsignedchar)uSend[i];

uTmp=uSum0x000f;

uSend[10]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uSum4)0x000f;

uSend[9]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

for(i=0;i11;i++)

{for(lTmp=0L;lTmpDELAY_TIMES;lTmp++)

{if((inportb(STATS_PORT)0x20)!

=0)

break;

}

if(lTmp=DELAY_TIMES)

{

return(FALSE);

}

outportb(DATA_PORT,uSend[i]);

}

disable();

for(lTmp=0;lTmpDELAY_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;inumber*4+4;i++)

{for(lTmp=0L;lTmpDELAY_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;inumber*4+2;i++)

uSum=uSum+(unsignedint)uReceive[i];

uTmp=uSum0xf;

uTmp=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

if((unsignedchar)uTmp!

=uReceive[number*4+3])return(FALSE);

uTmp=(uSum4)0xf;

uTmp=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

if((unsignedchar)uTmp!

=uReceive[number*4+2])return(FALSE);

for(j=0;jnumber;j++)

{for(i=j*4+1;ij*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];

}

returnTRUE;

}

intwrite_data_register(unsignedintuAddress,unsignedintnumber)

{inti;

for(i=0;i3;i++)

if(_write_data_register(uAddress,number)==TRUE)

returnTRUE;

returnFALSE;

}

int_write_data_register(unsignedintuAddress,unsignedintnumber)

{unsignedcharuSend[111];

unsignedintuTmp,uSum,num;

longlTmp;

inti;

if(TESTING==1)returnTRUE;

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;

elseuSend[6]=num+0x30;

num=(number*2)%16;

if(num=10)uSend[7]=num+0x41-10;

elseuSend[7]=num+0x30;

uAddress=0x1000+2*uAddress;

uTmp=uAddress0x000f;

uSend[5]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress4)0x000f;

uSend[4]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress8)0x000f;

uSend[3]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress12)0x000f;

uSend[2]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

for(i=0;inumber;i++)

{

uTmp=uWrite_value[i]0x000f;

uSend[i*4+9]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uWrite_value[i]4)0x000f;

uSend[i*4+8]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uWrite_value[i]8)0x000f;

uSend[i*4+11]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uWrite_value[i]12)0x000f;

uSend[i*4+10]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

}

uSum=0;

for(i=1;i9+number*4;i++)

uSum+=uSend[i];

uTmp=uSum0x000f;

uSend[number*4+10]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uSum4)0x000f;

uSend[number*4+9]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

for(i=0;i11+number*4;i++)

{for(lTmp=0L;lTmpDELAY_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;lTmpDELAY_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);

}

}

intforce_m_contact(unsigneduAddress,unsignedcharucOn_off)

{inti;

for(i=0;i3;i++)

if(_force_m_contact(uAddress,ucOn_off)==TRUE)

returnTRUE;

returnFALSE;

}

int_force_m_contact(unsigneduAddress,unsignedcharucOn_off)

{unsigneduSend[]={2,0x37,0x30,0x30,0x30,0x30,3,0x30,0x30};

unsigneduTmp,uSum,i;

longlTmp;

if(TESTING==1)returnTRUE;

init_plc();

uAddress=uAddress+0x800;

uSend[1]=ucOn_off;

uTmp=uAddress0x000f;

uSend[3]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress4)0x000f;

uSend[2]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress8)0x000f;

uSend[5]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uAddress12)0x000f;

uSend[4]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uSum=0;

for(i=1;i7;i++)

uSum+=uSend[i];

uTmp=uSum0x000f;

uSend[8]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

uTmp=(uSum4)0x000f;

uSend[7]=(uTmp10)?

(uTmp+0x30):

(uTmp+0x41-0xa);

for(i=0;i9;i++)

{for(lTmp=0L;lTmpDELAY_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;lTmpDELAY_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);

}

}

intread_m_register(unsignedintuAddress,unsignedintnumber)

{i

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2