📄 siemens_mpi.cpp
字号:
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include "W95_s7.h"
#include "Komfort.h"
#include "type.h"
static DWORD bitMasks[32] = {0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x00000100, 0x00000200, 0x00000400, 0x00000800,
0x00001000, 0x00002000, 0x00004000, 0x00008000,
0x00010000, 0x00020000, 0x00040000, 0x00080000,
0x00100000, 0x00200000, 0x00400000, 0x00800000,
0x01000000, 0x02000000, 0x04000000, 0x08000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000
};
//////////////////////////////////////////////////////////////////////////
//Function name :BitGet
//Input :int data,source data
// :int bitno,to show which bit to be get
//Output :true is 1,false is 0
//Remark :Get bit
//Global :bitMasks[32]
//Call function :
//Author :Jianfeng Zhang
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
bool BitGet(int data, int bitno) //Get the bit in a data
{
if ( bitno<0 ) bitno = 0;
else
if ( bitno>31 ) bitno = 31;
return (data&bitMasks[bitno])!=0;
}
//////////////////////////////////////////////////////////////////////////
//Function name :BitSet
//Input :int data,source data
// :int bitno,to show which bit to be set
// :bool on,true means set 1,false for 0
//Output :the changed data
//Remark :set the data bit
//Global :bitMasks
//Call function :
//Author :Jianfeng Zhang
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int BitSet(int data, int bitno, bool on) //Set the bit of a data
{
if ( bitno<0 ) bitno = 0;
else
if ( bitno>31 ) bitno = 31;
if ( on )
return data|bitMasks[bitno];
else
return data&(~bitMasks[bitno]);
}
//////////////////////////////////////////////////////////////////////////
//Function name :SwapShort
//Input :short& data,to be changed
//Output :
//Remark :swap a the high and low 8 bits of a short data
//Global :
//Call function :
//Author :Jianfeng Zhang
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
void WINAPI SwapShort(short& data)
{
data = (data&0x00ff)<<8 | (data&0xff00)>>8;
}
//////////////////////////////////////////////////////////////////////////
//Function name :SwapLong
//Input :long& data,to be changed
//Output :
//Remark :swap high and low bit of a long data
//Global :
//Call function :
//Author :Jianfeng Zhang
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
void WINAPI SwapLong(long& data)
{
data = (data&0x000000ff)<<8*3 | (data&0x0000ff00)<<8 |
(data&0x00ff0000)>>8 | (data&0xff000000)>>8*3;
}
//error message list
static char* errorData[6] =
{
" ",
"ERROR.DAT file does not exist or cannot be opened.",
"Error when reading the ERROR.DAT file.",
"Incorrect call of the ERROR.DAT error text file.",
"No error text exists for this error number.",
"Too many error texts in ERROR.DAT"
};
//////////////////////////////////////////////////////////////////////////
//Function name :ErrorMessage
//Input :int errorNo,index of error
// :char* buffer,store the error strings
//Output :
//Remark :Show the error message
//Global :
//Call function :error_message
//Author :Jianfeng Zhang
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
void WINAPI ErrorMessage(int errorNo,char* buffer)
{
int error = error_message(0,"ERROR.DAT");
if (error != 0)
{
sprintf(buffer,"%s",errorData[error]);
return;
}
error_message(errorNo,buffer);
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_R_I
//Input :int deviceNo
// :int bitNo,bit no
// :int* value,store the result
//Output :error code
//Remark :read the I's bit
//Global :
//Call function :e_field_read
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
//bit read and write
//deviceNo: the device number, I3.1,deviceNo is 3
//bitNo: offset of bit,I3.1,bitNo is 1
//value: store the result of I3.1,0 or 1
//return value: the error code
//对Discrete Inputs读取Data (bit)
int WINAPI PLC_R_I(int deviceNo,int bitNo,int* value)
{
int iValue;
int error;
error = e_field_read(deviceNo,1,&iValue);
if (0 == error)
{
if(BitGet(iValue,bitNo))
{
*value = 1;
}
else
{
*value = 0;
}
return 0;
}
return error;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_R_Q
//Input :int deviceNo
// :int bitNo,bit no
// :int* value,store the result
//Output :error code
//Remark :read the Q's bit
//Global :
//Call function :a_field_read
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_R_Q(int deviceNo,int bitNo,int* value)
{
int iValue;
int error;
error = a_field_read(deviceNo,1,&iValue);
if (0 == error)
{
if(BitGet(iValue,bitNo))
{
*value = 1;
}
else
{
*value = 0;
}
return 0;
}
return error;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_W_Q
//Input :int deviceNo
// :int bitNo,bit no
// :bool bValue,set the value,true or false
//Output :error code
//Remark :read the Q's bit
//Global :
//Call function :a_field_read
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_W_Q(int deviceNo,int bitNo,bool bValue)
{
int iValue;
int error;
error = a_field_read(deviceNo,1,&iValue);
if (error != 0)
{
return error;
}
iValue = BitSet(iValue,bitNo,bValue);
error = a_field_write(deviceNo,1,&iValue);
{
if (error != 0)
{
return error;
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_R_M
//Input :int deviceNo
// :int bitNo,bit no
// :int* value,store the result
//Output :error code
//Remark :read the M's bit
//Global :
//Call function :mb_bittest
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_R_M(int deviceNo,int bitNo,int* value)
{
int error;
char cValue;
error = mb_bittest(deviceNo,bitNo,&cValue);
if (cValue == 0)
{
*value = 0;
}
else
{
*value = 1;
}
return error;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_W_M
//Input :int deviceNo
// :int bitNo,bit no
// :bool bValue,set the value,true or false
//Output :error code
//Remark :read the Q's bit
//Global :
//Call function :mb_setbit,mb_resetbit
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_W_M(int deviceNo,int bitNo,bool bValue)
{
int error;
if (bValue)
{
error = mb_setbit(deviceNo,bitNo);
}
else
{
error = mb_resetbit(deviceNo,bitNo);
}
return error;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_R_DB
//Input :int blockNo,DB block
// :int deviceNo
// :int bitNo,bit no
// :int* value,store the result
//Output :error code
//Remark :read the DB's bit
//Global :
//Call function :d_field_read
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_R_DB(int blockNo,int deviceNo,int bitNo,int* value)
{
int error;
int iValue;
error = d_field_read(blockNo,deviceNo,1,&iValue);
if (error)
{
return error;
}
if (BitGet(iValue,bitNo))
{
*value = 1;
}
else
{
*value = 0;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_W_DB
//Input :int blockNo,DB block
// :int deviceNo
// :int bitNo,bit no
// :bool bValue,set the value,true or false
//Output :error code
//Remark :read the DB's bit
//Global :
//Call function :d_field_read
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_W_DB(int blockNo,int deviceNo,int bitNo,bool bValue)
{
int error;
int iValue;
error = d_field_read(blockNo,deviceNo,1,&iValue);
if (error)
{
return error;
}
iValue = BitSet(iValue,bitNo,bValue);
error = d_field_write(blockNo,deviceNo,1,&iValue);
return error;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_R_IW
//Input :int deviceNo
// :int amount:number of data to be read
// :void* buffer:store the result
//Output :error code
//Remark :read the I's Word
//Global :
//Call function :e_field_read
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_R_IW(int deviceNo,int amount,void* buffer)
{
int error;
error = e_field_read(deviceNo,amount*2,buffer);
return error;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_R_ID
//Input :int deviceNo
// :int amount:number of data to be read
// :void* buffer:store the result
//Output :error code
//Remark :read the I's DWord
//Global :
//Call function :e_field_read
//Author :
//Time :2009-04-25
//Modify person :
//Time :
//Version :V1.0.0
//////////////////////////////////////////////////////////////////////////
int WINAPI PLC_R_ID(int deviceNo,int amount,void* buffer)
{
int error;
error = e_field_read(deviceNo,amount*4,buffer);
return error;
}
//////////////////////////////////////////////////////////////////////////
//Function name :PLC_R_QW
//Input :int deviceNo
// :int amount:number of data to be read
// :void* buffer:store the result
//Output :error code
//Remark :read the Q's Word
//Global :
//Call function :a_field_read
//Author :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -