C#/XL_Driver
6. 채널 정보 출력 2 ☆보완필요☆
소송왕잡스
2020. 2. 5. 17:53
이전 게시글에서 driverConfig.channel 배열에 접근하여 각 채널의 정보를 읽어보았다
각 채널마다 설정된 비트레이트, 샘플 레이트 등 추가적인 정보를 확인하는 방법을 알아보자
이전 게시글에서 보았던 xl_channel_config 클래스의 구조 중에서
public class xl_channel_config
{
... 생략
public xl_bus_params busParams;
... 생략
}
xl_bus_params 클래스를 멤버로 갖고 있음을 알 수 있는데, 위 클래스의 구조를 살펴보면 아래와 같다
public class xl_bus_params
{
public XLDefine.XL_BusTypes busType;
public xl_can_bus_params dataCan;
public xl_canFd_bus_params dataCanFd;
public xl_most_bus_params dataMost;
public xl_flexray_bus_params dataFr;
public xl_ethernet_bus_params dataEth;
public xl_a429_bus_params dataA429;
public xl_bus_params();
}
이들 중에서 xl_can_bus_params 클래스와 xl_canFd_bus_params 클래스를 우선 알아보자
xl_can_bus_params
public class xl_can_bus_params
{
public uint bitrate;
public byte sjw;
public byte tseg1;
public byte tseg2;
public byte sam;
public byte outputMode;
public byte[] reserved;
public byte canOpMode;
public xl_can_bus_params();
}
xl_canFd_bus_params
public class xl_canFd_bus_params
{
public uint arbitrationBitRate;
public byte sjwAbr;
public byte tseg1Abr;
public byte tseg2Abr;
public byte samAbr;
public byte outputMode;
public byte sjwDbr;
public byte tseg1Dbr;
public byte tseg2Dbr;
public uint dataBitRate;
public byte canOpMode;
public xl_canFd_bus_params();
}
조금의 차이는 있지만 공통적으로 tseg 변수와 outputMode, bitRate 변수들이 존재함을 알 수 있다.
추가적인 호출 메소드들이 있는지 알아봐야 하겠지만 우선 5번 게시글의 예제처럼 각 멤버 변수에
접근하여 출력 및 수정할 수 있다.
아래의 코드 예제를 확인해보자
코드 예제
using System;
using System.Threading;
using Microsoft.Win32.SafeHandles;
using vxlapi_NET;
namespace XLpractice
{
class Class1
{
// Driver access through XLDriver (wrapper)
private static XLDriver CANDemo = new XLDriver();
// Driver configuration
private static XLClass.xl_driver_config driverConfig = new XLClass.xl_driver_config();
private static String appName = "xlCANdemoNET";
// Variables required by XLDriver
private static XLDefine.XL_HardwareType hwType = XLDefine.XL_HardwareType.XL_HWTYPE_NONE;
private static uint hwIndex = 0;
private static uint hwChannel = 0;
[STAThread]
static int Main(string[] args)
{
XLDefine.XL_Status status;
Console.WriteLine("-------------------------------------------------------------------");
Console.WriteLine(" xlCANdemo.NET C# V11.0 ");
Console.WriteLine("Copyright (c) 2019 by Vector Informatik GmbH. All rights reserved.");
Console.WriteLine("-------------------------------------------------------------------\n");
// print .NET wrapper version
Console.WriteLine("vxlapi_NET : " + typeof(XLDriver).Assembly.GetName().Version);
// Open XL Driver
status = CANDemo.XL_OpenDriver();
Console.WriteLine("Open Driver : " + status);
if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
// Get XL Driver configuration
status = CANDemo.XL_GetDriverConfig(ref driverConfig);
Console.WriteLine("Get Driver Config : " + status);
if (status != XLDefine.XL_Status.XL_SUCCESS) PrintFunctionError();
// Display all found channels
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);
Console.WriteLine(" - hwtype : " +
driverConfig.channel[i].hwType);
Console.WriteLine(" - hwIndex : " +
driverConfig.channel[i].hwIndex);
Console.WriteLine(" - hwChannel : " +
driverConfig.channel[i].hwChannel);
Console.WriteLine(" - Channel Index : " +
driverConfig.channel[i].channelIndex);
Console.WriteLine(" - tseg1 : " +
driverConfig.channel[i].busParams.dataCan.tseg1);
Console.WriteLine(" - tseg2 : " +
driverConfig.channel[i].busParams.dataCan.tseg2);
Console.WriteLine(" - bitrate : " +
driverConfig.channel[i].busParams.dataCan.bitrate);
}
return 0;
}
private static int PrintFunctionError()
{
Console.WriteLine("\nERROR: Function call failed!\nPress any key to continue...");
Console.ReadKey();
return -1;
}
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(); //벡터 하드웨어 configuration tool 여는 메소드
Console.ReadKey();
}
}
}
출력 결과
-------------------------------------------------------------------
xlCANdemo.NET C# V11.0
Copyright (c) 2019 by Vector Informatik GmbH. All rights reserved.
-------------------------------------------------------------------
vxlapi_NET : 11.0.14.22309
Open Driver : XL_SUCCESS
Get Driver Config : XL_SUCCESS
[0] VN1640A Channel 1
- Channel Mask : 1
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 0
- Channel Index : 0
- tseg1 : 4
- tseg2 : 3
- bitrate : 500000
[1] VN1640A Channel 2
- Channel Mask : 2
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 1
- Channel Index : 1
- tseg1 : 4
- tseg2 : 3
- bitrate : 500000
[2] VN1640A Channel 3
- Channel Mask : 4
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 2
- Channel Index : 2
- tseg1 : 4
- tseg2 : 3
- bitrate : 500000
[3] VN1640A Channel 4
- Channel Mask : 8
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 3
- Channel Index : 3
- tseg1 : 4
- tseg2 : 3
- bitrate : 500000
[4] VN1640A Channel 5
- Channel Mask : 16
- Transceiver Name: On board D/A IO 1021
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 4
- Channel Index : 4
- tseg1 : 0
- tseg2 : 0
- bitrate : 0
[5] Virtual Channel 1
- Channel Mask : 32
- Transceiver Name: Virtual CAN
- Serial Number : 0
- hwtype : XL_HWTYPE_VIRTUAL
- hwIndex : 0
- hwChannel : 0
- Channel Index : 5
- tseg1 : 4
- tseg2 : 3
- bitrate : 500000
[6] Virtual Channel 2
- Channel Mask : 64
- Transceiver Name: Virtual CAN
- Serial Number : 0
- hwtype : XL_HWTYPE_VIRTUAL
- hwIndex : 0
- hwChannel : 1
- Channel Index : 6
- tseg1 : 4
- tseg2 : 3
- bitrate : 500000
계속하려면 아무 키나 누르십시오 . . .
tseg 값들과 bitrate가 출력된 것을 확인할 수 있습니다.
tseg의 값을 직접 접근하여 바꿔보는 것을 시도해보겠습니다.
멤버 변수에 직접 접근하여 tseg1의 값을 변경하려면 아래와같이 tseg1 = 100; 으로 바꿔주는 코드를 추가하고
// Display all found channels
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);
Console.WriteLine(" - hwtype : " +
driverConfig.channel[i].hwType);
Console.WriteLine(" - hwIndex : " +
driverConfig.channel[i].hwIndex);
Console.WriteLine(" - hwChannel : " +
driverConfig.channel[i].hwChannel);
Console.WriteLine(" - Channel Index : " +
driverConfig.channel[i].channelIndex);
driverConfig.channel[i].busParams.dataCan.tseg1 = 100;
Console.WriteLine(" - tseg1 : " +
driverConfig.channel[i].busParams.dataCan.tseg1);
Console.WriteLine(" - tseg2 : " +
driverConfig.channel[i].busParams.dataCan.tseg2);
Console.WriteLine(" - bitrate : " +
driverConfig.channel[i].busParams.dataCan.bitrate);
}
위 코드를 실행하면
-------------------------------------------------------------------
xlCANdemo.NET C# V11.0
Copyright (c) 2019 by Vector Informatik GmbH. All rights reserved.
-------------------------------------------------------------------
vxlapi_NET : 11.0.14.22309
Open Driver : XL_SUCCESS
Get Driver Config : XL_SUCCESS
[0] VN1640A Channel 1
- Channel Mask : 1
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 0
- Channel Index : 0
- tseg1 : 100
- tseg2 : 3
- bitrate : 500000
[1] VN1640A Channel 2
- Channel Mask : 2
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 1
- Channel Index : 1
- tseg1 : 100
- tseg2 : 3
- bitrate : 500000
[2] VN1640A Channel 3
- Channel Mask : 4
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 2
- Channel Index : 2
- tseg1 : 100
- tseg2 : 3
- bitrate : 500000
[3] VN1640A Channel 4
- Channel Mask : 8
- Transceiver Name: CANpiggy 1057Gcap (Highspeed)
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 3
- Channel Index : 3
- tseg1 : 100
- tseg2 : 3
- bitrate : 500000
[4] VN1640A Channel 5
- Channel Mask : 16
- Transceiver Name: On board D/A IO 1021
- Serial Number : 26638
- hwtype : XL_HWTYPE_VN1640
- hwIndex : 0
- hwChannel : 4
- Channel Index : 4
- tseg1 : 100
- tseg2 : 0
- bitrate : 0
[5] Virtual Channel 1
- Channel Mask : 32
- Transceiver Name: Virtual CAN
- Serial Number : 0
- hwtype : XL_HWTYPE_VIRTUAL
- hwIndex : 0
- hwChannel : 0
- Channel Index : 5
- tseg1 : 100
- tseg2 : 3
- bitrate : 500000
[6] Virtual Channel 2
- Channel Mask : 64
- Transceiver Name: Virtual CAN
- Serial Number : 0
- hwtype : XL_HWTYPE_VIRTUAL
- hwIndex : 0
- hwChannel : 1
- Channel Index : 6
- tseg1 : 100
- tseg2 : 3
- bitrate : 500000
계속하려면 아무 키나 누르십시오 . . .
tseg1에 해당하는 값은 바뀌었지만 bitrate 계산에는 영향을 끼치지 않은 것을 볼 수 있습니다.