00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include <cob_generic_can/CanPeakSysUSB.h>
00055 #include <stdlib.h>
00056 #include <cerrno>
00057 #include <cstring>
00058 #include <stdlib.h>
00059 #include <sys/types.h>
00060 #include <sys/stat.h>
00061 #include <fcntl.h>
00062
00063
00064 CANPeakSysUSB::CANPeakSysUSB(const char* cIniFile)
00065 {
00066 m_bInitialized = false;
00067
00068
00069 m_IniFile.SetFileName(cIniFile, "CanPeakSysUSB.cpp");
00070
00071 init();
00072 }
00073
00074
00075 CANPeakSysUSB::~CANPeakSysUSB()
00076 {
00077 if (m_bInitialized)
00078 {
00079 CAN_Close(m_handle);
00080 }
00081 }
00082
00083
00084 void CANPeakSysUSB::init()
00085 {
00086 std::string sCanDevice;
00087
00088 if( m_IniFile.GetKeyString( "TypeCan", "DevicePath", &sCanDevice, false) != 0) {
00089 sCanDevice = "/dev/pcan32";
00090 } else std::cout << "CAN-device path read from ini-File: " << sCanDevice << std::endl;
00091
00092
00093 m_handle = LINUX_CAN_Open(sCanDevice.c_str(), O_RDWR);
00094
00095 if (! m_handle)
00096 {
00097
00098 std::cout << "Cannot open CAN on USB: " << strerror(errno) << std::endl;
00099 sleep(3);
00100 exit(0);
00101 }
00102
00103 int ret = CAN_ERR_OK;
00104 int iBaudrateVal = 0;
00105 m_IniFile.GetKeyInt( "CanCtrl", "BaudrateVal", &iBaudrateVal, true);
00106
00107 switch(iBaudrateVal)
00108 {
00109 case 0:
00110 ret = CAN_Init(m_handle, CAN_BAUD_1M, CAN_INIT_TYPE_ST);
00111 break;
00112 case 2:
00113 ret = CAN_Init(m_handle, CAN_BAUD_500K, CAN_INIT_TYPE_ST);
00114 break;
00115 case 4:
00116 ret = CAN_Init(m_handle, CAN_BAUD_250K, CAN_INIT_TYPE_ST);
00117 break;
00118 case 6:
00119 ret = CAN_Init(m_handle, CAN_BAUD_125K, CAN_INIT_TYPE_ST);
00120 break;
00121 case 9:
00122 ret = CAN_Init(m_handle, CAN_BAUD_50K, CAN_INIT_TYPE_ST);
00123 break;
00124 case 11:
00125 ret = CAN_Init(m_handle, CAN_BAUD_20K, CAN_INIT_TYPE_ST);
00126 break;
00127 case 13:
00128 ret = CAN_Init(m_handle, CAN_BAUD_10K, CAN_INIT_TYPE_ST);
00129 break;
00130 }
00131
00132 if(ret)
00133 {
00134 std::cout << "CANPeakSysUSB::CANPeakSysUSB(), error in init" << std::endl;
00135 }
00136 else
00137 {
00138 std::cout << "CANPeakSysUSB::CanpeakSys(), init ok" << std::endl;
00139 m_bInitialized = true;
00140 }
00141 }
00142
00143
00144 bool CANPeakSysUSB::transmitMsg(CanMsg CMsg, bool bBlocking)
00145 {
00146 TPCANMsg TPCMsg;
00147 bool bRet = true;
00148
00149 if (m_bInitialized == false) return false;
00150
00151
00152 TPCMsg.LEN = CMsg.m_iLen;
00153 TPCMsg.ID = CMsg.m_iID;
00154 TPCMsg.MSGTYPE = CMsg.m_iType;
00155 for(int i=0; i<8; i++)
00156 TPCMsg.DATA[i] = CMsg.getAt(i);
00157
00158
00159 int iRet;
00160 iRet = CAN_Write(m_handle, &TPCMsg);
00161 iRet = CAN_Status(m_handle);
00162
00163 if(iRet < 0)
00164 {
00165 std::cout << "CANPeakSysUSB::transmitMsg, errorcode= " << nGetLastError() << std::endl;
00166 bRet = false;
00167 }
00168
00169
00170 return bRet;
00171 }
00172
00173
00174 bool CANPeakSysUSB::receiveMsg(CanMsg* pCMsg)
00175 {
00176 TPCANRdMsg TPCMsg;
00177 TPCMsg.Msg.LEN = 8;
00178 TPCMsg.Msg.MSGTYPE = 0;
00179 TPCMsg.Msg.ID = 0;
00180
00181 int iRet = CAN_ERR_OK;
00182 bool bRet = false;
00183
00184 if (m_bInitialized == false) return false;
00185
00186 iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0);
00187
00188 if (iRet == CAN_ERR_OK)
00189 {
00190 pCMsg->m_iID = TPCMsg.Msg.ID;
00191 pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
00192 TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
00193 bRet = true;
00194 }
00195 else if (CAN_Status(m_handle) != CAN_ERR_QRCVEMPTY)
00196 {
00197
00198 std::cout << "CANPeakSysUSB::receiveMsg, errorcode= " << nGetLastError() << std::endl;
00199 pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
00200 }
00201
00202 return bRet;
00203 }
00204
00205
00206 bool CANPeakSysUSB::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
00207 {
00208 int i, iRet;
00209
00210 TPCANRdMsg TPCMsg;
00211 TPCMsg.Msg.LEN = 8;
00212 TPCMsg.Msg.MSGTYPE = 0;
00213 TPCMsg.Msg.ID = 0;
00214
00215 if (m_bInitialized == false) return false;
00216
00217
00218 bool bRet = true;
00219 iRet = CAN_ERR_OK;
00220 i=0;
00221 do
00222 {
00223 iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0);
00224
00225 if(iRet == CAN_ERR_OK)
00226 break;
00227
00228 i++;
00229 usleep(100000);
00230 }
00231 while(i < iNrOfRetry);
00232
00233
00234 if(iRet != CAN_ERR_OK)
00235 {
00236 std::cout << "CANPeakSysUSB::receiveMsgRetry, errorcode= " << nGetLastError() << std::endl;
00237 pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
00238 bRet = false;
00239 }
00240 else
00241 {
00242 pCMsg->m_iID = TPCMsg.Msg.ID;
00243 pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
00244 TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
00245 }
00246
00247 return bRet;
00248 }
00249