Search code examples
javavb6binaryserial-portreceiver

VB6 application vs Java 8u60 for Serial Communication


I am writing a Java application to replace a VB6 application. The application has to poll a remote system using a 10 byte binary protocol, the response is also a 10 byte binary message where the last byte is a checksum calculated by summing up all the previous bytes.

The problem I'm experiencing is a high number of errors in the Java application where the checksum doesn't match.

The results are quite repeatable. The VB6 application has virtually no errors, however the Java application has lots. I've checked the port settings, they are the same 19200,n,8,1.

In VB6 the port is set-up the serial port is set-up as follows:

    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 = ""

The javax.comm SerialPort set-up is as follows:

    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

In VB6 the event handler just copies the receive bytes into a byte array and thats exactly what the Java code does via the SerialEvent.

Java checksum computation:

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 checksum code:

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

Solution

  • The actual problem was I didn't call "enableReceiveThreshold".

    Works now.