private AisStatus LOCAL_bufWrite(Byte[] buffer, Int32 index, Int32 Bytes, Int32 timeout) { Int32 xmtSize = ioBits / 8; AisStatus status = AisStatus.IN_PROGRESS; // check that we can write specified Byte count cleanly if (Bytes % xmtSize != 0) { logFxn(String.Format("(AIS Parse): Cannot write {0} Bytes in chunks of {1}!", Bytes, xmtSize)); return(AisStatus.ERROR); } // perform IO transaction in N-bit "bites" for (Int32 i = 0; i < Bytes / xmtSize; i++) { status |= writeFxn(buffer, index + i * xmtSize, xmtSize, timeout); LOCAL_delay(ioDelay); if (status < 0) { logFxn("(AIS Parse): I/O Error in write!"); break; } } return(status); }
private AisStatus AIS_OS(UInt32 command) { UInt32 xmtWord = command; UInt32 rcvWord = 0; Byte[] rcvWordB = new Byte[4]; AisStatus status = AisStatus.IN_PROGRESS; Int32 retryCnt = 0; Int32 retryCntMax = 10; while (true) { // send ping status |= LOCAL_bufWrite(LOCAL_UInt322b(xmtWord), 0, 4, ioTimeout); // receive pong if (status >= 0) { status |= LOCAL_bufRead(rcvWordB, 0, 4, ioTimeout); rcvWord = LOCAL_b2UInt32(rcvWordB); } // fail on IO error if (status < 0) { LOCAL_delay(opcodeDelay); if (retryCnt++ >= retryCntMax) { logFxn(String.Format("(AIS Parse): Opcode Sync failed after {0} consecutive I/O failures.", retryCnt)); return(AisStatus.ERROR); } // send zero word (32 bits) to clear potentially corrupted state on target status |= LOCAL_bufWrite(LOCAL_UInt322b(0), 0, 4, ioTimeout); status = 0; continue; } // pass on proper response if (rcvWord == AIS_opcode2ack(xmtWord)) { if (retryCnt > 0) { logFxn(String.Format("(AIS Parse): Opcode Sync passed after {0} consecutive I/O failures.", retryCnt)); } return(AisStatus.IN_PROGRESS); } } }
// Start Word Sync Function private AisStatus AIS_SWS() { UInt32 rcvWord = 0; Byte[] rcvWordB = new Byte[4]; UInt32 xmtWord = AIS_XMT_START(ioBits); AisStatus status = AisStatus.IN_PROGRESS; while (true) { // send xmt start status |= LOCAL_bufWrite(LOCAL_UInt322b(xmtWord), 0, ioBits / 8, ioTimeout); if (status < 0) { status = 0; LOCAL_delay(opcodeDelay); continue; } // receive word status |= LOCAL_bufRead(rcvWordB, 0, ioBits / 8, ioTimeout); rcvWord = LOCAL_b2UInt32(rcvWordB); // fail on IO error if (status < 0) { return(AisStatus.ERROR); } // break if word is rcv start if (rcvWord == AIS_RCV_START(ioBits)) { break; } } return(AisStatus.IN_PROGRESS); }