使用SCL编程CP341的Modbus通讯_第1页
使用SCL编程CP341的Modbus通讯_第2页
使用SCL编程CP341的Modbus通讯_第3页
使用SCL编程CP341的Modbus通讯_第4页
使用SCL编程CP341的Modbus通讯_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

因为原厂的MODBUS非常贵,比一个CPU315-2PNDP还要贵,为了帮公司省钱,决定用SCL软体来做MODBUS,测试成功了,有兴趣的可以看看,互相学习。

OB1测试程序如下

下方是SCL源代码

FUNCTIONFC2000:WORD

TITLE='MODBUS_READ_WORD'

VAR_INPUT

nNode_Add

:

INT;

nBuffer_Add

:

INT;

nLADDR

:

INT;

fSending_Trig

:

BOOL;

END_VAR

VAR_OUTPUT

fBUSY

:

BOOL;

fREADY

:

BOOL;

nDATA

:

INT;

END_VAR

VAR_TEMP

crc_table

:

ARRAY[0..512]

OFBYTE;

send_frame

:

ARRAY[0..50]

OFBYTE;

receive_frame

:

ARRAY[0..50]

OFBYTE;

crc_Low_byte,crc_High_byte,index_byte

:BYTE;

crc_Low,crc_High,index,i,Length,crc

:INT;

buffer_address_High,buffer_address_Low

:INT;

node_address,buffer_address

:

INT;

f_snd_done,f_snd_error:BOOL;

w_snd_status

:

WORD;

b_rcv_L_TYP:

CHAR;

b_rcv_L_NO,b_rcv_L_OFFSET,b_rcv_L_CF_BYT,b_rcv_L_CF_BIT,b_rcv_LEN

:

INT;

b_rcv_NDR,b_rcv_ERROR

:

BOOL;

b_rcv_STATUS

:

WORD;

END_VAR

BEGIN

fBUSY:=true;

fREADY:=false;

nDATA:=-1;

(*********InitialCRCTable********)

crc_table[0]:=16#0;

crc_table[1]:=16#C1;

crc_table[2]:=16#81;

crc_table[3]:=16#40;

crc_table[4]:=16#1;

crc_table[5]:=16#C0;

crc_table[6]:=16#80;

crc_table[7]:=16#41;

crc_table[8]:=16#1;

crc_table[9]:=16#C0;

crc_table[10]:=16#80;

crc_table[11]:=16#41;

crc_table[12]:=16#0;

crc_table[13]:=16#C1;

crc_table[14]:=16#81;

crc_table[15]:=16#40;

crc_table[16]:=16#1;

crc_table[17]:=16#C0;

crc_table[18]:=16#80;

crc_table[19]:=16#41;

crc_table[20]:=16#0;

crc_table[21]:=16#C1;

crc_table[22]:=16#81;

crc_table[23]:=16#40;

crc_table[24]:=16#0;

crc_table[25]:=16#C1;

crc_table[26]:=16#81;

crc_table[27]:=16#40;

crc_table[28]:=16#1;

crc_table[29]:=16#C0;

crc_table[30]:=16#80;

crc_table[31]:=16#41;

crc_table[32]:=16#1;

crc_table[33]:=16#C0;

crc_table[34]:=16#80;

crc_table[35]:=16#41;

crc_table[36]:=16#0;

crc_table[37]:=16#C1;

crc_table[38]:=16#81;

crc_table[39]:=16#40;

crc_table[40]:=16#0;

crc_table[41]:=16#C1;

crc_table[42]:=16#81;

crc_table[43]:=16#40;

crc_table[44]:=16#1;

crc_table[45]:=16#C0;

crc_table[46]:=16#80;

crc_table[47]:=16#41;

crc_table[48]:=16#0;

crc_table[49]:=16#C1;

crc_table[50]:=16#81;

crc_table[51]:=16#40;

crc_table[52]:=16#1;

crc_table[53]:=16#C0;

crc_table[54]:=16#80;

crc_table[55]:=16#41;

crc_table[56]:=16#1;

crc_table[57]:=16#C0;

crc_table[58]:=16#80;

crc_table[59]:=16#41;

crc_table[60]:=16#0;

crc_table[61]:=16#C1;

crc_table[62]:=16#81;

crc_table[63]:=16#40;

crc_table[64]:=16#1;

crc_table[65]:=16#C0;

crc_table[66]:=16#80;

crc_table[67]:=16#41;

crc_table[68]:=16#0;

crc_table[69]:=16#C1;

