Created
May 7, 2021 21:04
-
-
Save mac-can/8fea17c5e8398478a2e065dd37fe5f6f to your computer and use it in GitHub Desktop.
macOS Library for PCAN-USB Interfaces: Blocking Read Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// macOS Library for PCAN-USB Interfaces | |
// Example: receive CAN messages (Blockng read) | |
// | |
// To compile type `clang++ pcbusb_recv.cpp -l PCBUSB -o pcbusb_recv' | |
// libPCBUSB.dylib is expected to be in the library search path. | |
// | |
// Note: Press Ctrl+C to terminate the program. | |
// | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
#include <errno.h> | |
#include <sys/time.h> | |
#include "PCBUSB.h" | |
#define PCAN_CHANNEL PCAN_USBBUS1 | |
#define PCAN_BAUDRATE PCAN_BAUD_250K | |
static volatile int running = 1; | |
static void sigterm(int signo) | |
{ | |
//printf("got signal %d\n", signo); | |
running = 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int fdes; | |
fd_set rdfs; | |
TPCANMsg message; | |
TPCANStatus status; | |
if ((signal(SIGTERM, sigterm) == SIG_ERR) || | |
(signal(SIGINT, sigterm) == SIG_ERR)) { | |
perror("Error"); | |
return errno; | |
} | |
status = CAN_Initialize(PCAN_CHANNEL, PCAN_BAUDRATE, 0, 0, 0); | |
printf("Initialize CAN: 0x%x\n", status); | |
if (status != PCAN_ERROR_OK) goto leave; | |
// get a file descriptor for blocking read via `select' | |
status = CAN_GetValue(PCAN_CHANNEL, PCAN_RECEIVE_EVENT, &fdes, sizeof(int)); | |
if (status != PCAN_ERROR_OK) goto leave; | |
// see `man select' for details on I/O descriptor sets | |
FD_ZERO(&rdfs); | |
FD_SET(fdes, &rdfs); | |
while (running) { | |
status = CAN_Read(PCAN_CHANNEL, &message, NULL); | |
if (status == PCAN_ERROR_OK) { | |
if (!(message.MSGTYPE & PCAN_MESSAGE_STATUS)) | |
printf(" - R ID:%4x LEN:%1x DATA:%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
(int) message.ID, (int) message.LEN, (int) message.DATA[0], | |
(int) message.DATA[1], (int) message.DATA[2], | |
(int) message.DATA[3], (int) message.DATA[4], | |
(int) message.DATA[5], (int) message.DATA[6], | |
(int) message.DATA[7]); | |
} else if (status == PCAN_ERROR_QRCVEMPTY) { | |
// block until at least one CAN message has been received | |
if (select(fdes+1, &rdfs, NULL, NULL, NULL) < 0) | |
perror("Error"); | |
} else { | |
printf("Error 0x%x\n", status); | |
break; | |
} | |
} | |
leave: | |
CAN_Uninitialize(PCAN_NONEBUS); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// macOS Library for PCAN-USB Interfaces | |
// Example: send CAN messages | |
// | |
// To compile type `clang++ pcbusb_send.cpp -l PCBUSB -o pcbusb_send' | |
// libPCBUSB.dylib is expected to be in the library search path. | |
// | |
// Note: Press Ctrl+C to terminate the program. | |
// | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
#include <errno.h> | |
#include <string.h> | |
#include "PCBUSB.h" | |
#define PCAN_CHANNEL PCAN_USBBUS2 | |
#define PCAN_BAUDRATE PCAN_BAUD_250K | |
static volatile int running = 1; | |
static void sigterm(int signo) | |
{ | |
//printf("got signal %d\n", signo); | |
running = 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
TPCANMsg message; | |
TPCANStatus status; | |
unsigned long frames = 0; | |
if ((signal(SIGTERM, sigterm) == SIG_ERR) || | |
(signal(SIGINT, sigterm) == SIG_ERR)) { | |
perror("+++ Error"); | |
return errno; | |
} | |
status = CAN_Initialize(PCAN_CHANNEL, PCAN_BAUDRATE, 0, 0, 0); | |
printf("Initialize CAN: 0x%x\n", status); | |
if (status != PCAN_ERROR_OK) goto leave; | |
message.ID = 0x100; | |
message.LEN = 8; | |
message.MSGTYPE = PCAN_MESSAGE_STANDARD; | |
memset(message.DATA, 0x00, message.LEN); | |
while (running) { | |
if ((status = CAN_Write(PCAN_CHANNEL, &message)) == PCAN_ERROR_OK) { | |
frames++; | |
// if ((frames % 2048) == 0) | |
// printf(" - T Message %lu\n", frames); | |
message.DATA[0] = (BYTE)((frames & 0x000000FFU) >> 0); | |
message.DATA[1] = (BYTE)((frames & 0x0000FF00U) >> 8); | |
message.DATA[2] = (BYTE)((frames & 0x00FF0000U) >> 16); | |
message.DATA[3] = (BYTE)((frames & 0xFF000000U) >> 24); | |
} | |
} | |
leave: | |
status = CAN_Uninitialize(PCAN_NONEBUS); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment