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