crc_table[70]:=16#81;

crc_table[71]:=16#40;

crc_table[72]:=16#0;

crc_table[73]:=16#C1;

crc_table[74]:=16#81;

crc_table[75]:=16#40;

crc_table[76]:=16#1;

crc_table[77]:=16#C0;

crc_table[78]:=16#80;

crc_table[79]:=16#41;

crc_table[80]:=16#0;

crc_table[81]:=16#C1;

crc_table[82]:=16#81;

crc_table[83]:=16#40;

crc_table[84]:=16#1;

crc_table[85]:=16#C0;

crc_table[86]:=16#80;

crc_table[87]:=16#41;

crc_table[88]:=16#1;

crc_table[89]:=16#C0;

crc_table[90]:=16#80;

crc_table[91]:=16#41;

crc_table[92]:=16#0;

crc_table[93]:=16#C1;

crc_table[94]:=16#81;

crc_table[95]:=16#40;

crc_table[96]:=16#0;

crc_table[97]:=16#C1;

crc_table[98]:=16#81;

crc_table[99]:=16#40;

crc_table[100]:=16#1;

crc_table[101]:=16#C0;

crc_table[102]:=16#80;

crc_table[103]:=16#41;

crc_table[104]:=16#1;

crc_table[105]:=16#C0;

crc_table[106]:=16#80;

crc_table[107]:=16#41;

crc_table[108]:=16#0;

crc_table[109]:=16#C1;

crc_table[110]:=16#81;

crc_table[111]:=16#40;

crc_table[112]:=16#1;

crc_table[113]:=16#C0;

crc_table[114]:=16#80;

crc_table[115]:=16#41;

crc_table[116]:=16#0;

crc_table[117]:=16#C1;

crc_table[118]:=16#81;

crc_table[119]:=16#40;

crc_table[120]:=16#0;

crc_table[121]:=16#C1;

crc_table[122]:=16#81;

crc_table[123]:=16#40;

crc_table[124]:=16#1;

crc_table[125]:=16#C0;

crc_table[126]:=16#80;

crc_table[127]:=16#41;

crc_table[128]:=16#1;

crc_table[129]:=16#C0;

crc_table[130]:=16#80;

crc_table[131]:=16#41;

crc_table[132]:=16#0;

crc_table[133]:=16#C1;

crc_table[134]:=16#81;

crc_table[135]:=16#40;

crc_table[136]:=16#0;

crc_table[137]:=16#C1;

crc_table[138]:=16#81;

crc_table[139]:=16#40;

crc_table[140]:=16#1;

crc_table[141]:=16#C0;

crc_table[142]:=16#80;

crc_table[143]:=16#41;

crc_table[144]:=16#0;

crc_table[145]:=16#C1;

crc_table[146]:=16#81;

crc_table[147]:=16#40;

crc_table[148]:=16#1;

crc_table[149]:=16#C0;

crc_table[150]:=16#80;

crc_table[151]:=16#41;

crc_table[152]:=16#1;

crc_table[153]:=16#C0;

crc_table[154]:=16#80;

crc_table[155]:=16#41;

crc_table[156]:=16#0;

crc_table[157]:=16#C1;

crc_table[158]:=16#81;

crc_table[159]:=16#40;

crc_table[160]:=16#0;

crc_table[161]:=16#C1;

crc_table[162]:=16#81;

crc_table[163]:=16#40;

crc_table[164]:=16#1;

crc_table[165]:=16#C0;

crc_table[166]:=16#80;

crc_table[167]:=16#41;

crc_table[168]:=16#1;

crc_table[169]:=16#C0;

crc_table[170]:=16#80;

crc_table[171]:=16#41;

crc_table[172]:=16#0;

crc_table[173]:=16#C1;

crc_table[174]:=16#81;

crc_table[175]:=16#40;

crc_table[176]:=16#1;

crc_table[177]:=16#C0;

crc_table[178]:=16#80;

crc_table[179]:=16#41;

crc_table[180]:=16#0;

crc_table[181]:=16#C1;

crc_table[182]:=16#81;

crc_table[183]:=16#40;

crc_table[184]:=16#0;

crc_table[185]:=16#C1;

crc_table[186]:=16#81;

crc_table[187]:=16#40;

crc_table[188]:=16#1;

crc_table[189]:=16#C0;

crc_table[190]:=16#80;

crc_table[191]:=16#41;

crc_table[192]:=16#0;

crc_table[193]:=16#C1;

crc_table[194]:=16#81;

