#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "2416addr.h"
#include "2416lib.h"
#include "..\inc\def.h"
#include "..\inc\option.h"
#include "..\inc\communication.h"
#include "..\inc\systemconf.h"
//#include <stdio.h>
extern char PlcType;
extern INTERALMEMORY g_InteralMemory;
extern int whichUart;
extern U8 com_frequence;
extern U8 g_com_Flag ;
INT8U ReadBit(INT16U DevNumber,char *DevName)
{
INT8U bitValue;
INT8U num;
int count;
if((strcmp(DevName,"P_KB")==0) ||(strcmp(DevName,"P_B")==0 ))
{
return Inn_ReadBit(DevNumber,DevName);
}
com_frequence++;
if(com_frequence > 2)
{
com_frequence = 0;
FeedWacthDog();
}
switch(PlcType)
{
case 0:
return FP_ReadBit(DevName[0],DevNumber);
break;
case 1:
return NA_ReadBit(DevNumber);
break;
case 2:
return FX_ReadBit(DevName[0],DevNumber);
break;
case 3:
return DVP_ReadBit(DevName[0],DevNumber);
break;
case 4: //永宏PLC
return FATEK_ReadBit(DevName[0],DevNumber);
break;
case 7:
return Scm_ReadBit(DevNumber);
break;
case 8:
return UniTel_ReadBit(DevNumber);
break;
case 9://080731 RTU
{
num = 0;
do
{
for(count=0; count<DELAY_TIME; count++); // 10000
bitValue = RTU_ReadBit(DevNumber);
num++;
}
while((0xff == bitValue) && (num<2)); // 2 通讯失败的时候多试几次
return bitValue;
}
//return RTU_ReadBit(DevNumber);
break;
case 10:
return VIGOR_ReadBit(DevName[0],DevNumber);
break;
default: return 255;
}
}
INT8U WriteBit(INT16U DevNumber,char *DevName,U8 Value)
{
INT8U boolSucceed;
INT8U num;
int count;
if((strcmp(DevName,"P_KB")==0) ||(strcmp(DevName,"P_B")==0 ))
{
return Inn_WriteBit(DevNumber,DevName,Value);
}
com_frequence++;
if(com_frequence > 2)
{
com_frequence = 0;
FeedWacthDog();
}
switch(PlcType)
{
case 0:
boolSucceed = FP_WriteBit(DevName[0],DevNumber,Value);
break;
case 1:
boolSucceed = NA_WriteBit(DevNumber,Value);
break;
case 2:
boolSucceed = FX_WriteBit(DevName[0],DevNumber,Value);
break;
case 3:
boolSucceed = DVP_WriteBit(DevName[0],DevNumber,Value);
break;
case 4:
boolSucceed = FATEK_WriteBit(DevName[0],DevNumber,Value);
break;
case 7:
{
num = 0;
do
{
boolSucceed = Scm_WriteBit(DevNumber,Value);
num++;
}
while((FALSE == boolSucceed) && (num<3)); // 通讯失败的时候多试几次
//return boolSucceed;
}
//return Scm_WriteBit(DevNumber,Value);
break;
case 8:
boolSucceed = UniTel_WriteBit(DevNumber,Value);
break;
case 9://080731 RTU
{
num = 0;
do
{
for(count=0; count<DELAY_TIME; count++); // 10000
boolSucceed = RTU_WriteBit(DevNumber,Value);
num++;
}
while((FALSE == boolSucceed) && (num<5)); // 通讯失败的时候多试几次3
//return boolSucceed;
}
//return RTU_WriteBit(DevNumber,Value);
break;
case 10:
boolSucceed = VIGOR_WriteBit(DevName[0],DevNumber,Value);
break;
default: boolSucceed = 255;
}
return boolSucceed;
}
int ReadWord(INT16U DevNumber,char *DevName)
{
int wordValue;
INT8U num;
int count;
if((strcmp(DevName,"P_KW")==0) ||(strcmp(DevName,"P_W")==0 ))
{
return Inn_ReadWord(DevNumber,DevName);
}
com_frequence++;
if(com_frequence > 2)
{
com_frequence = 0;
FeedWacthDog();
}
g_com_Flag = TRUE; // 通讯标志
switch(PlcType)
{
case 0:
return FP_ReadWord(DevName,DevNumber);
break;
case 1:
return NA_ReadWord(DevNumber);
break;
case 2:
return FX_ReadWord(DevName,DevNumber);
break;
case 3:
return DVP_ReadWord(DevName[0],DevNumber);
break;
case 4:
return FATEK_ReadWord(DevName[0],DevNumber);
break;
case 7:
return Scm_ReadWord(DevNumber);
break;
case 8:
return UniTel_ReadWord(DevNumber);
break;
case 9://080731 RTU
{
num = 0;
do
{
for(count=0; count<DELAY_TIME; count++); // 10000
wordValue = RTU_ReadWord(DevNumber);
num++;
}
while((-1 == wordValue) && (num<2)); // 2 通讯失败的时候多试几次
return wordValue;
}
// return RTU_ReadWord(DevNumber);
break;
case 10:
return VIGOR_ReadWord(DevName[0],DevNumber);
break;
default: return 255;
}
}
INT8U WriteWord(INT16U DevNumber,char *DevName,int Value)
{
INT8U boolSucceed;
INT8U num;
int count;
if((strcmp(DevName,"P_KW")==0) ||(strcmp(DevName,"P_W")==0 ))
{
return Inn_WriteWord(DevNumber,DevName,Value);
}
com_frequence++;
if(com_frequence > 2)
{
com_frequence = 0;
FeedWacthDog();
}
switch(PlcType)
{
case 0:
return FP_WriteWord(DevName,DevNumber,Value);
break;
case 1:
return NA_WriteWord(DevNumber,Value);
break;
case 2:
return FX_WriteWord(DevName,DevNumber,Value);
break;
case 3:
return DVP_WriteWord(DevName[0],DevNumber,Value);
break;
case 4:
return FATEK_WriteWord(DevName[0],DevNumber,Value);
break;
case 7:
{
num = 0;
do
{
boolSucceed = Scm_WriteWord(DevNumber,Value);
num++;
}
while((FALSE == boolSucceed) && (num<3)); // 通讯失败的时候多试几次
return boolSucceed;
}
//return Scm_WriteWord(DevNumber,Value);
break;
case 8:
return UniTel_WriteWord(DevNumber,Value);
break;
case 9://080731 RTU
{
num = 0;
do
{
for(count=0; count<DELAY_TIME; count++); // 10000
boolSucceed = RTU_WriteWord(DevNumber,Value);
num++;
}
while((FALSE == boolSucceed) && (num<5)); // 通讯失败的时候多试几次3
return boolSucceed;
}
//return RTU_WriteWord(DevNumber,Value);
break;
case 10:
return VIGOR_WriteWord(DevName[0],DevNumber,Value);
break;
default: return 255;
}
}
////////////////////////////////////////////////////////////////////////////////////////
/////////// NERO___PLC ///////////
////////////////////////////////////////////////////////////////////////////////////////
INT8U NA_ReadBit(INT16U DevNumber)
{
U8 auchMsg[7],SendArray[16],RecArray[16];
U8 Result=FALSE;
U8 Count;
int i;
auchMsg[0]=1;
auchMsg[1]=1;
auchMsg[2]=DevNumber/256;
auchMsg[3]=DevNumber % 256;
auchMsg[4]=0;
auchMsg[5]=1;
auchMsg[6]=LRC(auchMsg,6); //校验码
SendArray[0]=0x3a;
for(Count=0;Count<7;Count++)
{
SendArray[Count*2+1]=DataToChar(auchMsg[Count]/16);
SendArray[Count*2+2]=DataToChar(auchMsg[Count] % 16);
}
SendArray[15]=0x0a;
//rINTMSK |=BIT_GLOBAL;
for(Count=0;Count<16;Count++) //发字符串
{
Uart_SendByte(SendArray[Count]);
}
for(i=0;i<1000;i++);
Count = 0;
while((RecArray[0]=Uart_Getch())!=':' && Count<=20) Count++;
if(RecArray[0]==':')
{
RecArray[1]=Uart_Getch();
if((RecArray[2]=Uart_Getch())!=0)
{
for(Count=3;Count<15;Count++) //收字符串
{
RecArray[Count]=Uart_Getch();
}
}
else
Result=3;
}
else
Result=3;
if(Result!=3)
{
if(RecArray[4]=='1' && RecArray[3]=='0' && RecArray[7]=='0' && RecArray[8]=='1')
{
Result=TRUE;
}
}
//rINTMSK &=~(BIT_GLOBAL|BIT_EINT1);
return Result ;
}
U8 NA_WriteBit(INT16U DevNumber,U8 Value)
{
U8 auchMsg[7],SendArray[16],RecArray[17];
U8 Count;
U8 Result=FALSE;
int i;
auchMsg[0]=1;
auchMsg[1]=5;
auchMsg[2]=DevNumber/256;
auchMsg[3]=DevNumber % 256;
if(Value==0)
{
auchMsg[4]=0;
auchMs