AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
pcan_basic_windows_plugin.cpp
Go to the documentation of this file.
1//================================================================================================
10//================================================================================================
11
14
15#include <thread>
16
17namespace isobus
18{
20 handle(channel),
21 openResult(PCAN_ERROR_OK)
22 {
23 }
24
28
30 {
31 return (PCAN_ERROR_OK == openResult);
32 }
33
35 {
36 CAN_Uninitialize(handle);
37 }
38
40 {
41 openResult = CAN_Initialize(handle, PCAN_BAUD_250K);
42
43 if (PCAN_ERROR_OK != openResult)
44 {
45 LOG_CRITICAL("[PCAN]: Error trying to connect to PCAN probe");
46 }
47 }
48
50 {
51 TPCANStatus result;
52 TPCANMsg CANMsg;
53 TPCANTimestamp CANTimeStamp;
54 bool retVal = false;
55
56 result = CAN_Read(handle, &CANMsg, &CANTimeStamp);
57
58 if (PCAN_ERROR_OK == result)
59 {
60 canFrame.dataLength = CANMsg.LEN;
61 memcpy(canFrame.data, CANMsg.DATA, CANMsg.LEN);
62 canFrame.identifier = CANMsg.ID;
63 canFrame.isExtendedFrame = (PCAN_MESSAGE_EXTENDED == CANMsg.MSGTYPE);
64 canFrame.timestamp_us = (CANTimeStamp.millis * 1000) + CANTimeStamp.micros;
65 retVal = true;
66 }
67 else
68 {
69 std::this_thread::sleep_for(std::chrono::milliseconds(1));
70 }
71 return retVal;
72 }
73
75 {
76 TPCANStatus result;
77 TPCANMsg msgCanMessage;
78
79 msgCanMessage.ID = canFrame.identifier;
80 msgCanMessage.LEN = canFrame.dataLength;
81 msgCanMessage.MSGTYPE = canFrame.isExtendedFrame ? PCAN_MESSAGE_EXTENDED : PCAN_MESSAGE_STANDARD;
82 memcpy(msgCanMessage.DATA, canFrame.data, canFrame.dataLength);
83
84 result = CAN_Write(handle, &msgCanMessage);
85
86 return (PCAN_ERROR_OK == result);
87 }
88}
A class that acts as a logging sink. The intent is that someone could make their own derived class of...
A CAN frame for interfacing with a hardware layer, like socket CAN or other interface.
std::uint8_t dataLength
The length of the data used in the frame.
std::uint32_t identifier
The 32 bit identifier of the frame.
std::uint64_t timestamp_us
A microsecond timestamp.
bool isExtendedFrame
Denotes if the frame is extended format.
std::uint8_t data[8]
The data payload of the frame.
TPCANStatus openResult
Stores the result of the call to begin CAN communication. Used for is_valid check later.
TPCANHandle handle
The handle as defined in the PCAN driver API.
void open() override
Connects to the hardware you specified in the constructor's channel argument.
void close() override
Closes the connection to the hardware.
bool write_frame(const isobus::CANMessageFrame &canFrame) override
Writes a frame to the bus (synchronous)
bool get_is_valid() const override
Returns if the connection with the hardware is valid.
bool read_frame(isobus::CANMessageFrame &canFrame) override
Returns a frame from the hardware (synchronous), or false if no frame can be read.
PCANBasicWindowsPlugin(WORD channel)
Constructor for the Windows version of the PEAK PCAN Basic CAN driver.
virtual ~PCANBasicWindowsPlugin()
The destructor for PCANBasicWindowsPlugin.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
An interface for using a PEAK PCAN device.