crc_table[195]:=16#40;

crc_table[196]:=16#1;

crc_table[197]:=16#C0;

crc_table[198]:=16#80;

crc_table[199]:=16#41;

crc_table[200]:=16#1;

crc_table[201]:=16#C0;

crc_table[202]:=16#80;

crc_table[203]:=16#41;

crc_table[204]:=16#0;

crc_table[205]:=16#C1;

crc_table[206]:=16#81;

crc_table[207]:=16#40;

crc_table[208]:=16#1;

crc_table[209]:=16#C0;

crc_table[210]:=16#80;

crc_table[211]:=16#41;

crc_table[212]:=16#0;

crc_table[213]:=16#C1;

crc_table[214]:=16#81;

crc_table[215]:=16#40;

crc_table[216]:=16#0;

crc_table[217]:=16#C1;

crc_table[218]:=16#81;

crc_table[219]:=16#40;

crc_table[220]:=16#1;

crc_table[221]:=16#C0;

crc_table[222]:=16#80;

crc_table[223]:=16#41;

crc_table[224]:=16#1;

crc_table[225]:=16#C0;

crc_table[226]:=16#80;

crc_table[227]:=16#41;

crc_table[228]:=16#0;

crc_table[229]:=16#C1;

crc_table[230]:=16#81;

crc_table[231]:=16#40;

crc_table[232]:=16#0;

crc_table[233]:=16#C1;

crc_table[234]:=16#81;

crc_table[235]:=16#40;

crc_table[236]:=16#1;

crc_table[237]:=16#C0;

crc_table[238]:=16#80;

crc_table[239]:=16#41;

crc_table[240]:=16#0;

crc_table[241]:=16#C1;

crc_table[242]:=16#81;

crc_table[243]:=16#40;

crc_table[244]:=16#1;

crc_table[245]:=16#C0;

crc_table[246]:=16#80;

crc_table[247]:=16#41;

crc_table[248]:=16#1;

crc_table[249]:=16#C0;

crc_table[250]:=16#80;

crc_table[251]:=16#41;

crc_table[252]:=16#0;

crc_table[253]:=16#C1;

crc_table[254]:=16#81;

crc_table[255]:=16#40;

crc_table[256]:=16#0;

crc_table[257]:=16#C0;

crc_table[258]:=16#C1;

crc_table[259]:=16#1;

crc_table[260]:=16#C3;

crc_table[261]:=16#3;

crc_table[262]:=16#2;

crc_table[263]:=16#C2;

crc_table[264]:=16#C6;

crc_table[265]:=16#6;

crc_table[266]:=16#7;

crc_table[267]:=16#C7;

crc_table[268]:=16#5;

crc_table[269]:=16#C5;

crc_table[270]:=16#C4;

crc_table[271]:=16#4;

crc_table[272]:=16#CC;

crc_table[273]:=16#C;

crc_table[274]:=16#D;

crc_table[275]:=16#CD;

crc_table[276]:=16#F;

crc_table[277]:=16#CF;

crc_table[278]:=16#CE;

crc_table[279]:=16#E;

crc_table[280]:=16#A;

crc_table[281]:=16#CA;

crc_table[282]:=16#CB;

crc_table[283]:=16#B;

crc_table[284]:=16#C9;

crc_table[285]:=16#9;

crc_table[286]:=16#8;

crc_table[287]:=16#C8;

crc_table[288]:=16#D8;

crc_table[289]:=16#18;

crc_table[290]:=16#19;

crc_table[291]:=16#D9;

crc_table[292]:=16#1B;

crc_table[293]:=16#DB;

crc_table[294]:=16#DA;

crc_table[295]:=16#1A;

crc_table[296]:=16#1E;

crc_table[297]:=16#DE;

crc_table[298]:=16#DF;

crc_table[299]:=16#1F;

crc_table[300]:=16#DD;

crc_table[301]:=16#1D;

crc_table[302]:=16#1C;

crc_table[303]:=16#DC;

crc_table[304]:=16#14;

crc_table[305]:=16#D4;

crc_table[306]:=16#D5;

crc_table[307]:=16#15;

crc_table[308]:=16#D7;

crc_table[309]:=16#17;

crc_table[310]:=16#16;

crc_table[311]:=16#D6;

crc_table[312]:=16#D2;

crc_table[313]:=16#12;

crc_table[314]:=16#13;

crc_table[315]:=16#D3;

crc_table[316]:=16#11;

crc_table[317]:=16#D1;

crc_table[318]:=16#D0;

