AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
sys_tec_windows_plugin.cpp
Go to the documentation of this file.
1//================================================================================================
10//================================================================================================
11
14
15namespace isobus
16{
17 SysTecWindowsPlugin::SysTecWindowsPlugin(std::uint8_t channel, std::uint32_t baudrate) :
18 baudrateConstant(baudrate),
19 channelIndex(channel)
20 {
21 if (channel > USBCAN_CHANNEL_CH1)
22 {
23 LOG_CRITICAL("[SYSTEC]: Invalid channel");
24 }
25 }
26
27 SysTecWindowsPlugin::SysTecWindowsPlugin(std::uint32_t serialNumber, std::uint32_t baudrate) :
28 serialNumber(serialNumber),
29 baudrateConstant(baudrate)
30 {
31 }
32
37
39 {
40 return (openResult && (USBCAN_INVALID_HANDLE != handle));
41 }
42
44 {
45 if (get_is_valid())
46 {
47 openResult = false;
48 UcanDeinitCan(handle);
49 UcanDeinitHardware(handle);
50 handle = USBCAN_INVALID_HANDLE;
51 }
52 }
53
55 {
56 if (!get_is_valid())
57 {
58 if (0 == serialNumber)
59 {
60 openResult = (USBCAN_SUCCESSFUL == UcanInitHardwareEx(&handle, channelIndex, NULL, this));
61 }
62 else
63 {
64 openResult = (USBCAN_SUCCESSFUL == UcanInitHardwareEx2(&handle, serialNumber, NULL, this));
65 }
66
67 if (openResult && (USBCAN_INVALID_HANDLE != handle))
68 {
69 tUcanInitCanParam initCANParameters = { 0 };
70 tUcanChannelInfo channelInfo = { 0 };
71 channelInfo.m_dwSize = sizeof(tUcanChannelInfo);
72
73 initCANParameters.m_dwSize = sizeof(tUcanInitCanParam);
74 initCANParameters.m_bMode = kUcanModeNormal;
75 initCANParameters.m_bBTR0 = ((baudrateConstant >> 8) & 0xFF);
76 initCANParameters.m_bBTR1 = (baudrateConstant & 0xFF);
77 initCANParameters.m_bOCR = 0x1A;
78 initCANParameters.m_dwAMR = USBCAN_AMR_ALL;
79 initCANParameters.m_dwACR = USBCAN_ACR_ALL;
80 initCANParameters.m_dwBaudrate = USBCAN_BAUDEX_USE_BTR01;
81 initCANParameters.m_wNrOfRxBufferEntries = USBCAN_DEFAULT_BUFFER_ENTRIES;
82 initCANParameters.m_wNrOfTxBufferEntries = USBCAN_DEFAULT_BUFFER_ENTRIES;
83
84 openResult = (USBCAN_SUCCESSFUL == UcanInitCanEx2(this->handle, channelIndex, &initCANParameters));
85
86 if (!openResult)
87 {
88 LOG_CRITICAL("[SYSTEC]: Error trying to configure a SYS TEC probe channel");
89 }
90 }
91 else
92 {
93 LOG_CRITICAL("[SYSTEC]: Error trying to connect to SYS TEC probe");
94 }
95 }
96 else
97 {
98 LOG_WARNING("[SYSTEC]: CAN Adapter already initialized.");
99 }
100 }
101
103 {
104 bool retVal = false;
105
106 if (get_is_valid())
107 {
108 tCanMsgStruct CANMessage = { 0 };
109
110 retVal = (USBCAN_SUCCESSFUL == UcanReadCanMsgEx(handle, &channelIndex, &CANMessage, NULL));
111
112 if (retVal)
113 {
114 canFrame.dataLength = CANMessage.m_bDLC;
115 canFrame.identifier = CANMessage.m_dwID;
116 canFrame.isExtendedFrame = (USBCAN_MSG_FF_EXT == CANMessage.m_bFF);
117 canFrame.data[0] = CANMessage.m_bData[0];
118 canFrame.data[1] = CANMessage.m_bData[1];
119 canFrame.data[2] = CANMessage.m_bData[2];
120 canFrame.data[3] = CANMessage.m_bData[3];
121 canFrame.data[4] = CANMessage.m_bData[4];
122 canFrame.data[5] = CANMessage.m_bData[5];
123 canFrame.data[6] = CANMessage.m_bData[6];
124 canFrame.data[7] = CANMessage.m_bData[7];
125 }
126 else
127 {
130 std::this_thread::sleep_for(std::chrono::milliseconds(1)); // This is long enough for 2 messages to be received at max bus load, which should be fine.
131 }
132 }
133 return retVal;
134 }
135
137 {
138 bool retVal = false;
139
140 if (get_is_valid())
141 {
142 tCanMsgStruct CANMessage;
143
144 CANMessage.m_dwID = canFrame.identifier;
145 CANMessage.m_bDLC = canFrame.dataLength;
146 CANMessage.m_bFF = USBCAN_MSG_FF_EXT;
147 CANMessage.m_bData[0] = canFrame.data[0];
148 CANMessage.m_bData[1] = canFrame.data[1];
149 CANMessage.m_bData[2] = canFrame.data[2];
150 CANMessage.m_bData[3] = canFrame.data[3];
151 CANMessage.m_bData[4] = canFrame.data[4];
152 CANMessage.m_bData[5] = canFrame.data[5];
153 CANMessage.m_bData[6] = canFrame.data[6];
154 CANMessage.m_bData[7] = canFrame.data[7];
155
156 retVal = (USBCAN_SUCCESSFUL == UcanWriteCanMsgEx(handle, channelIndex, &CANMessage, nullptr));
157 }
158 return retVal;
159 }
160}
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.
bool isExtendedFrame
Denotes if the frame is extended format.
std::uint8_t data[8]
The data payload of the frame.
A class that represents a generic CAN message of arbitrary length.
SysTecWindowsPlugin(std::uint8_t channel=USBCAN_CHANNEL_CH0, std::uint32_t baudrate=USBCAN_BAUD_250kBit)
Constructor for the Windows SYS TEC plugin.
virtual ~SysTecWindowsPlugin()
The destructor for PCANBasicWindowsPlugin.
std::uint32_t serialNumber
The serial number to connect to, or 0 if not used.
std::uint16_t baudrateConstant
The constant used to configure the adapter's baudrate.
bool write_frame(const isobus::CANMessageFrame &canFrame) override
Writes a frame to the bus (synchronous)
void open() override
Connects to the hardware you specified in the constructor's channel argument.
std::uint8_t handle
The handle for the device, used to interact with the DLL.
bool read_frame(isobus::CANMessageFrame &canFrame) override
Returns a frame from the hardware (synchronous), or false if no frame can be read.
bool openResult
Stores the result of the call to begin CAN communication. Used for is_valid check later.
void close() override
Closes the connection to the hardware.
std::uint8_t channelIndex
The channel for the device, used if you have a multi-channel device.
bool get_is_valid() const override
Returns if the connection with the hardware is valid.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
An interface for using a SYS TEC sysWORXX USB CAN device.