Lesen von CAN Frames vorbereitet
This commit is contained in:
@@ -41,6 +41,20 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename)
|
|||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// socket auf nicht-blockierend umstellen
|
||||||
|
int fcntl_flags = fcntl(intf_data[iMotorIndex].socket, F_GETFL, 0);
|
||||||
|
if (fcntl_flags < 0)
|
||||||
|
{
|
||||||
|
printf("Could not get file descriptor flags!\n");
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
fcntl_flags |= O_NONBLOCK;
|
||||||
|
if (fcntl(intf_data[iMotorIndex].socket, F_SETFL, fcntl_flags) < 0)
|
||||||
|
{
|
||||||
|
printf("Could not set file descriptor flags (set socket none-blocking)!\n");
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
printf("Interface %s (motor %d) opened!\n", ifacename, iMotorIndex);
|
printf("Interface %s (motor %d) opened!\n", ifacename, iMotorIndex);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -177,3 +191,53 @@ void Can_TransmitMotorPower(int iMotorIndex)
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// @brief Read data from CAN interface
|
||||||
|
/// @param iMotorIndex
|
||||||
|
void Can_ReadData(int iMotorIndex)
|
||||||
|
{
|
||||||
|
ssize_t nbytes = 0;
|
||||||
|
struct can_frame frame;
|
||||||
|
|
||||||
|
// wir wollen immer alle verfügbaren Frames lesen
|
||||||
|
while ((nbytes = read(intf_data[iMotorIndex].socket, &frame, sizeof(frame))) > 0)
|
||||||
|
{
|
||||||
|
canid_t pgn = frame.can_id & 0x00FFFF00;
|
||||||
|
|
||||||
|
switch(pgn)
|
||||||
|
{
|
||||||
|
case 0x00F00300: // PGN 61443 "Electronic Engine Controller 2"
|
||||||
|
// haben wir selber gesendet -> ignorieren
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0x00F00500: // PGN 61445 "Electronic Transmission Controller 2"
|
||||||
|
// haben wir selber gesendet -> ignorieren
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0x00FF1300: // PGN 65299 "Manufacturer PGN"
|
||||||
|
// hier finden wir die Zustände der Schalter
|
||||||
|
Can_Read_Manu_PGN(iMotorIndex, &frame);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0x00FF1400: // PGN 65300 "Manufacturer PGN 2"
|
||||||
|
// hier bekommen wir die Leistung des Motors angezeigt
|
||||||
|
Can_Read_Manu_PGN2(iMotorIndex, &frame);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Read PGN 65299
|
||||||
|
/// @param frame
|
||||||
|
void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Read PGN 65300
|
||||||
|
/// @param frame
|
||||||
|
void Can_Read_Manu_PGN2(int iMotorIndex, struct can_frame *frame)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@@ -49,4 +50,8 @@ void Can_SetMotorPower(int iMotorIndex, int iPower);
|
|||||||
void Can_TransmitMotorGear(int iMotorIndex);
|
void Can_TransmitMotorGear(int iMotorIndex);
|
||||||
void Can_TransmitMotorPower(int iMotorIndex);
|
void Can_TransmitMotorPower(int iMotorIndex);
|
||||||
|
|
||||||
|
void Can_ReadData(int iMotorIndex);
|
||||||
|
void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame);
|
||||||
|
void Can_Read_Manu_PGN2(int iMotorIndex, struct can_frame *frame);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
3
main.c
3
main.c
@@ -55,6 +55,9 @@ static void do_cyclic_1ms(struct period_info *pinfo)
|
|||||||
{
|
{
|
||||||
uint16_t nCalled = 0;
|
uint16_t nCalled = 0;
|
||||||
|
|
||||||
|
// read each cycle CAN data
|
||||||
|
Can_ReadData(0);
|
||||||
|
|
||||||
if ((pinfo->cyclecounter % 10) == 0)
|
if ((pinfo->cyclecounter % 10) == 0)
|
||||||
{
|
{
|
||||||
// called every 10ms
|
// called every 10ms
|
||||||
|
|||||||
Reference in New Issue
Block a user