crc_table[319]:=16#10;

crc_table[320]:=16#F0;

crc_table[321]:=16#30;

crc_table[322]:=16#31;

crc_table[323]:=16#F1;

crc_table[324]:=16#33;

crc_table[325]:=16#F3;

crc_table[326]:=16#F2;

crc_table[327]:=16#32;

crc_table[328]:=16#36;

crc_table[329]:=16#F6;

crc_table[330]:=16#F7;

crc_table[331]:=16#37;

crc_table[332]:=16#F5;

crc_table[333]:=16#35;

crc_table[334]:=16#34;

crc_table[335]:=16#F4;

crc_table[336]:=16#3C;

crc_table[337]:=16#FC;

crc_table[338]:=16#FD;

crc_table[339]:=16#3D;

crc_table[340]:=16#FF;

crc_table[341]:=16#3F;

crc_table[342]:=16#3E;

crc_table[343]:=16#FE;

crc_table[344]:=16#FA;

crc_table[345]:=16#3A;

crc_table[346]:=16#3B;

crc_table[347]:=16#FB;

crc_table[348]:=16#39;

crc_table[349]:=16#F9;

crc_table[350]:=16#F8;

crc_table[351]:=16#38;

crc_table[352]:=16#28;

crc_table[353]:=16#E8;

crc_table[354]:=16#E9;

crc_table[355]:=16#29;

crc_table[356]:=16#EB;

crc_table[357]:=16#2B;

crc_table[358]:=16#2A;

crc_table[359]:=16#EA;

crc_table[360]:=16#EE;

crc_table[361]:=16#2E;

crc_table[362]:=16#2F;

crc_table[363]:=16#EF;

crc_table[364]:=16#2D;

crc_table[365]:=16#ED;

crc_table[366]:=16#EC;

crc_table[367]:=16#2C;

crc_table[368]:=16#E4;

crc_table[369]:=16#24;

crc_table[370]:=16#25;

crc_table[371]:=16#E5;

crc_table[372]:=16#27;

crc_table[373]:=16#E7;

crc_table[374]:=16#E6;

crc_table[375]:=16#26;

crc_table[376]:=16#22;

crc_table[377]:=16#E2;

crc_table[378]:=16#E3;

crc_table[379]:=16#23;

crc_table[380]:=16#E1;

crc_table[381]:=16#21;

crc_table[382]:=16#20;

crc_table[383]:=16#E0;

crc_table[384]:=16#A0;

crc_table[385]:=16#60;

crc_table[386]:=16#61;

crc_table[387]:=16#A1;

crc_table[388]:=16#63;

crc_table[389]:=16#A3;

crc_table[390]:=16#A2;

crc_table[391]:=16#62;

crc_table[392]:=16#66;

crc_table[393]:=16#A6;

crc_table[394]:=16#A7;

crc_table[395]:=16#67;

crc_table[396]:=16#A5;

crc_table[397]:=16#65;

crc_table[398]:=16#64;

crc_table[399]:=16#A4;

crc_table[400]:=16#6C;

crc_table[401]:=16#AC;

crc_table[402]:=16#AD;

crc_table[403]:=16#6D;

crc_table[404]:=16#AF;

crc_table[405]:=16#6F;

crc_table[406]:=16#6E;

crc_table[407]:=16#AE;

crc_table[408]:=16#AA;

crc_table[409]:=16#6A;

crc_table[410]:=16#6B;

crc_table[411]:=16#AB;

crc_table[412]:=16#69;

crc_table[413]:=16#A9;

crc_table[414]:=16#A8;

crc_table[415]:=16#68;

crc_table[416]:=16#78;

crc_table[417]:=16#B8;

crc_table[418]:=16#B9;

crc_table[419]:=16#79;

crc_table[420]:=16#BB;

crc_table[421]:=16#7B;

crc_table[422]:=16#7A;

crc_table[423]:=16#BA;

crc_table[424]:=16#BE;

crc_table[425]:=16#7E;

crc_table[426]:=16#7F;

crc_table[427]:=16#BF;

crc_table[428]:=16#7D;

crc_table[429]:=16#BD;

crc_table[430]:=16#BC;

crc_table[431]:=16#7C;

crc_table[432]:=16#B4;

crc_table[433]:=16#74;

crc_table[434]:=16#75;

crc_table[435]:=16#B5;

crc_table[436]:=16#77;

crc_table[437]:=16#B7;

crc_table[438]:=16#B6;

crc_table[439]:=16#76;

