// //called when the OnBus button is pressed // void CCanTestMFCDlg::OnOnBus() { int stat; int handle; char tmpStr[200]; //init canlib canInitializeLibrary(); //open channel 0 handle = canOpenChannel(0, canOPEN_EXCLUSIVE); if (handle < 0) { sprintf(tmpStr, "ERROR: canOpenChannel() handle returned: %d", handle); AfxMessageBox(tmpStr); } //set up the bus stat = canSetBusParams(handle, canBITRATE_125K, 0, 0, 0, 0, 0); if (stat < 0) { AfxMessageBox("ERROR: canSetBusParams()."); } //go on bus stat = canBusOn(handle); if (stat < 0) { AfxMessageBox("ERROR: canBusOn()."); } //set notification stat = canSetNotify(handle, this->GetSafeHwnd() , canNOTIFY_RX | canNOTIFY_ERROR | canNOTIFY_STATUS | canEVENT_TX | canNOTIFY_ENVVAR); //save the can channel handle in the global variable m_handle m_handle = handle; }
int main(int argc, char *argv[]) { canStatus stat; canHandle hnd; int channelRx; int channelTx; if (argc != 3) { printUsageAndExit(argv[0]); } { char *endPtr = NULL; errno = 0; channelRx = strtol(argv[1], &endPtr, 10); if ( (errno != 0) || ((channelRx == 0) && (endPtr == argv[1])) ) { printUsageAndExit(argv[0]); } channelTx = strtol(argv[2], &endPtr, 10); if ( (errno != 0) || ((channelTx == 0) && (endPtr == argv[2])) ) { printUsageAndExit(argv[0]); } } canInitializeLibrary(); hnd = canOpenChannel(channelRx, canOPEN_REQUIRE_EXTENDED); if (hnd < 0) { printf("canOpenChannel %d", channelRx); check("", hnd); return -1; } stat = canBusOff(hnd); check("canBusOff", stat); if (stat != canOK) { goto ErrorExit; } stat = canSetNotify(hnd, callback, canNOTIFY_ERROR | canNOTIFY_STATUS, NULL); check("canSetNotify", stat); if (stat != canOK) { goto ErrorExit; } stat = canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0); check("canSetBusParams", stat); if (stat != canOK) { goto ErrorExit; } stat = canBusOn(hnd); check("canBusOn", stat); if (stat != canOK) { goto ErrorExit; } incBusLoad(channelTx, canOPEN_REQUIRE_EXTENDED, 0); testBusLoad(hnd); incBusLoad(channelTx, canOPEN_REQUIRE_EXTENDED, 100); testBusLoad(hnd); testBusLoad(hnd); incBusLoad(channelTx, canOPEN_REQUIRE_EXTENDED, 300); testBusLoad(hnd); testBusLoad(hnd); ErrorExit: stat = canBusOff(hnd); check("canBusOff", stat); stat = canClose(hnd); check("canClose", stat); stat = canUnloadLibrary(); check("canUnloadLibrary", stat); return 0; }