}// end RESET_J1939 //-FUNCTION----------------------------------------------------------------- // Routine : MM_J1939 // Description : Set the gateway's message mode // Returncode : None // ------------------------------------------------------------------------- public static void MM_J1939(byte nMsgMode) { msg_SETMSGMODE[SETMSGMODE_IDX_MODE] = nMsgMode; msg_SETMSGMODE[SETMSGMODE_IDX_CHKSUM] = ComputeCheckSum(msg_SETMSGMODE); // Transmit the message to the COM port COMPort.Transmit(msg_SETMSGMODE, MSG_LEN_SETMSGMODE + 3); }// end MM_J1939
}// end SA_J1939 //-FUNCTION----------------------------------------------------------------- // Routine : RQ_J1939 // Description : Request info from the gateway // Returncode : None // ------------------------------------------------------------------------- public static void RQ_J1939(byte nID) { msg_RQ[4] = nID; msg_RQ[5] = ComputeCheckSum(msg_RQ); // Transmit the message to the COM port COMPort.Transmit(msg_RQ, 6); }// end RQ_J1939
}// end SH_J1939 //-FUNCTION----------------------------------------------------------------- // Routine : SA_J1939 // Description : Set ACK message active (=1) / inactive (=0) // Returncode : None // ------------------------------------------------------------------------- public static void SA_J1939(byte nActive) { msg_SETACK[SETACK_IDX_ACTIVE] = nActive; msg_SETACK[SETACK_IDX_CHKSUM] = ComputeCheckSum(msg_SETACK); // Transmit the message to the COM port COMPort.Transmit(msg_SETACK, 6); }// end SA_J1939
}// end RQ_J1939 //-FUNCTION----------------------------------------------------------------- // Routine : RESET_J1939 // Description : Reset the gateway // Returncode : None // ------------------------------------------------------------------------- public static void RESET_J1939() { msg_RESET[7] = ComputeCheckSum(msg_RESET); // Transmit the message to the COM port COMPort.Transmit(msg_RESET, 8); System.Threading.Thread.Sleep(100); // Time in msec }// end RESET_J1939
//-FUNCTION----------------------------------------------------------------- // Routine : SH_J1939 // Description : Set heartbeat frequency in milliseconds // Returncode : None // ------------------------------------------------------------------------- public static void SH_J1939(int nFrequency) { msg_SETHEART[SETHEART_IDX_FREQMSB] = (byte)(nFrequency >> 8); msg_SETHEART[SETHEART_IDX_FREQLSB] = (byte)(nFrequency & 0xFF); msg_SETHEART[SETHEART_IDX_CHKSUM] = ComputeCheckSum(msg_SETHEART); // Transmit the message to the COM port COMPort.Transmit(msg_SETHEART, 7); }// end SH_J1939
}// end FD_J1939 //-FUNCTION----------------------------------------------------------------- // Routine : FA_FD_1939 // Description : Creates filter message, adds PGN & checksum, and sends it // Returncode : None // ------------------------------------------------------------------------- public static void FA_FD_J1939(long lPGN) { // Declarations int nMsgLen = MSG_LEN_FA + 3; // Fill the port number and PGN msg_FA[FA_IDX_PGN_MSB] = (byte)((lPGN & 0xFF0000) >> 16); msg_FA[FA_IDX_PGN_2ND] = (byte)((lPGN & 0x00FF00) >> 8); msg_FA[FA_IDX_PGN_LSB] = (byte)(lPGN & 0x0000FF); // Process and fill the checksum (does not include stuff bytes) msg_FA[FA_IDX_CHKSUM] = ComputeCheckSum(msg_FA); // Determine the total message length by scanning for START and ESC tokens for (int nIndex = 1; nIndex < MSG_LEN_FA + 3; nIndex++) { if (msg_FA[nIndex] == MSG_TOKEN_START || msg_FA[nIndex] == MSG_TOKEN_ESC) { nMsgLen++; } } if (msg_FA[FA_IDX_CHKSUM] == MSG_TOKEN_START || msg_FA[FA_IDX_CHKSUM] == MSG_TOKEN_ESC) { nMsgLen++; } // Resize the message to be transmitted byte[] pMsg = new byte[nMsgLen]; // Copy the message; insert stuff bytes where necessary pMsg[0] = MSG_TOKEN_START; // Insert the START token int nPointer = 1; for (int nIndex = 1; nIndex < MSG_LEN_FA + 3; nIndex++) { if (msg_FA[nIndex] == MSG_TOKEN_START) { pMsg[nPointer++] = MSG_TOKEN_ESC; pMsg[nPointer++] = MSG_START_STUFF; }// end if else if (msg_FA[nIndex] == MSG_TOKEN_ESC) { pMsg[nPointer++] = MSG_TOKEN_ESC; pMsg[nPointer++] = MSG_ESC_STUFF; }// end else if else { pMsg[nPointer++] = msg_FA[nIndex]; } }// end for // Transmit the message to the COM port COMPort.Transmit(pMsg, nMsgLen); }// end FA_FD_J1939
}// end MM_J1939 //-FUNCTION----------------------------------------------------------------- // Routine : SET_J1939 // Description : Set J1939 Parameters // Returncode : None // ------------------------------------------------------------------------- public static void SET_J1939(byte nSrcAddr, byte nAddrBottom, byte nAddrTop, byte nOpMode, byte[] pNAME, bool bRequestRESTATUSMessage) { if (bRequestRESTATUSMessage == true) { msg_SET1[SET_IDX_SA] = nSrcAddr; msg_SET1[SET_IDX_BOTTOM] = nAddrBottom; msg_SET1[SET_IDX_TOP] = nAddrTop; msg_SET1[SET_IDX_OPMODE] = nOpMode; for (int nIndex = 0; nIndex < 8; nIndex++) { msg_SET1[nIndex + SET_IDX_NAME] = pNAME[nIndex]; } msg_SET1[SET_IDX_CHKSUM] = ComputeCheckSum(msg_SET1); // Transmit the message to the COM port COMPort.Transmit(msg_SET1, MSG_LEN_SET); }// end if else { msg_SET[SET_IDX_SA] = nSrcAddr; msg_SET[SET_IDX_BOTTOM] = nAddrBottom; msg_SET[SET_IDX_TOP] = nAddrTop; msg_SET[SET_IDX_OPMODE] = nOpMode; for (int nIndex = 0; nIndex < 8; nIndex++) { msg_SET[nIndex + SET_IDX_NAME] = pNAME[nIndex]; } msg_SET[SET_IDX_CHKSUM] = ComputeCheckSum(msg_SET); // Transmit the message to the COM port COMPort.Transmit(msg_SET, MSG_LEN_SET); } // end else } // end SET_J1939
}// end TX_J1939 //-FUNCTION----------------------------------------------------------------- // Routine : TXP_J1939 // Description : Creates periodic transmit message, adds parameters & checksum, // and sends it // Returncode : None // ------------------------------------------------------------------------- public static void TXP_J1939(long lPGN, int nDest, int nSrc, int nPriority, byte[] nData, int nDataLen, int nInterval, bool bLoopback) { // Declarations int nMsgLenUnstuffed = MSG_LEN_TXP + nDataLen; // Fill all parameters byte[] pUnstMsg = new byte[nMsgLenUnstuffed + 3]; // Add header length pUnstMsg[TXP_IDX_MSGSTART] = MSG_TOKEN_START; pUnstMsg[TXP_IDX_MSGLENMSB] = (byte)((nMsgLenUnstuffed & 0xFF00) >> 8); pUnstMsg[TXP_IDX_MSGLENLSB] = (byte)(nMsgLenUnstuffed & 0xFF); if (bLoopback == true) { pUnstMsg[TXP_IDX_MSGID] = MSG_ID_TXPL; } else { pUnstMsg[TXP_IDX_MSGID] = MSG_ID_TXP; } pUnstMsg[TXP_IDX_PGNMSB] = (byte)((lPGN & 0xFF0000) >> 16); pUnstMsg[TXP_IDX_PGN2ND] = (byte)((lPGN & 0x00FF00) >> 8); pUnstMsg[TXP_IDX_PGNLSB] = (byte)(lPGN & 0x0000FF); pUnstMsg[TXP_IDX_DESTADDR] = (byte)nDest; pUnstMsg[TXP_IDX_SRCADDR] = (byte)nSrc; pUnstMsg[TXP_IDX_PRIORITY] = (byte)nPriority; pUnstMsg[TXP_IDX_FREQMSB] = (byte)((nInterval & 0xFF00) >> 8); pUnstMsg[TXP_IDX_FREQLSB] = (byte)(nInterval & 0x00FF); // Add actual data without stuff bytes nMsgLenUnstuffed += 3; // Add the header length int nPointer = TXP_IDX_DATASTART; for (int nIndex = 0; nIndex < nDataLen; nIndex++) { pUnstMsg[nPointer++] = nData[nIndex]; } // Process and fill the checksum (does not include stuff bytes) pUnstMsg[nPointer++] = ComputeCheckSum(pUnstMsg); // Determine the total message length by scanning for START and ESC tokens int nMsgLenStuffed = nPointer; for (int nIndex = 1; nIndex < nPointer; nIndex++) { if (pUnstMsg[nIndex] == MSG_TOKEN_START || pUnstMsg[nIndex] == MSG_TOKEN_ESC) { nMsgLenStuffed++; } } // Resize the message to be transmitted byte[] pMsg = new byte[nMsgLenStuffed]; // Copy the message; insert stuff bytes where necessary pMsg[0] = MSG_TOKEN_START; // Insert the START token nPointer = 1; for (int nIndex = 1; nIndex < nMsgLenUnstuffed; nIndex++) { if (pUnstMsg[nIndex] == MSG_TOKEN_START) { pMsg[nPointer++] = MSG_TOKEN_ESC; pMsg[nPointer++] = MSG_START_STUFF; }// end if else if (pUnstMsg[nIndex] == MSG_TOKEN_ESC) { pMsg[nPointer++] = MSG_TOKEN_ESC; pMsg[nPointer++] = MSG_ESC_STUFF; }// end else if else { pMsg[nPointer++] = pUnstMsg[nIndex]; } }// end for // Transmit the message to the COM port COMPort.Transmit(pMsg, nMsgLenStuffed); }// end TXP_J1939