crc_table[440]:=16#72;

crc_table[441]:=16#B2;

crc_table[442]:=16#B3;

crc_table[443]:=16#73;

crc_table[444]:=16#B1;

crc_table[445]:=16#71;

crc_table[446]:=16#70;

crc_table[447]:=16#B0;

crc_table[448]:=16#50;

crc_table[449]:=16#90;

crc_table[450]:=16#91;

crc_table[451]:=16#51;

crc_table[452]:=16#93;

crc_table[453]:=16#53;

crc_table[454]:=16#52;

crc_table[455]:=16#92;

crc_table[456]:=16#96;

crc_table[457]:=16#56;

crc_table[458]:=16#57;

crc_table[459]:=16#97;

crc_table[460]:=16#55;

crc_table[461]:=16#95;

crc_table[462]:=16#94;

crc_table[463]:=16#54;

crc_table[464]:=16#9C;

crc_table[465]:=16#5C;

crc_table[466]:=16#5D;

crc_table[467]:=16#9D;

crc_table[468]:=16#5F;

crc_table[469]:=16#9F;

crc_table[470]:=16#9E;

crc_table[471]:=16#5E;

crc_table[472]:=16#5A;

crc_table[473]:=16#9A;

crc_table[474]:=16#9B;

crc_table[475]:=16#5B;

crc_table[476]:=16#99;

crc_table[477]:=16#59;

crc_table[478]:=16#58;

crc_table[479]:=16#98;

crc_table[480]:=16#88;

crc_table[481]:=16#48;

crc_table[482]:=16#49;

crc_table[483]:=16#89;

crc_table[484]:=16#4B;

crc_table[485]:=16#8B;

crc_table[486]:=16#8A;

crc_table[487]:=16#4A;

crc_table[488]:=16#4E;

crc_table[489]:=16#8E;

crc_table[490]:=16#8F;

crc_table[491]:=16#4F;

crc_table[492]:=16#8D;

crc_table[493]:=16#4D;

crc_table[494]:=16#4C;

crc_table[495]:=16#8C;

crc_table[496]:=16#44;

crc_table[497]:=16#84;

crc_table[498]:=16#85;

crc_table[499]:=16#45;

crc_table[500]:=16#87;

crc_table[501]:=16#47;

crc_table[502]:=16#46;

crc_table[503]:=16#86;

crc_table[504]:=16#82;

crc_table[505]:=16#42;

crc_table[506]:=16#43;

crc_table[507]:=16#83;

crc_table[508]:=16#41;

crc_table[509]:=16#81;

crc_table[510]:=16#80;

crc_table[511]:=16#40;

(***byte-0:nodeAddress***)

node_address:=nNode_Add;

IFnode_address<1THEN

node_address:=1;

END_IF;

IFnode_address>127THEN

node_address:=127;

END_IF;

send_frame[0]:=INT_TO_BYTE(node_address);

(***byte-1:functioncode***)

send_frame[1]:=16#03;//ReadRegister

(***byte-2;3:StartingAddress***)

buffer_address:=nBuffer_Add;

IFbuffer_address<0THEN

buffer_address:=0;

END_IF;

IFbuffer_address>32767THEN

buffer_address:=32767;

END_IF;

buffer_address_High:=buffer_addressDIV256;

buffer_address_Low:=buffer_addressMOD256;

send_frame[2]:=INT_TO_BYTE(buffer_address_High);//StartingAddressHigh

send_frame[3]:=INT_TO_BYTE(buffer_address_Low);//StartingAddressLow

(***byte-4;5:NumberofPoints***)

send_frame[4]:=16#00;//NumberofPointsHigh

send_frame[5]:=16#01;//NumberofPointsLow

Length:=6;

crc_Low_byte:=16#FF;

crc_High_byte:=16#FF;

FORi:=0TO(Length-1)BY1DO

index:=BYTE_TO_INT(crc_High_byteXORsend_frame);

crc_High_byte:=crc_Low_byteXORcrc_table[index];

crc_Low_byte:=crc_table[index+256];

END_FOR;

crc:=BYTE_TO_INT(crc_High_byte)*256+BYTE_TO_INT(crc_Low_byte);

send_frame

:=crc_High_byte;

send_frame[i+1]:=crc_Low_byte;

Length:=Length+2;

FORi:=0TO(Length-1)BY1DO

DB2000.DB_BYTE

:=send_frame;

END_FOR;

(***********Sending************)

P_SND_RK.DB8(

SF:='S'//IN:CHAR

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论