1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
using System; using System.Threading; using Microsoft.Win32.SafeHandles; using vxlapi_NET;
namespace xlCANDemo { class Class1 { private static XLDriver CANDemo = new XLDriver(); private static String appName = "xlCANdemoNET";
private static XLClass.xl_driver_config driverConfig = new XLClass.xl_driver_config();
private static XLDefine.XL_HardwareType hwType = XLDefine.XL_HardwareType.XL_HWTYPE_NONE; private static uint hwIndex = 0; private static uint hwChannel = 0; private static int portHandle = -1; private static UInt64 accessMask = 0; private static UInt64 permissionMask = 0; private static UInt64 txMask = 0; private static UInt64 rxMask = 0; private static int txCi = -1; private static int rxCi = -1; private static EventWaitHandle xlEvWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, null);
private static Thread rxThread; private static bool blockRxThread = false;
[STAThread] static int Main(string[] args) { XLDefine.XL_Status status;
Console.WriteLine("-------------------------------------------------------------------"); Console.WriteLine(" xlCANdemo.NET C# V11.6 "); Console.WriteLine("Copyright (c) 2020 by Vector Informatik GmbH. All rights reserved."); Console.WriteLine("-------------------------------------------------------------------\n");
Console.WriteLine("vxlapi_NET : " + typeof(XLDriver).Assembly.GetName().Version);
status = CANDemo.XL_OpenDriver(); Console.WriteLine("Open Driver : " + status); if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
status = CANDemo.XL_GetDriverConfig(ref driverConfig); Console.WriteLine("Get Driver Config : " + status); if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
Console.WriteLine("DLL Version : " + CANDemo.VersionToString(driverConfig.dllVersion));
Console.WriteLine("Channels found : " + driverConfig.channelCount);
for (int i = 0; i < driverConfig.channelCount; i++) { Console.WriteLine("\n [{0}] " + driverConfig.channel[i].name, i); Console.WriteLine(" - Channel Mask : " + driverConfig.channel[i].channelMask); Console.WriteLine(" - Transceiver Name: " + driverConfig.channel[i].transceiverName); Console.WriteLine(" - Serial Number : " + driverConfig.channel[i].serialNumber); }
if ((CANDemo.XL_GetApplConfig(appName, 0, ref hwType, ref hwIndex, ref hwChannel, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN) != XLDefine.XL_Status.XL_SUCCESS) || (CANDemo.XL_GetApplConfig(appName, 1, ref hwType, ref hwIndex, ref hwChannel, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN) != XLDefine.XL_Status.XL_SUCCESS)) { CANDemo.XL_SetApplConfig(appName, 0, XLDefine.XL_HardwareType.XL_HWTYPE_NONE, 0, 0, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN); CANDemo.XL_SetApplConfig(appName, 1, XLDefine.XL_HardwareType.XL_HWTYPE_NONE, 0, 0, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN); PrintAssignErrorAndPopupHwConf(); }
while (!GetAppChannelAndTestIsOk(0, ref txMask, ref txCi) || !GetAppChannelAndTestIsOk(1, ref rxMask, ref rxCi)) { PrintAssignErrorAndPopupHwConf(); }
PrintConfig();
accessMask = txMask | rxMask; permissionMask = accessMask;
status = CANDemo.XL_OpenPort(ref portHandle, appName, accessMask, ref permissionMask, 1024, XLDefine.XL_InterfaceVersion.XL_INTERFACE_VERSION, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN); Console.WriteLine("\n\nOpen Port : " + status); if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
status = CANDemo.XL_CanRequestChipState(portHandle, accessMask); Console.WriteLine("Can Request Chip State: " + status); if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
status = CANDemo.XL_ActivateChannel(portHandle, accessMask, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN, XLDefine.XL_AC_Flags.XL_ACTIVATE_NONE); Console.WriteLine("Activate Channel : " + status); if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
int tempInt = -1; status = CANDemo.XL_SetNotification(portHandle, ref tempInt, 1); xlEvWaitHandle.SafeWaitHandle = new SafeWaitHandle(new IntPtr(tempInt), true);
Console.WriteLine("Set Notification : " + status); if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
status = CANDemo.XL_ResetClock(portHandle); Console.WriteLine("Reset Clock : " + status + "\n\n"); if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
Console.WriteLine("Start Rx thread..."); rxThread = new Thread(new ThreadStart(RXThread)); rxThread.Start();
Console.WriteLine("Press <ENTER> to transmit CAN messages \n <b>, <ENTER> to block Rx thread for rx-overrun-test \n <B>, <ENTER> burst of CAN TX messages \n <x>, <ENTER> to exit");
while (true) { if (blockRxThread) Console.WriteLine("Rx thread blocked.");
string str = Console.ReadLine(); if (str == "b") blockRxThread = !blockRxThread; else if (str == "B") { for (int i = 0; i < 1000; i++) { CANTransmitDemo(); } } else if (str == "x") break; else { CANTransmitDemo(); } }
rxThread.Abort(); Console.WriteLine("Close Port : " + CANDemo.XL_ClosePort(portHandle)); Console.WriteLine("Close Driver : " + CANDemo.XL_CloseDriver());
return 0; }
private static int PrintFunctionError() { Console.WriteLine("\nERROR: Function call failed!\nPress any key to continue..."); Console.ReadKey(); return -1; }
private static void PrintConfig() { Console.WriteLine("\n\nAPPLICATION CONFIGURATION");
foreach (int channelIndex in new int[] {txCi, rxCi}) { Console.WriteLine("-------------------------------------------------------------------"); Console.WriteLine("Configured Hardware Channel : " + driverConfig.channel[channelIndex].name); Console.WriteLine("Hardware Driver Version : " + CANDemo.VersionToString(driverConfig.channel[channelIndex].driverVersion)); Console.WriteLine("Used Transceiver : " + driverConfig.channel[channelIndex].transceiverName); }
Console.WriteLine("-------------------------------------------------------------------\n"); }
private static void PrintAssignErrorAndPopupHwConf() { Console.WriteLine("\nPlease check application settings of \"" + appName + " CAN1/CAN2\",\nassign them to available hardware channels and press enter."); CANDemo.XL_PopupHwConfig(); Console.ReadKey(); }
private static bool GetAppChannelAndTestIsOk(uint appChIdx, ref UInt64 chMask, ref int chIdx) { XLDefine.XL_Status status = CANDemo.XL_GetApplConfig(appName, appChIdx, ref hwType, ref hwIndex, ref hwChannel, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN); if (status != XLDefine.XL_Status.XL_SUCCESS) { Console.WriteLine("XL_GetApplConfig : " + status); PrintFunctionError(); }
chMask = CANDemo.XL_GetChannelMask(hwType, (int)hwIndex, (int)hwChannel); chIdx = CANDemo.XL_GetChannelIndex(hwType, (int)hwIndex, (int)hwChannel); if (chIdx < 0 || chIdx >= driverConfig.channelCount) { return false; }
return (driverConfig.channel[chIdx].channelBusCapabilities & XLDefine.XL_BusCapabilities.XL_BUS_ACTIVE_CAP_CAN) != 0; }
public static void CANTransmitDemo() { XLDefine.XL_Status txStatus;
XLClass.xl_event_collection xlEventCollection = new XLClass.xl_event_collection(2);
xlEventCollection.xlEvent[0].tagData.can_Msg.id = 0x100; xlEventCollection.xlEvent[0].tagData.can_Msg.dlc = 8; xlEventCollection.xlEvent[0].tagData.can_Msg.data[0] = 1; xlEventCollection.xlEvent[0].tagData.can_Msg.data[1] = 2; xlEventCollection.xlEvent[0].tagData.can_Msg.data[2] = 3; xlEventCollection.xlEvent[0].tagData.can_Msg.data[3] = 4; xlEventCollection.xlEvent[0].tagData.can_Msg.data[4] = 5; xlEventCollection.xlEvent[0].tagData.can_Msg.data[5] = 6; xlEventCollection.xlEvent[0].tagData.can_Msg.data[6] = 7; xlEventCollection.xlEvent[0].tagData.can_Msg.data[7] = 8; xlEventCollection.xlEvent[0].tag = XLDefine.XL_EventTags.XL_TRANSMIT_MSG;
xlEventCollection.xlEvent[1].tagData.can_Msg.id = 0x200; xlEventCollection.xlEvent[1].tagData.can_Msg.dlc = 8; xlEventCollection.xlEvent[1].tagData.can_Msg.data[0] = 9; xlEventCollection.xlEvent[1].tagData.can_Msg.data[1] = 10; xlEventCollection.xlEvent[1].tagData.can_Msg.data[2] = 11; xlEventCollection.xlEvent[1].tagData.can_Msg.data[3] = 12; xlEventCollection.xlEvent[1].tagData.can_Msg.data[4] = 13; xlEventCollection.xlEvent[1].tagData.can_Msg.data[5] = 14; xlEventCollection.xlEvent[1].tagData.can_Msg.data[6] = 15; xlEventCollection.xlEvent[1].tagData.can_Msg.data[7] = 16; xlEventCollection.xlEvent[1].tag = XLDefine.XL_EventTags.XL_TRANSMIT_MSG;
txStatus = CANDemo.XL_CanTransmit(portHandle, txMask, xlEventCollection); Console.WriteLine("Transmit Message : " + txStatus); }
public static void RXThread() { XLClass.xl_event receivedEvent = new XLClass.xl_event();
XLDefine.XL_Status xlStatus = XLDefine.XL_Status.XL_SUCCESS;
while (true) { if (xlEvWaitHandle.WaitOne(1000)) { xlStatus = XLDefine.XL_Status.XL_SUCCESS;
while (xlStatus != XLDefine.XL_Status.XL_ERR_QUEUE_IS_EMPTY) { while (blockRxThread) { Thread.Sleep(1000); }
xlStatus = CANDemo.XL_Receive(portHandle, ref receivedEvent);
if (xlStatus == XLDefine.XL_Status.XL_SUCCESS) { if ((receivedEvent.flags & XLDefine.XL_MessageFlags.XL_EVENT_FLAG_OVERRUN) != 0) { Console.WriteLine("-- XL_EVENT_FLAG_OVERRUN --"); }
if (receivedEvent.tag == XLDefine.XL_EventTags.XL_RECEIVE_MSG) { if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_OVERRUN) != 0) { Console.WriteLine("-- XL_CAN_MSG_FLAG_OVERRUN --"); }
if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME) == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME) { Console.WriteLine("ERROR FRAME"); }
else if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME) == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME) { Console.WriteLine("REMOTE FRAME"); }
else { Console.WriteLine(CANDemo.XL_GetEventString(receivedEvent)); } } } } } } } } }
|