发表于:2008-01-14 16:24:00
2楼
A.你首先要看你用的流量计是不是常见的通讯协议,如果是常见的,那么FACVIEW的帮助中可能就有介绍,可以参考着定义通讯设置.
B.如果是流量计厂家自己定义的通讯协议,并且你用串口调试工具已经解读出来了,那么我给你一个简单的例子,你可以参考一下:
首先需要通信设置:
1.I/O服务器
服务器名:自定义
2.主板
主板名:自定义
主板类型:COMX
地址:0
3. 端口
端口名:COM1
端口号:根据使用的串口设置(1,2,3,4)
主板名:同上
波特率:9600
数据位:8
停止位:1
奇偶校验:NONE
不需要添加I/O设备
_____________
下面是一个串口测试程序
/*MUDBUS Slave program*/
INT hTask;
INT hPort;
INT hDev;
STRING buffer_RD; //读缓冲区
STRING buffer_TX; //写缓冲区
INT CRC16rd_Hi; //读缓冲区CRC16校验码高字节
INT CRC16rd_Lo; //读缓冲区CRC16校验码低字节
INT CRC16tx_Hi; //写缓冲区CRC16校验码高字节
INT CRC16tx_Lo; //写缓冲区CRC16校验码低字节
INT
FUNCTION
StartSerial(STRING sPort)
hPort = ComOpen(sPort, 0);
IF hPort < 0 THEN
Prompt("Cannot open port " + sPort);
RETURN -1;
ELSE
RETURN 0;
END
END
INT
FUNCTION
Startdev(STRING sDev)
hDev = DevOpen(sDev, 0);
IF hDev < 0 THEN
Prompt("Cannot open File " + sDev);
RETURN -1;
ELSE
RETURN 0;
END
END
INT
FUNCTION
SerialRead()
INT length;
INT error;
length = 128;
error = ComRead(hPort, buffer_rd, length, 2);
IF error THEN
Prompt("Error from port " + error : ####);
ComReset(hPort);
RETURN -1;
ELSE
RETURN 0;
END
END
INT
FUNCTION
SerialWrite(INT length)
INT error;
error = ComWrite(hPort, buffer_tx, length, 1);
IF error THEN
Prompt("Error Writing port"+error:####);
ComReset(hPort);
RETURN -1;
ELSE
RETURN 0;
END
END
FUNCTION Open_COM(INT Slave)
IF StartSerial("COM1")<>0 THEN
Message("Error","open Serial port Fail!",49);
ComClose(hPort);
ELSE
IF startdev("MSDEV")<>0 THEN
Message("Error","open dBASE file fail!",49);
DevClose(hDev);
ELSE
hTask=TaskNew("Main",slave:##,0);
END
END
END
FUNCTION Close_COM()
ComClose(hPort);
TaskKill(hTask);
DevClose(hDev);
END
FUNCTION Main(INT Slave)
INT error;
WHILE 1 DO
IF IODeviceInfo("5",3)="1" THEN
IF SerialRead()=0 THEN
IF StrGetChar(buffer_rd,0)=Slave THEN
error=Func03(Slave);
IF error>=0 THEN
SerialWrite(error);
END
END
END
END
END
END
INT
FUNCTION Func03(INT slave)
INT addr;
INT number;
INT value,eng_zero,eng_full,scale;
INT value_hi,value_lo;
INT i,j;
IF StrGetChar(buffer_rd,1)=3 THEN
CRC16(6,1);
IF (StrGetChar(buffer_rd,6)=CRC16rd_lo) AND (StrGetChar(buffer_rd,7)=CRC16rd_hi) THEN
number=StrGetChar(buffer_rd,4)*256+StrGetChar(buffer_rd,5);
IF number<=50 THEN
addr=40001+StrGetChar(buffer_rd,2)*256+StrGetChar(buffer_rd,3);
DevFirst(hDEV);
IF DevFind(hDev,StrTrim(IntToStr(addr)),"ADDR")=0 THEN
StrSetChar(buffer_tx,0,slave);
StrSetChar(buffer_tx,1,3);
StrSetChar(buffer_tx,2,number*2);
WHILE NOT DevEOF(hdev) DO
value=TagRead(StrTrim(DevGetField(hDEV,"NAME")));
eng_zero=StrToReal(DevGetField(hDEV,"ENG_ZERO"));
eng_full=StrToReal(DevGetField(hDEV,"ENG_FULL"));
scale=StrToInt(DevGetField(hDEV,"SCALE"));
value_lo=(value*Pow(10,scale)*65535)/((eng_full-eng_zero)*Pow(10,scale));
value_hi=value_lo/256;
value_lo=value_lo-value_hi*256;
StrSetChar(buffer_tx,i*2+3,value_hi);
StrSetChar(buffer_tx,i*2+3+1,value_lo);
i=i+1;
IF i=number THEN
DevSeek(hDEV,DevSize(hDEV));
END
DevNext(hDev);
END
IF i<number THEN
FOR j=i TO number-1 DO
StrSetChar(buffer_tx,j*2+3,0);
StrSetChar(buffer_tx,j*2+3+1,0);
END
END
CRC16(number*2+3,0);
StrSetChar(buffer_tx,number*2+3,CRC16tx_lo);
StrSetChar(buffer_tx,number*2+3+1,CRC16tx_hi);
ELSE
RETURN -1;
END
ELSE
RETURN -1;
END
ELSE
RETURN -1;
END
ELSE
RETURN -1;
END
RETURN number*2+3+2;
END
INT
FUNCTION CRC16(INT total,INT falg)
INT CRC16Lo,CRC16Hi;
INT CL=0x01,CH=0xa0;
INT SaveHi,SaveLo;
INT i,j;
INT getchar;
CRC16Lo=0xFF;
CRC16Hi=0xFF;
FOR i=0 TO Total-1 DO
IF falg=1 THEN
getchar=StrGetChar(buffer_rd,i);
ELSE
getchar=StrGetChar(buffer_tx,i);
END
CRC16Lo=CRC16Lo BITXOR GetChar;
FOR j=0 TO 7 DO
SaveHi=CRC16Hi;
SaveLo=CRC16Lo;
CRC16Hi=CRC16Hi/2;
CRC16Lo=CRC16Lo/2;
IF ((SaveHi BITAND 0x01)=0x01) THEN
CRC16Lo=CRC16Lo BITOR 0x80;
END
IF ((SaveLo BITAND 0x01)=0x01) THEN
CRC16Hi=CRC16Hi BITXOR CH;
CRC16Lo=CRC16Lo BITXOR CL;
END
END
END
IF falg=1 THEN
CRC16rd_hi=CRC16hi;
CRC16rd_lo=CRC16lo;
ELSE
CRC16tx_hi=CRC16hi;
CRC16tx_lo=CRC16lo;
END
RETURN 0;
END