COMLIB 通讯程序示例.docx
《COMLIB 通讯程序示例.docx》由会员分享,可在线阅读,更多相关《COMLIB 通讯程序示例.docx(28页珍藏版)》请在冰豆网上搜索。
![COMLIB 通讯程序示例.docx](https://file1.bdocx.com/fileroot1/2023-5/26/acaa66e1-b3a2-4cf1-bb88-33c11b8001a5/acaa66e1-b3a2-4cf1-bb88-33c11b8001a51.gif)
COMLIB通讯程序示例
1.COMLIB通讯程序示例
1.1PC(COM1和COM2)按字节发送-接收
全局变量
VAR_GLOBAL
(*TestCOM1atPCInterfaceCOM1*)
COM1InDataAT%IB100:
PcComInData;
COM1OutDataAT%QB100:
PcComOutData;
COM1RxBuffer:
ComBuffer;
COM1TxBuffer:
ComBuffer;
(*TestCOM2atPCInterfaceCOM2*)
COM2InDataAT%IB200:
PcComInData;
COM2OutDataAT%QB200:
PcComOutData;
COM2RxBuffer:
ComBuffer;
COM2TxBuffer:
ComBuffer;
END_VAR
后台程序:
ComBackground
PROGRAMComBackground
VAR
COM1Control:
PcComControl;
COM2Control:
PcComControl;
END_VAR
COM1Control(COMin:
=COM1InData,
COMout:
=COM1OutData,
TxBuffer:
=COM1TxBuffer,
RxBuffer:
=COM1RxBuffer);
COM2Control(COMin:
=COM2InData,
COMout:
=COM2OutData,
TxBuffer:
=COM2TxBuffer,
RxBuffer:
=COM2RxBuffer);
测试程序:
TESTCOM1
PROGRAMTESTCOM1
VAR
SendData:
ARRAY[0..9]OFBYTE:
=1,2,3,4,5,6,7,8,9,10;
RecData:
ARRAY[0..9]OFBYTE;
COM1Send:
SendByte;
COM2Rec:
ReceiveByte;
Delay:
TON;
I:
BYTE;
Index:
BYTE:
=0;
END_VAR
(*按字节方式发送数据到COM1*)
Delay(IN:
=TRUE,PT:
=T#100MS);
IFDelay.QTHEN
Delay(IN:
=FALSE);
FORi:
=0TO9DO
COM1Send(SendByte:
=SendData[i],TxBuffer:
=COM1TxBuffer);
END_FOR
FORi:
=0TO9DO
SendData[i]:
=SendData[i]+1;
END_FOR
END_IF
(*按字节方式从COM2接收数据*)
COM2Rec(RxBuffer:
=COM2RXbuffer);
IFCOM2Rec.ByteReceivedTHEN
RecData[Index]:
=COM2Rec.ReceivedByte;
Index:
=Index+1;
IFIndex>=SIZEOF(RecData)THEN
Index:
=0;
END_IF
END_IF
主程序:
MAIN
PROGRAMMAIN
VAR
END_VAR
TESTCOM1;
1.2PC(COM1和COM2)按字符串发送-接收
全局变量:
同上
后台程序:
ComBackground同上
测试程序:
TESTCOM1
PROGRAMTESTCOM1
VAR
SendString:
STRING:
='$$Hello,welcometoBECKHOFF!
$$';
RecString:
STRING;
(注:
可变换为无Prefix和Suffix示例,应加:
TIMEOUT:
=T#80MS)
Prefix:
STRING:
='$$';
Suffix:
STRING:
='$$';
TimeOut:
TIME:
=T#0MS;
Reset:
BOOL;
COM1SString:
SendString;
COM2RString:
ReceiveString;
Delay:
TON;
END_VAR
(*按字符串方式发送数据到COM1*)
Delay(IN:
=TRUE,PT:
=T#100MS);
IFDelay.QTHEN
Delay(IN:
=FALSE);
COM1SString(SendString:
=SendString,TxBuffer:
=COM1TxBuffer);
END_IF
(*按字符串方式从COM2接收数据*)
COM2RString(
Prefix:
=Prefix,
Suffix:
=Suffix,
Timeout:
=TimeOut,
Reset:
=Reset,
ReceivedString:
=RecString,
RxBuffer:
=COM2RxBuffer);
主程序:
MAIN
PROGRAMMAIN
VAR
END_VAR
TESTCOM1;
1.3PC(COM1和COM2)按数据发送-接收
全局变量:
同上
后台程序:
ComBackground同上
测试程序:
TESTCOM1
PROGRAMTESTCOM1
VAR
Prefix:
ARRAY[0..1]OFBYTE:
=16#0D,16#0A;
Suffix:
ARRAY[0..1]OFBYTE:
=16#0D,16#0A;
SendData:
ARRAY[0..9]OFBYTE:
=
16#0D,16#0A,1,2,3,4,5,6,16#0D,16#0A;
RecData:
ARRAY[0..9]OFBYTE;
COM1SData:
SendData;
COM2RData:
ReceiveData;
Delay:
TON;
Reset:
BOOL;
i:
BYTE;
END_VAR
(*按任意类型数据方式发送数据到COM1*)
Delay(IN:
=TRUE,PT:
=T#100MS);
IFDelay.QTHEN
Delay(IN:
=FALSE);
COM1SData(
pSendData:
=ADR(SendData),
Length:
=SIZEOF(SendData),
TxBuffer:
=COM1TxBuffer);
FORi:
=2TO7DO
SendData[i]:
=SendData[i]+1;
END_FOR
END_IF
(*按任意类型数据方式从COM2接收数据*)
COM2RData(
pPrefix:
=ADR(Prefix),
LenPrefix:
=SIZEOF(Prefix),
pSuffix:
=ADR(Suffix),
LenSuffix:
=SIZEOF(Suffix),
pReceiveData:
=ADR(RecData),
SizeReceiveData:
=SIZEOF(RecData),
Timeout:
=T#50MS,
Reset:
=Reset,
RxBuffer:
=COM2RxBuffer);
主程序:
MAIN
PROGRAMMAIN
VAR
END_VAR
TESTCOM1;
1.4ASC功能说明
主程序:
MAIN
PROGRAMMAIN
VAR
ascout:
BYTE;
str:
STRING:
='a';
END_VAR
ascout:
=ASC(str);
1.5CHR功能说明
主程序:
MAIN
PROGRAMMAIN
VAR
chrout:
STRING;
cinp:
BYTE:
=48;
END_VAR
chrout:
=CHR(cinp);
1.6KL6001串行数据通讯
全局变量:
VAR_GLOBAL
Kl6InDataAT%IB0:
KL6inData5B;
Kl6OutDataAT%QB0:
KL6outData5B;
RxBuffer:
ComBuffer;
TxBuffer:
ComBuffer;
END_VAR
主程序:
PROGRAMMAIN
VAR
ComControl:
KL6Control5B;
Timer:
TON;
Receive:
ReceiveByte;
Send:
SendByte;
Rb:
BYTE;
Sb:
BYTE;
END_VAR
ComControl(COMin:
=KL6InData,
COMout:
=KL6OutData,
TxBuffer:
=TxBuffer,
RxBuffer:
=RxBuffer);
Timer(IN:
=TRUE,PT:
=T#1S);
IFTimer.QTHEN
(*每秒发送一个字符*)
IFSb=0THEN
Sb:
=64;
END_IF
Sb:
=Sb+1;
IFSb>90THEN
Sb:
=64;
END_IF
Send(SendByte:
=Sb,TxBuffer:
=TxBuffer);
Timer(IN:
=FALSE);
END_IF
Receive(RxBuffer:
=Rxbuffer);
IFReceive.ByteReceivedTHEN
(*获取接收的字符*)
Rb:
=Receive.ReceivedByte;
(*其它程序处理...*)
END_IF
1.6KL6021–KL6021RS-485之间的串行数据通讯
1.6.1发送方:
全局变量:
VAR_GLOBAL
KL6021_1InDataAT%IB0:
KL6inData5B;
KL6021_1OutDataAT%QB0:
KL6outData5B;
KL6021_1RxBuffer:
ComBuffer;
KL6021_1TxBuffer:
ComBuffer;
END_VAR
主程序:
PROGRAMMAIN
VAR
KL6021_1ComControl:
KL6Control5B;
KL6021_1Send:
SendByte;
SendData:
ARRAY[0..9]OFBYTE:
=10(16#68);
Index1:
BYTE:
=0;
T01:
TON;
I:
BYTE;
KL6021_1Receive:
ReceiveByte;
ReceiveData:
ARRAY[0..19]OFBYTE:
=20(16#0);
Index:
BYTE:
=0;
END_VAR
KL6021_1ComControl(COMin:
=KL6021_1InData,
COMout:
=KL6021_1OutData,
RxBuffer:
=KL6021_1RxBuffer,
TxBuffer:
=KL6021_1TxBuffer);
(*===============KL6021_1SendData===============*)
IFNOTKL6021_1Send.BusyTHEN
KL6021_1Send(SendByte:
=SendData[Index1],
TxBuffer:
=KL6021_1TxBuffer);
Index1:
=Index1+1;
IFIndex1>=SIZEOF(SendData)THEN
Index1:
=0;
END_IF
END_IF
T01(IN:
=NOTT01.Q,PT:
=T#800MS);
SendData[0]:
=16#FF;
SendData[1]:
=16#FF;
SendData[9]:
=16#0D;
IFT01.QTHEN
FORi:
=2TO8DO
SendData[i]:
=SendData[i]+i;
END_FOR
END_IF
KL6021_1Receive(RxBuffer:
=KL6021_1RXbuffer);
IFKL6021_1Receive.ByteReceivedTHEN
ReceiveData[Index]:
=KL6021_1Receive.ReceivedByte;
Index:
=Index+1;
IFIndex>=SIZEOF(ReceiveData)THEN
Index:
=0;
END_IF
END_IF
%QB6:
=SendData[2];
1.6.2接收方:
全局变量:
VAR_GLOBAL
KL6021_2InDataAT%IB0:
KL6inData5B;
KL6021_2OutDataAT%QB0:
KL6outData5B;
KL6021_2RxBuffer:
ComBuffer;
KL6021_2TxBuffer:
ComBuffer;
END_VAR
主程序:
PROGRAMMAIN
VAR
KL6021_2ComControl:
KL6Control5B;
KL6021_2Receive:
ReceiveByte;
ReceiveData:
ARRAY[0..19]OFBYTE:
=20(16#0);
Index:
BYTE:
=0;
END_VAR
KL6021_2ComControl(COMin:
=KL6021_2InData,
COMout:
=KL6021_2OutData,
RxBuffer:
=KL6021_2RxBuffer,
TxBuffer:
=KL6021_2TxBuffer);
(*===============KL6021_2receiveData===============*)
KL6021_2Receive(RxBuffer:
=KL6021_2RXbuffer);
IFKL6021_2Receive.ByteReceivedTHEN
ReceiveData[Index]:
=KL6021_2Receive.ReceivedByte;
Index:
=Index+1;
IFIndex>=SIZEOF(ReceiveData)THEN
Index:
=0;
END_IF
END_IF
2.ModbusRTU通讯
2.1PC作ModbusRTU从站
全局变量:
VAR_CONFIG
MAIN.MB.Port.IO.outAT%QB0:
ModbusPcComOutData;
MAIN.MB.Port.IO.inAT%IB0:
ModbusPcComInData;
END_VAR
主程序:
PROGRAMMAIN
VAR
MB1:
ModbusRtuSlave_PcCOM;
InputAT%IW100:
ARRAY[0..9]OFWORD;
OutputAT%QW100:
ARRAY[0..9]OFWORD;
MemoryAT%MW100:
ARRAY[0..9]OFWORD;
ErrorID:
Modbus_Errors;
END_VAR
TwinCAT系统管理器链接:
略
2.2PC作ModbusRTU主站
示例程序参见:
ModbusRTU软件说明
2.3BC作ModbusRTU主站
示例程序参见:
ModbusRTU软件说明
2.4BC作ModbusRTU从站
示例程序参见:
ModbusRTU软件说明
2.5PC作ModbusRTU主站,BC7300作从站示例程序
2.5.1PC机程序
全局变量:
VAR_CONFIG
MAIN.MB.Port.IO.outAT%QB0:
ModbusPcComOutData;
MAIN.MB.Port.IO.inAT%IB0:
ModbusPcComInData;
END_VAR
主程序:
PROGRAMMAIN
VAR
MB1:
ModbusRtuMaster_PcCOM;
ReadVal:
ARRAY[0..4]OFWORD;
UnitID:
BYTE:
=11;
Quantity:
WORD:
=2;
MBAddr:
WORD:
=0;
cbLength:
UINT:
=4;
bExecute:
BOOL;
bBusy:
BOOL;
Error:
BOOL;
ErrorID:
MODBUS_ERRORS;
cbRead:
UINT;
END_VAR
TwinCAT系统管理器链接:
略
2.5.2BC7300测试程序
Blink程序:
PROGRAMBlink
VAR
Timer1:
TON;
Timer2:
TON;
fbCounter1:
CTU;
fbCounter2:
CTD;
END_VAR
主程序:
PROGRAMMAIN
VAR
bInputAT%IX0.0:
BOOL;
END_VAR
IFNOTbInputTHEN
Blink;
%QX0.1:
=TRUE;
ELSE
%QX0.1:
=FALSE;
END_IF
2.6PC与BC8000/BK8000通讯程序:
略
2.7ADS通讯程序:
略
3.PID控制示例:
3.1惯性环节测试:
PROGRAMMAIN
VAR
fbPT1:
FB_P_T1;
bInit:
BOOL:
=TRUE;
fCycleTime:
LREAL:
=0.01;
Kp:
LREAL:
=1.0;
T1:
LREAL:
=5.0;
fVal:
LREAL:
=80.0;
fOut:
LREAL;
END_VAR
3.2BCxxxxPID测试:
3.2.1过程对象仿真:
FUNCTION_BLOCKSIMU
VAR_INPUT
Inp:
REAL;
END_VAR
VAR_IN_OUT
Index:
BYTE;
Buffer:
ARRAY[0..9]OFREAL;
END_VAR
VAR_OUTPUT
Out:
REAL;
END_VAR
VAR
i:
BYTE;
Sum:
REAL;
END_VAR
IF(Index>9)OR(Index<0)THEN
Index:
=0;
END_IF
Buffer[Index]:
=Inp;
Index:
=Index+1;
Sum:
=0;
FORi:
=0TO9DO
Sum:
=Sum+Buffer[i];
END_FOR
Out:
=Sum/10.0;
3.2.2测试程序:
TEST
PROGRAMTEST
VAR
SIMU1:
SIMU;
Vinp:
REAL;
Index:
BYTE:
=0;
Buffer:
ARRAY[0..9]OFREAL;
Vout:
REAL;
PID1:
CtrlStandardPID;
bInit:
BOOL:
=TRUE;
Vset:
REAL:
=50.0;
fKp:
REAL:
=0.8;
fTn:
REAL:
=0.28;
nErrorStatus:
DINT;
bMaxAT%QX0.0:
BOOL;
bMinAT%QX0.1:
BOOL;
bARW:
BOOL;
convAT%QB1:
BYTE;
END_VAR
3.2.3主程序:
MAIN
PROGRAMMAIN
VAR
END_VAR
TEST;
3.3温度控制示例
3.3.1过程对象仿真:
FUNCTION_BLOCKFB_Process
VAR_INPUT
y:
LREAL;
Ta:
LREAL;
xOffset:
LREAL;
bInit:
BOOL;
bNoise:
BOOL;
END_VAR
VAR_OUTPUT
x:
LREAL;
END_VAR
VAR
KP1:
LREAL:
=1.0;
KP2:
LREAL:
=1.0;
T11:
LREAL:
=1.0;
T12:
LREAL:
=10;
iDeadTime:
INT:
=0;
StreckeTeil1:
FB_P_T1;
StreckeTeil2:
FB_P_T1;
StreckeDeadtime:
FB_DeadTime;
StreckeNoise:
DRAND;
END_VAR
StreckeTeil1(
bInit:
=bInit,
fCycleTime:
=Ta,
Kp