// ######################################################################################## // called from CANForm CANErrorTimer to reset error register private void ResetErrorReg() { int Result; byte[] Buf = new Byte[4]; Buf[0] = 0; Result = C4UFX.WriteReg(0, 0, 0x2D, 1, Buf); }
// #################################################################### // called from the timer service on the CANForm every 10ms private void CANService() { CheckForIncomingCANMessages(); if (--CANStatus <= 0) { CANCheckStatus(false); CANStatus = 100; // every second } if (CANSendQueue.Count > 0) // send any messages in the queue { int error = 0; C4UFX.CANMessage mess = CANSendQueue.Dequeue(); C4UFX.SendCANMess(0, 0, mess, out error); DisplayCANData(mess); } }
// #################################################################### private void CheckForIncomingCANMessages() { int Result = C4UFX.Resp_Ack; C4UFX.CANMessage CANMess = new C4UFX.CANMessage(); while (Result == C4UFX.Resp_Ack) { Result = C4UFX.GetCANMess(0, 0, ref CANMess); if (Result == C4UFX.Resp_Ack) { DisplayCANData(CANMess); if (inCANMessage != null) { inCANMessage(CANMess); // call our delegate } } MIBText.Text = C4UFX.GetNumCANMess(0, 0).ToString(); } }
// ######################################################################################## private bool CANInit() { int Result; Result = C4UFX.EnableTS(0, 0); Result = C4UFX.ResetInterface(0, 0); // clear out any misc messages if (Result != C4UFX.Resp_Ack) { CANCheckStatus(true); return(false); } Result = C4UFX.ClearCANMess(0, 0); // clear out any misc messages if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.SetCANBaud(0, 0, CANBAUD); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.ClearCANMess(0, 0); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.RecCANMess(0, 0, CANRECMESS0, 0x0C + 1); //'//MO0 + enable rollover bit if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.RecCANMess(0, 0, CANRECMESS1, 0x1C); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.SetFilters(0, 0, 0, CANRECFILTER0, true); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.SetFilters(0, 0, 1, CANRECFILTER1, true); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.SetFilters(0, 0, 2, CANRECFILTER2, true); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.SetFilters(0, 0, 3, CANRECFILTER3, true); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.SetFilters(0, 0, 4, CANRECFILTER4, true); if (Result != 1) { CANCheckStatus(true); return(false); } Result = C4UFX.SetFilters(0, 0, 5, CANRECFILTER5, true); if (Result != 1) { CANCheckStatus(true); return(false); } return(true); }
// ######################################################################################## private void CANCheckStatus(bool ForceErr) { int Result; if ((CANInterfaceStatus != CANSTAT.OK) | (ForceErr == true)) { C4UFX.CANMessage CANMess = new C4UFX.CANMessage(); int Error; CANMess.ID = 0; CANMess.DLC = 0; for (int c = 0; c < 8; c++) { CANMess.Data[c] = 0; } CANMess.EID = true; CANMess.RTR = false; CANMess.MO = 0; CANMess.SendOption = 7; Result = C4UFX.SendCANMess(0, 0, CANMess, out Error); if (Result != C4UFX.Resp_Ack) { switch (Result) { case C4UFX.Resp_NAck: // message was transmitted but there was an error, so reset CANInit(); SetCANInterfaceError(CANSTAT.SENDERR); break; case C4UFX.Resp_ErrImproperResponse: SetCANInterfaceError(CANSTAT.NOINT); break; default: SetCANInterfaceError(CANSTAT.ERROR); break; } } else { SetCANInterfaceError(CANSTAT.OK); } } else // we're connected and working so display status register { byte[] DataBuf = new byte[10]; Result = C4UFX.ReadReg(0, 0, 0x2D, 1, ref DataBuf); // EFLG if (Result == C4UFX.Resp_Ack) { if (DataBuf[0] == 0) { SetInterfaceStatusLabelVisible(0, true); } else { SetInterfaceStatusLabelVisible(0, false); } if ((DataBuf[0] & 0x80) > 0) { SetInterfaceStatusLabelVisible(7, true); SetCANErrorTimerState(true); } else { SetInterfaceStatusLabelVisible(7, false); } if ((DataBuf[0] & 0x40) > 0) { SetInterfaceStatusLabelVisible(6, true); SetCANErrorTimerState(true); } else { SetInterfaceStatusLabelVisible(6, false); } if ((DataBuf[0] & 0x20) > 0) { SetInterfaceStatusLabelVisible(5, true); } else { SetInterfaceStatusLabelVisible(5, false); } if ((DataBuf[0] & 0x10) > 0) { SetInterfaceStatusLabelVisible(4, true); } else { SetInterfaceStatusLabelVisible(4, false); } if ((DataBuf[0] & 0x08) > 0) { SetInterfaceStatusLabelVisible(3, true); } else { SetInterfaceStatusLabelVisible(3, false); } if ((DataBuf[0] & 0x04) > 0) { SetInterfaceStatusLabelVisible(2, true); } else { SetInterfaceStatusLabelVisible(2, false); } if ((DataBuf[0] & 0x02) > 0) { SetInterfaceStatusLabelVisible(1, true); } else { SetInterfaceStatusLabelVisible(1, false); } } else { } } }