VB6 应用程序与 Java 8u60 的串行通信
VB6 application vs Java 8u60 for Serial Communication
我正在编写 Java 应用程序来替换 VB6 应用程序。应用程序必须使用 10 字节二进制协议轮询远程系统,响应也是 10 字节二进制消息,其中最后一个字节是通过对所有先前字节求和计算得出的校验和。
我遇到的问题是 Java 应用程序中校验和不匹配的大量错误。
结果非常可重复。 VB6 应用程序几乎没有错误,但是 Java 应用程序有很多错误。我检查了端口设置,它们是相同的 19200,n,8,1.
在VB6中端口设置串口设置如下:
CDTimeout = 0
CommPort = 1
CTSTimeout = 0
DSRTimeout = 0
DTREnable = False
EOFEnable = False
Handshaking = None
InBufferCount = 0
InBufferSize = 1024
Index = 0
InputLen = 0
InputMode = comInputModeBinary
Interval = 1000
Name = "comInterfaceUnit"
NullDiscard = False
OutBufferSize = 512
ParityReplace = "?"
RThreshold = 10
RTSEnable = False
Settings = "19200,n,8,1"
SThreshold = 0
Tag = ""
javax.comm串口设置如下:
baudrate = 19200
closed = false
databits = 8
dtr = false
flowcontrol = 0
framing = false
framingByte = 0
framingByteReceived = false
name = "COM1"
nativeHandle = 324295048
notifyMask = 1
parity = 0
rcvThreshold = -1
rcvTimeout = -1
rts = true
startBI = false
stateCD = false
stateCTS = false
stateDSR = false
stateFE = false
stateOE = false
statePE = false
stateRI = false
stopBits = 1
在 VB6 中,事件处理程序只是将接收到的字节复制到一个字节数组中,这正是 Java 代码通过 SerialEvent 所做的。
Java 校验和计算:
int intChecksumInPkt = (int)(arychrMsg[RESPONSE_LENGTH -1] & 0xff) ,intCalcChecksum = 0;
for( int b=0; b<RESPONSE_LENGTH -1; b++ ) {
intCalcChecksum += (int)(arychrMsg[b] & 0xff);
}
intCalcChecksum &= 0xff;
if ( intCalcChecksum == intChecksumInPkt ) { ... }
VB6校验码:
intSum = 0
For intI = gintInterfaceUnitReplyStatusOffset To gintInterfaceUnitReplyData8Offset
intSum = intSum + CInt(gbytInterfaceUnitReplyArray(gintCommPortIndex, intI))
Next intI
If gbytInterfaceUnitReplyArray(gintCommPortIndex, gintInterfaceUnitReplyChecksumOffset) = CByte(intSum And &HFF) Then
' Match, so a 'good' message.
blnFunctionValue = True
End If
实际问题是我没有调用 "enableReceiveThreshold"。
现在有效。
我正在编写 Java 应用程序来替换 VB6 应用程序。应用程序必须使用 10 字节二进制协议轮询远程系统,响应也是 10 字节二进制消息,其中最后一个字节是通过对所有先前字节求和计算得出的校验和。
我遇到的问题是 Java 应用程序中校验和不匹配的大量错误。
结果非常可重复。 VB6 应用程序几乎没有错误,但是 Java 应用程序有很多错误。我检查了端口设置,它们是相同的 19200,n,8,1.
在VB6中端口设置串口设置如下:
CDTimeout = 0
CommPort = 1
CTSTimeout = 0
DSRTimeout = 0
DTREnable = False
EOFEnable = False
Handshaking = None
InBufferCount = 0
InBufferSize = 1024
Index = 0
InputLen = 0
InputMode = comInputModeBinary
Interval = 1000
Name = "comInterfaceUnit"
NullDiscard = False
OutBufferSize = 512
ParityReplace = "?"
RThreshold = 10
RTSEnable = False
Settings = "19200,n,8,1"
SThreshold = 0
Tag = ""
javax.comm串口设置如下:
baudrate = 19200
closed = false
databits = 8
dtr = false
flowcontrol = 0
framing = false
framingByte = 0
framingByteReceived = false
name = "COM1"
nativeHandle = 324295048
notifyMask = 1
parity = 0
rcvThreshold = -1
rcvTimeout = -1
rts = true
startBI = false
stateCD = false
stateCTS = false
stateDSR = false
stateFE = false
stateOE = false
statePE = false
stateRI = false
stopBits = 1
在 VB6 中,事件处理程序只是将接收到的字节复制到一个字节数组中,这正是 Java 代码通过 SerialEvent 所做的。
Java 校验和计算:
int intChecksumInPkt = (int)(arychrMsg[RESPONSE_LENGTH -1] & 0xff) ,intCalcChecksum = 0;
for( int b=0; b<RESPONSE_LENGTH -1; b++ ) {
intCalcChecksum += (int)(arychrMsg[b] & 0xff);
}
intCalcChecksum &= 0xff;
if ( intCalcChecksum == intChecksumInPkt ) { ... }
VB6校验码:
intSum = 0
For intI = gintInterfaceUnitReplyStatusOffset To gintInterfaceUnitReplyData8Offset
intSum = intSum + CInt(gbytInterfaceUnitReplyArray(gintCommPortIndex, intI))
Next intI
If gbytInterfaceUnitReplyArray(gintCommPortIndex, gintInterfaceUnitReplyChecksumOffset) = CByte(intSum And &HFF) Then
' Match, so a 'good' message.
blnFunctionValue = True
End If
实际问题是我没有调用 "enableReceiveThreshold"。
现在有效。