#pragma once


namespace SendRecv {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	using namespace pisocanfd_net;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

		static bool SelectedCardOpen = false;
        static unsigned char SelectedBoardNo = 0;
		static unsigned char SelectedCardPortNo = 0;
        
		//Data Length Code to Number of CAN data bytes 
		static array< unsigned char >^ CANFD_DLC2DBN = gcnew array< unsigned char >(16){ 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64 };
        

		static unsigned char CANPort_P1 = 1;

	public: 

	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::TextBox^  txtDSamplePoint;
	private: System::Windows::Forms::Label^  label18;
	private: System::Windows::Forms::TextBox^  txtDBaudrate;
	private: System::Windows::Forms::Label^  label19;
	private: System::Windows::Forms::Label^  label20;
	private: System::Windows::Forms::Label^  label21;
	private: System::Windows::Forms::TextBox^  txtASamplePoint;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::TextBox^  txtABaudrate;
	private: System::Windows::Forms::Label^  label8;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Label^  label7;
			 static unsigned char CANPort_P2 = 2;

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  btnSend;
	protected: 
	private: System::Windows::Forms::Label^  label17;
	private: System::Windows::Forms::ComboBox^  cboSendMsg_Port;
	private: System::Windows::Forms::GroupBox^  grpScanDevice;
	private: System::Windows::Forms::Button^  btnActive;
	private: System::Windows::Forms::Button^  btnScan;
	private: System::Windows::Forms::ComboBox^  cboModuleName;
	private: System::Windows::Forms::TextBox^  txtSendMsg_ID;

	private: System::Windows::Forms::GroupBox^  grpReceiveMessage;
	private: System::Windows::Forms::ListBox^  lstReceiveMessage;
	private: System::Windows::Forms::Button^  btnClear;
	private: System::Windows::Forms::TextBox^  txtSendMsg_Data;

	private: System::Windows::Forms::ComboBox^  cboSendMsg_DLC;
	private: System::Windows::Forms::ComboBox^  cboSendMsg_Type;

	private: System::Windows::Forms::GroupBox^  grpSendMessage;
	private: System::Windows::Forms::ComboBox^  cboSendMsg_Mode;

	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label13;
	private: System::Windows::Forms::Label^  label14;
	private: System::Windows::Forms::Label^  label15;
	private: System::Windows::Forms::Label^  label16;
	private: System::Windows::Forms::Timer^  tmrRxMsg;
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->btnSend = (gcnew System::Windows::Forms::Button());
			this->label17 = (gcnew System::Windows::Forms::Label());
			this->cboSendMsg_Port = (gcnew System::Windows::Forms::ComboBox());
			this->grpScanDevice = (gcnew System::Windows::Forms::GroupBox());
			this->btnActive = (gcnew System::Windows::Forms::Button());
			this->btnScan = (gcnew System::Windows::Forms::Button());
			this->cboModuleName = (gcnew System::Windows::Forms::ComboBox());
			this->txtSendMsg_ID = (gcnew System::Windows::Forms::TextBox());
			this->grpReceiveMessage = (gcnew System::Windows::Forms::GroupBox());
			this->lstReceiveMessage = (gcnew System::Windows::Forms::ListBox());
			this->btnClear = (gcnew System::Windows::Forms::Button());
			this->txtSendMsg_Data = (gcnew System::Windows::Forms::TextBox());
			this->cboSendMsg_DLC = (gcnew System::Windows::Forms::ComboBox());
			this->cboSendMsg_Type = (gcnew System::Windows::Forms::ComboBox());
			this->grpSendMessage = (gcnew System::Windows::Forms::GroupBox());
			this->cboSendMsg_Mode = (gcnew System::Windows::Forms::ComboBox());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label13 = (gcnew System::Windows::Forms::Label());
			this->label14 = (gcnew System::Windows::Forms::Label());
			this->label15 = (gcnew System::Windows::Forms::Label());
			this->label16 = (gcnew System::Windows::Forms::Label());
			this->tmrRxMsg = (gcnew System::Windows::Forms::Timer(this->components));
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->txtDSamplePoint = (gcnew System::Windows::Forms::TextBox());
			this->label18 = (gcnew System::Windows::Forms::Label());
			this->txtDBaudrate = (gcnew System::Windows::Forms::TextBox());
			this->label19 = (gcnew System::Windows::Forms::Label());
			this->label20 = (gcnew System::Windows::Forms::Label());
			this->label21 = (gcnew System::Windows::Forms::Label());
			this->txtASamplePoint = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->txtABaudrate = (gcnew System::Windows::Forms::TextBox());
			this->label8 = (gcnew System::Windows::Forms::Label());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->grpScanDevice->SuspendLayout();
			this->grpReceiveMessage->SuspendLayout();
			this->grpSendMessage->SuspendLayout();
			this->groupBox1->SuspendLayout();
			this->SuspendLayout();
			// 
			// btnSend
			// 
			this->btnSend->Location = System::Drawing::Point(384, 64);
			this->btnSend->Name = L"btnSend";
			this->btnSend->Size = System::Drawing::Size(184, 23);
			this->btnSend->TabIndex = 51;
			this->btnSend->Text = L"Send CAN Message";
			this->btnSend->UseVisualStyleBackColor = true;
			this->btnSend->Click += gcnew System::EventHandler(this, &Form1::btnSend_Click);
			// 
			// label17
			// 
			this->label17->AutoSize = true;
			this->label17->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label17->Location = System::Drawing::Point(24, 68);
			this->label17->Name = L"label17";
			this->label17->Size = System::Drawing::Size(65, 16);
			this->label17->TabIndex = 50;
			this->label17->Text = L"CAN Port";
			// 
			// cboSendMsg_Port
			// 
			this->cboSendMsg_Port->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->cboSendMsg_Port->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->cboSendMsg_Port->FormattingEnabled = true;
			this->cboSendMsg_Port->Items->AddRange(gcnew cli::array< System::Object^  >(2) {L"CAN1", L"CAN2"});
			this->cboSendMsg_Port->Location = System::Drawing::Point(96, 64);
			this->cboSendMsg_Port->Name = L"cboSendMsg_Port";
			this->cboSendMsg_Port->Size = System::Drawing::Size(72, 24);
			this->cboSendMsg_Port->TabIndex = 49;
			// 
			// grpScanDevice
			// 
			this->grpScanDevice->Controls->Add(this->btnActive);
			this->grpScanDevice->Controls->Add(this->btnScan);
			this->grpScanDevice->Controls->Add(this->cboModuleName);
			this->grpScanDevice->Font = (gcnew System::Drawing::Font(L"PMingLiU", 11.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->grpScanDevice->ForeColor = System::Drawing::Color::Black;
			this->grpScanDevice->Location = System::Drawing::Point(8, 8);
			this->grpScanDevice->Name = L"grpScanDevice";
			this->grpScanDevice->Size = System::Drawing::Size(224, 88);
			this->grpScanDevice->TabIndex = 3;
			this->grpScanDevice->TabStop = false;
			this->grpScanDevice->Text = L"Scan Device";
			// 
			// btnActive
			// 
			this->btnActive->Enabled = false;
			this->btnActive->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->btnActive->Location = System::Drawing::Point(152, 56);
			this->btnActive->Name = L"btnActive";
			this->btnActive->Size = System::Drawing::Size(64, 24);
			this->btnActive->TabIndex = 12;
			this->btnActive->Text = L"Active";
			this->btnActive->UseVisualStyleBackColor = true;
			this->btnActive->Click += gcnew System::EventHandler(this, &Form1::btnOpen_Click);
			// 
			// btnScan
			// 
			this->btnScan->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->btnScan->Location = System::Drawing::Point(8, 56);
			this->btnScan->Name = L"btnScan";
			this->btnScan->Size = System::Drawing::Size(72, 24);
			this->btnScan->TabIndex = 11;
			this->btnScan->Text = L"Scan";
			this->btnScan->UseVisualStyleBackColor = true;
			this->btnScan->Click += gcnew System::EventHandler(this, &Form1::btnScan_Click);
			// 
			// cboModuleName
			// 
			this->cboModuleName->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->cboModuleName->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->cboModuleName->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->cboModuleName->FormattingEnabled = true;
			this->cboModuleName->Location = System::Drawing::Point(8, 24);
			this->cboModuleName->Name = L"cboModuleName";
			this->cboModuleName->Size = System::Drawing::Size(208, 24);
			this->cboModuleName->TabIndex = 1;
			this->cboModuleName->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::cboModuleName_SelectedIndexChanged);
			// 
			// txtSendMsg_ID
			// 
			this->txtSendMsg_ID->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->txtSendMsg_ID->Location = System::Drawing::Point(82, 32);
			this->txtSendMsg_ID->MaxLength = 8;
			this->txtSendMsg_ID->Name = L"txtSendMsg_ID";
			this->txtSendMsg_ID->Size = System::Drawing::Size(80, 22);
			this->txtSendMsg_ID->TabIndex = 38;
			this->txtSendMsg_ID->Text = L"0";
			this->txtSendMsg_ID->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->txtSendMsg_ID->TextChanged += gcnew System::EventHandler(this, &Form1::txtSendMsg_ID_TextChanged);
			this->txtSendMsg_ID->Leave += gcnew System::EventHandler(this, &Form1::txtSendMsg_ID_Leave);
			this->txtSendMsg_ID->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::txtSendMsg_ID_KeyPress);
			// 
			// grpReceiveMessage
			// 
			this->grpReceiveMessage->Controls->Add(this->lstReceiveMessage);
			this->grpReceiveMessage->Controls->Add(this->btnClear);
			this->grpReceiveMessage->Enabled = false;
			this->grpReceiveMessage->Font = (gcnew System::Drawing::Font(L"PMingLiU", 11.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->grpReceiveMessage->ForeColor = System::Drawing::Color::Black;
			this->grpReceiveMessage->Location = System::Drawing::Point(8, 208);
			this->grpReceiveMessage->Name = L"grpReceiveMessage";
			this->grpReceiveMessage->Size = System::Drawing::Size(584, 240);
			this->grpReceiveMessage->TabIndex = 5;
			this->grpReceiveMessage->TabStop = false;
			this->grpReceiveMessage->Text = L"CAN1/2 Receive Message";
			// 
			// lstReceiveMessage
			// 
			this->lstReceiveMessage->Font = (gcnew System::Drawing::Font(L"PMingLiU", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->lstReceiveMessage->FormattingEnabled = true;
			this->lstReceiveMessage->HorizontalScrollbar = true;
			this->lstReceiveMessage->Location = System::Drawing::Point(8, 24);
			this->lstReceiveMessage->Name = L"lstReceiveMessage";
			this->lstReceiveMessage->Size = System::Drawing::Size(568, 173);
			this->lstReceiveMessage->TabIndex = 1;
			// 
			// btnClear
			// 
			this->btnClear->Location = System::Drawing::Point(496, 208);
			this->btnClear->Name = L"btnClear";
			this->btnClear->Size = System::Drawing::Size(80, 24);
			this->btnClear->TabIndex = 0;
			this->btnClear->Text = L"Clear";
			this->btnClear->UseVisualStyleBackColor = true;
			this->btnClear->Click += gcnew System::EventHandler(this, &Form1::btnClear_Click);
			// 
			// txtSendMsg_Data
			// 
			this->txtSendMsg_Data->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->txtSendMsg_Data->Location = System::Drawing::Point(322, 32);
			this->txtSendMsg_Data->MaxLength = 191;
			this->txtSendMsg_Data->Name = L"txtSendMsg_Data";
			this->txtSendMsg_Data->Size = System::Drawing::Size(246, 22);
			this->txtSendMsg_Data->TabIndex = 41;
			this->txtSendMsg_Data->Text = L"00-00-00-00-00-00-00-00";
			this->txtSendMsg_Data->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->txtSendMsg_Data->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::txtSendMsg_D1_KeyPress);
			// 
			// cboSendMsg_DLC
			// 
			this->cboSendMsg_DLC->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->cboSendMsg_DLC->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->cboSendMsg_DLC->FormattingEnabled = true;
			this->cboSendMsg_DLC->Items->AddRange(gcnew cli::array< System::Object^  >(9) {L"0", L"1", L"2", L"3", L"4", L"5", L"6", 
				L"7", L"8"});
			this->cboSendMsg_DLC->Location = System::Drawing::Point(274, 32);
			this->cboSendMsg_DLC->Name = L"cboSendMsg_DLC";
			this->cboSendMsg_DLC->Size = System::Drawing::Size(40, 24);
			this->cboSendMsg_DLC->TabIndex = 40;
			// 
			// cboSendMsg_Type
			// 
			this->cboSendMsg_Type->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->cboSendMsg_Type->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->cboSendMsg_Type->FormattingEnabled = true;
			this->cboSendMsg_Type->Items->AddRange(gcnew cli::array< System::Object^  >(4) {L"0:Remote", L"1:CAN", L"2:FD no BRS", L"3:FD + BRS"});
			this->cboSendMsg_Type->Location = System::Drawing::Point(170, 32);
			this->cboSendMsg_Type->Name = L"cboSendMsg_Type";
			this->cboSendMsg_Type->Size = System::Drawing::Size(94, 24);
			this->cboSendMsg_Type->TabIndex = 39;
			this->cboSendMsg_Type->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::cboSendMsg_Type_SelectedIndexChanged);
			// 
			// grpSendMessage
			// 
			this->grpSendMessage->Controls->Add(this->btnSend);
			this->grpSendMessage->Controls->Add(this->label17);
			this->grpSendMessage->Controls->Add(this->cboSendMsg_Port);
			this->grpSendMessage->Controls->Add(this->txtSendMsg_ID);
			this->grpSendMessage->Controls->Add(this->txtSendMsg_Data);
			this->grpSendMessage->Controls->Add(this->cboSendMsg_DLC);
			this->grpSendMessage->Controls->Add(this->cboSendMsg_Type);
			this->grpSendMessage->Controls->Add(this->cboSendMsg_Mode);
			this->grpSendMessage->Controls->Add(this->label4);
			this->grpSendMessage->Controls->Add(this->label13);
			this->grpSendMessage->Controls->Add(this->label14);
			this->grpSendMessage->Controls->Add(this->label15);
			this->grpSendMessage->Controls->Add(this->label16);
			this->grpSendMessage->Enabled = false;
			this->grpSendMessage->Font = (gcnew System::Drawing::Font(L"PMingLiU", 11.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->grpSendMessage->ForeColor = System::Drawing::Color::Black;
			this->grpSendMessage->Location = System::Drawing::Point(8, 104);
			this->grpSendMessage->Name = L"grpSendMessage";
			this->grpSendMessage->Size = System::Drawing::Size(584, 96);
			this->grpSendMessage->TabIndex = 4;
			this->grpSendMessage->TabStop = false;
			this->grpSendMessage->Text = L"CAN1/2 Send Message";
			// 
			// cboSendMsg_Mode
			// 
			this->cboSendMsg_Mode->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->cboSendMsg_Mode->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->cboSendMsg_Mode->FormattingEnabled = true;
			this->cboSendMsg_Mode->Items->AddRange(gcnew cli::array< System::Object^  >(2) {L"11-bit ID", L"29-bit ID"});
			this->cboSendMsg_Mode->Location = System::Drawing::Point(2, 32);
			this->cboSendMsg_Mode->Name = L"cboSendMsg_Mode";
			this->cboSendMsg_Mode->Size = System::Drawing::Size(72, 24);
			this->cboSendMsg_Mode->TabIndex = 37;
			this->cboSendMsg_Mode->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::cboSendMsg_Mode_SelectedIndexChanged);
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label4->Location = System::Drawing::Point(424, 16);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(35, 16);
			this->label4->TabIndex = 29;
			this->label4->Text = L"Data";
			// 
			// label13
			// 
			this->label13->AutoSize = true;
			this->label13->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label13->Location = System::Drawing::Point(282, 16);
			this->label13->Name = L"label13";
			this->label13->Size = System::Drawing::Size(33, 16);
			this->label13->TabIndex = 28;
			this->label13->Text = L"DLC";
			// 
			// label14
			// 
			this->label14->AutoSize = true;
			this->label14->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label14->Location = System::Drawing::Point(200, 16);
			this->label14->Name = L"label14";
			this->label14->Size = System::Drawing::Size(36, 16);
			this->label14->TabIndex = 27;
			this->label14->Text = L"Type";
			// 
			// label15
			// 
			this->label15->AutoSize = true;
			this->label15->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label15->Location = System::Drawing::Point(16, 16);
			this->label15->Name = L"label15";
			this->label15->Size = System::Drawing::Size(40, 16);
			this->label15->TabIndex = 25;
			this->label15->Text = L"Mode";
			// 
			// label16
			// 
			this->label16->AutoSize = true;
			this->label16->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label16->Location = System::Drawing::Point(93, 16);
			this->label16->Name = L"label16";
			this->label16->Size = System::Drawing::Size(55, 16);
			this->label16->TabIndex = 26;
			this->label16->Text = L"ID (Hex)";
			// 
			// tmrRxMsg
			// 
			this->tmrRxMsg->Interval = 1;
			this->tmrRxMsg->Tick += gcnew System::EventHandler(this, &Form1::tmrRxMsg_Tick);
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->txtDSamplePoint);
			this->groupBox1->Controls->Add(this->label18);
			this->groupBox1->Controls->Add(this->txtDBaudrate);
			this->groupBox1->Controls->Add(this->label19);
			this->groupBox1->Controls->Add(this->label20);
			this->groupBox1->Controls->Add(this->label21);
			this->groupBox1->Controls->Add(this->txtASamplePoint);
			this->groupBox1->Controls->Add(this->label5);
			this->groupBox1->Controls->Add(this->txtABaudrate);
			this->groupBox1->Controls->Add(this->label8);
			this->groupBox1->Controls->Add(this->label6);
			this->groupBox1->Controls->Add(this->label7);
			this->groupBox1->Location = System::Drawing::Point(248, 8);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(344, 88);
			this->groupBox1->TabIndex = 14;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"Baud Rate";
			// 
			// txtDSamplePoint
			// 
			this->txtDSamplePoint->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->txtDSamplePoint->Location = System::Drawing::Point(256, 56);
			this->txtDSamplePoint->MaxLength = 10;
			this->txtDSamplePoint->Name = L"txtDSamplePoint";
			this->txtDSamplePoint->Size = System::Drawing::Size(56, 22);
			this->txtDSamplePoint->TabIndex = 15;
			this->txtDSamplePoint->Text = L"80.00";
			this->txtDSamplePoint->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// label18
			// 
			this->label18->AutoSize = true;
			this->label18->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label18->Location = System::Drawing::Point(8, 59);
			this->label18->Name = L"label18";
			this->label18->Size = System::Drawing::Size(76, 16);
			this->label18->TabIndex = 11;
			this->label18->Text = L"Data Phase";
			// 
			// txtDBaudrate
			// 
			this->txtDBaudrate->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->txtDBaudrate->Location = System::Drawing::Point(88, 56);
			this->txtDBaudrate->MaxLength = 10;
			this->txtDBaudrate->Name = L"txtDBaudrate";
			this->txtDBaudrate->Size = System::Drawing::Size(80, 22);
			this->txtDBaudrate->TabIndex = 12;
			this->txtDBaudrate->Text = L"2000.000";
			this->txtDBaudrate->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// label19
			// 
			this->label19->AutoSize = true;
			this->label19->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label19->Location = System::Drawing::Point(312, 59);
			this->label19->Name = L"label19";
			this->label19->Size = System::Drawing::Size(20, 16);
			this->label19->TabIndex = 16;
			this->label19->Text = L"%";
			// 
			// label20
			// 
			this->label20->AutoSize = true;
			this->label20->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label20->Location = System::Drawing::Point(224, 59);
			this->label20->Name = L"label20";
			this->label20->Size = System::Drawing::Size(26, 16);
			this->label20->TabIndex = 14;
			this->label20->Text = L"SP";
			// 
			// label21
			// 
			this->label21->AutoSize = true;
			this->label21->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label21->Location = System::Drawing::Point(168, 59);
			this->label21->Name = L"label21";
			this->label21->Size = System::Drawing::Size(36, 16);
			this->label21->TabIndex = 13;
			this->label21->Text = L"kbps";
			// 
			// txtASamplePoint
			// 
			this->txtASamplePoint->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->txtASamplePoint->Location = System::Drawing::Point(256, 21);
			this->txtASamplePoint->MaxLength = 10;
			this->txtASamplePoint->Name = L"txtASamplePoint";
			this->txtASamplePoint->Size = System::Drawing::Size(56, 22);
			this->txtASamplePoint->TabIndex = 9;
			this->txtASamplePoint->Text = L"80.00";
			this->txtASamplePoint->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label5->Location = System::Drawing::Point(8, 24);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(67, 16);
			this->label5->TabIndex = 5;
			this->label5->Text = L"Arbitration";
			// 
			// txtABaudrate
			// 
			this->txtABaudrate->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->txtABaudrate->Location = System::Drawing::Point(88, 21);
			this->txtABaudrate->MaxLength = 10;
			this->txtABaudrate->Name = L"txtABaudrate";
			this->txtABaudrate->Size = System::Drawing::Size(80, 22);
			this->txtABaudrate->TabIndex = 6;
			this->txtABaudrate->Text = L"500.000";
			this->txtABaudrate->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// label8
			// 
			this->label8->AutoSize = true;
			this->label8->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label8->Location = System::Drawing::Point(312, 24);
			this->label8->Name = L"label8";
			this->label8->Size = System::Drawing::Size(20, 16);
			this->label8->TabIndex = 10;
			this->label8->Text = L"%";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label6->Location = System::Drawing::Point(224, 24);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(26, 16);
			this->label6->TabIndex = 8;
			this->label6->Text = L"SP";
			// 
			// label7
			// 
			this->label7->AutoSize = true;
			this->label7->Font = (gcnew System::Drawing::Font(L"Arial", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label7->Location = System::Drawing::Point(168, 24);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(36, 16);
			this->label7->TabIndex = 7;
			this->label7->Text = L"kbps";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(604, 445);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->grpScanDevice);
			this->Controls->Add(this->grpReceiveMessage);
			this->Controls->Add(this->grpSendMessage);
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"Send/Receive CAN Message Demo";
			this->grpScanDevice->ResumeLayout(false);
			this->grpReceiveMessage->ResumeLayout(false);
			this->grpSendMessage->ResumeLayout(false);
			this->grpSendMessage->PerformLayout();
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void btnScan_Click(System::Object^  sender, System::EventArgs^  e) {			
			int iRet = 0;
			unsigned char i, BoardID;
            String^ str;

            cboModuleName->Items->Clear();
			
			for (i = 0; i < 16; i++)
            {                
				iRet = PISOCANFD::CANFD_GetCardBoardID(i, BoardID);
				if (iRet == 0)
				{
					switch (BoardID)
                    {
                        case 1: //PISO-CAN200U-FD
							str = String::Format("BoardNo: {0:D2}, {1}", i, "PISO-CAN200U-FD");
                            break;
                        case 2: //PISO-CAN400U-FD
                            str = String::Format("BoardNo: {0:D2}, {1}", i, "PISO-CAN400U-FD");
                            break;
                        case 3: //PEX-CAN200i-FD
                            str = String::Format("BoardNo: {0:D2}, {1}", i, "PEX-CAN200i-FD");
                            break;
                        case 4: //PCM-CAN200-FD
                            str = String::Format("BoardNo: {0:D2}, {1}", i, "PCM-CAN200-FD");
                            break;
                        default:
                            str = String::Format("BoardNo: {0:D2}, Not Support(0x{1:X})", i, BoardID);
                            break;
                    }
				}
				else
                {
					str = String::Format("BoardNo: {0:D2}, {1}", i, "NONE");
                }

				cboModuleName->Items->Add(str);
			}

			cboModuleName->SelectedIndex = 0;
		 }

private: System::Void cboModuleName_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			int iRet = 0;
			unsigned char PortNum = 0;

			SelectedBoardNo = (unsigned char)cboModuleName->SelectedIndex;

			if (cboModuleName->Items[SelectedBoardNo]->ToString()->Length > 20)
            {
				iRet = PISOCANFD::CANFD_GetCardPortNum((unsigned char)SelectedBoardNo, PortNum);
                if (iRet == 0)
                {
                    switch (PortNum)
                    {
                        case 2:
                            SelectedCardPortNo = PortNum;
                            break;
                        case 4:
                            SelectedCardPortNo = PortNum;
                            break;
                        default:
                            SelectedCardPortNo = 0;
                            break;
                    }
                }

				btnActive->Enabled = true;
            }
            else
            {
                SelectedCardPortNo = 0;
				btnActive->Enabled = false;
            }
		 }

private: System::Void btnOpen_Click(System::Object^  sender, System::EventArgs^  e) {
			int iRet = 0;
			String^ message;
			String^ caption;
            unsigned char BoardNo = (unsigned char)SelectedBoardNo;

            if (SelectedCardOpen == true)
            {
                tmrRxMsg->Enabled = false;

                //Close Device
				iRet = PISOCANFD::CANFD_CloseBoard(BoardNo);                
                if (iRet != 0)
                {
					message = String::Format("Fail to close device.[CANFD_CloseBoard, iRet=0x{0:X}]", iRet);
                    caption = "Error";
					MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                }
                else
                {
                    SelectedCardOpen = false;

                    btnScan->Enabled = true;
                    cboModuleName->Enabled = true;
                    btnActive->Text = "Active";

                    grpSendMessage->Enabled = false;
                    grpReceiveMessage->Enabled = false;

                }
            }
            else
            {
                    bool SetDeviceParam = true;


                    //Active Board
					iRet = PISOCANFD::CANFD_ActiveBoard(BoardNo);
                    if (iRet == 0)
                    {
                        unsigned char OPMode, ISOMode;
                        UInt32 BR_Arbit, BR_DataPhase;
                        UInt16 BR_ArbitSP, BR_DataPhaseSP;

						//====== Set CAN P1 parameters ======
						//Initialize CAN P1
						if (SetDeviceParam == true)
						{
							iRet = PISOCANFD::CANFD_Init(BoardNo, CANPort_P1);
							if (iRet != 0)
							{
								message = String::Format("Initialize CAN Port1 error. [CANFD_Init, iRet=0x{0:X}].", iRet);
								caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
								SetDeviceParam = false;
							}
						}

                        //Set CAN P1 baudrate and sample point                    
                        if (SetDeviceParam == true)
                        {
							BR_Arbit = Convert::ToUInt32(Convert::ToSingle(txtABaudrate->Text) * 1000.0);
							BR_DataPhase = Convert::ToUInt32(Convert::ToSingle(txtDBaudrate->Text) * 1000.0);
							BR_ArbitSP = Convert::ToUInt32(Convert::ToSingle(txtASamplePoint->Text) * 100.0);
							BR_DataPhaseSP = Convert::ToUInt32(Convert::ToSingle(txtDSamplePoint->Text) * 100.0);
                            if ((BR_Arbit >= 5000 && BR_Arbit <= 2000000) && (BR_DataPhase >= 100000 && BR_DataPhase <= 5000000)
                                && (BR_ArbitSP > 0 && BR_ArbitSP <= 10000) && (BR_DataPhaseSP > 0 && BR_DataPhaseSP <= 10000))
                            {
								iRet = PISOCANFD::CANFD_SetBitRateWithSP(BoardNo, CANPort_P1, BR_Arbit, BR_DataPhase, BR_ArbitSP, BR_DataPhaseSP);
                                if (iRet != 0)
                                {
									message = String::Format("Set CAN Port1 baudrate and sample point error. [CANFD_SetBitRateWithSP, iRet=0x{0:X}].", iRet);
                                    caption = "Error";
                                    MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                    SetDeviceParam = false;
                                }
                            }
                            else
                            {
                                message = String::Format("Port1 baudrate or sample point parameters error.");
                                caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                SetDeviceParam = false;
                            }
                        }

                        //Set CAN P1 Port ISO mode
                        if (SetDeviceParam == true)
                        {
                            //Set CAN Port to use ISO-11898 CAN FD specification
							ISOMode = 1; //1: ISO-11898, 0:  Bosch v1.0

							iRet = PISOCANFD::CANFD_SetISOCRCEn(BoardNo, CANPort_P1, ISOMode);
                            if (iRet != 0)
                            {
								message = String::Format("Set CAN Port1 ISO mode error. [CANFD_SetISOCRCEn, iRet=0x{0:X}].", iRet);
                                caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                SetDeviceParam = false;
                            }
                        }
						
						//Set CAN P1 Port Operation mode
                        if (SetDeviceParam == true)
                        {
                            //Set CAN Port to CAN FD Mode
							OPMode = 0; //0: CAN FD, 1: CAN 2.0, 2: Listen Only

							iRet = PISOCANFD::CANFD_SetOPMode(BoardNo, CANPort_P1, OPMode);
                            if (iRet != 0)
                            {
								message = String::Format("Set CAN Port1 operation mode error. [CANFD_SetOPMode, iRet=0x{0:X}].", iRet);
                                caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                SetDeviceParam = false;
                            }
                        }

						//====== Set CAN P2 parameters ======
						//Initialize CAN P2
						if (SetDeviceParam == true)
						{
							iRet = PISOCANFD::CANFD_Init(BoardNo, CANPort_P2);
							if (iRet != 0)
							{
								message = String::Format("Initialize CAN Port2 error. [CANFD_Init, iRet=0x{0:X}].", iRet);
								caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
								SetDeviceParam = false;
							}
						}

						//Set CAN P2 baudrate and sample point                    
                        if (SetDeviceParam == true)
                        {
							BR_Arbit = Convert::ToUInt32(Convert::ToSingle(txtABaudrate->Text) * 1000.0);
							BR_DataPhase = Convert::ToUInt32(Convert::ToSingle(txtDBaudrate->Text) * 1000.0);
							BR_ArbitSP = Convert::ToUInt32(Convert::ToSingle(txtASamplePoint->Text) * 100.0);
							BR_DataPhaseSP = Convert::ToUInt32(Convert::ToSingle(txtDSamplePoint->Text) * 100.0);
                            if ((BR_Arbit >= 5000 && BR_Arbit <= 2000000) && (BR_DataPhase >= 100000 && BR_DataPhase <= 5000000)
                                && (BR_ArbitSP > 0 && BR_ArbitSP <= 10000) && (BR_DataPhaseSP > 0 && BR_DataPhaseSP <= 10000))
                            {
								iRet = PISOCANFD::CANFD_SetBitRateWithSP(BoardNo, CANPort_P2, BR_Arbit, BR_DataPhase, BR_ArbitSP, BR_DataPhaseSP);
                                if (iRet != 0)
                                {
									message = String::Format("Set CAN Port2 baudrate and sample point error. [CANFD_SetBitRateWithSP, iRet=0x{0:X}].", iRet);
                                    caption = "Error";
                                    MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                    SetDeviceParam = false;
                                }
                            }
                            else
                            {
                                message = String::Format("Port2 baudrate or sample point parameters error.");
                                caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                SetDeviceParam = false;
                            }
                        }
			

                        //Set CAN P2 Port ISO mode
                        if (SetDeviceParam == true)
                        {
                            //Set CAN Port to use ISO-11898 CAN FD specification
							ISOMode = 1; //1: ISO-11898, 0:  Bosch v1.0

							iRet = PISOCANFD::CANFD_SetISOCRCEn(BoardNo, CANPort_P2, ISOMode);
                            if (iRet != 0)
                            {
								message = String::Format("Set CAN Port2 ISO mode error. [CANFD_SetISOCRCEn, iRet=0x{0:X}].", iRet);
                                caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                SetDeviceParam = false;
                            }
                        }
						
						//Set CAN P2 Port Operation mode
                        if (SetDeviceParam == true)
                        {
                            //Set CAN Port to CAN FD Mode
							OPMode = 0; //0: CAN FD, 1: CAN 2.0, 2: Listen Only

							iRet = PISOCANFD::CANFD_SetOPMode(BoardNo, CANPort_P2, OPMode);
                            if (iRet != 0)
                            {
								message = String::Format("Set CAN Port2 operation mode error. [CANFD_SetOPMode, iRet=0x{0:X}].", iRet);
                                caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                SetDeviceParam = false;
                            }
                        }
		
						//Install CAN Rx IRQ
						if (SetDeviceParam == true)
                        {
							iRet = PISOCANFD::CANFD_InstallIrq(BoardNo);
                            if (iRet != 0)
                            {
								message = String::Format("Set Board {0} IRQ error. [CANFD_InstallIrq, iRet=0x{1:X}].", BoardNo, iRet);
                                caption = "Error";
								MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                                SetDeviceParam = false;
                            }
                        }


						if (SetDeviceParam == true)
                        {
                            SelectedCardOpen = true;

                            btnScan->Enabled = false;
                            cboModuleName->Enabled = false;
                            btnActive->Text = "Close";

                            grpSendMessage->Enabled = true;
                            grpReceiveMessage->Enabled = true;

                            cboSendMsg_Mode->SelectedIndex = 0;
                            cboSendMsg_Type->SelectedIndex = 0;
                            cboSendMsg_DLC->SelectedIndex = 8;
                            cboSendMsg_Port->SelectedIndex = 0;
                            lstReceiveMessage->Items->Clear();

                            tmrRxMsg->Enabled = true;
						 }
						else
                        {
							PISOCANFD::CANFD_CloseBoard(BoardNo);
                        }
					}
                    else
                    {
						message = String::Format("Fail to active board.[CANFD_ActiveBoard, iRet=0x{0:X}]", iRet);
                        caption = "Error";
						MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                    }
				
			}
		 }

private: System::Void txtSendMsg_D1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
			// this will only allow valid hex values [0-9][a-f][A-F][-] to be entered. See ASCII table
            wchar_t c = e->KeyChar;
            if (c != '\b' && !((c <= 0x66 && c >= 0x61) || (c <= 0x46 && c >= 0x41) || (c >= 0x30 && c <= 0x39) || (c == 0x2c) || (c == '-')))
            {
                e->Handled = true;
            }
		 }

private: System::Void txtSendMsg_ID_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
			 // this will only allow valid hex values [0-9][a-f][A-F] to be entered. See ASCII table
            wchar_t c = e->KeyChar;
            if (c != '\b' && !((c <= 0x66 && c >= 0x61) || (c <= 0x46 && c >= 0x41) || (c >= 0x30 && c <= 0x39) || (c == 0x2c)))
            {
                e->Handled = true;
            }
		 }

private: System::Void txtSendMsg_ID_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			if (txtSendMsg_ID->Text != "")
            {
				UInt32 value = Convert::ToUInt32(txtSendMsg_ID->Text, 16);
                if (cboSendMsg_Mode->SelectedIndex == 0) //Standard
                {
                    if (value > 0x7FF)
                        txtSendMsg_ID->Text = "7FF";
                }
                else
                {
                    if (value > 0x1FFFFFFF)
                        txtSendMsg_ID->Text = "1FFFFFFF";
                }
            }

            txtSendMsg_ID->Text = txtSendMsg_ID->Text->ToUpper();
            txtSendMsg_ID->SelectionStart = txtSendMsg_ID->Text->Length;
		 }

private: System::Void txtSendMsg_ID_Leave(System::Object^  sender, System::EventArgs^  e) {
			if (cboSendMsg_Mode->SelectedIndex == 0) //Standard
            {
                txtSendMsg_ID->Text = txtSendMsg_ID->Text->PadLeft(3, '0');
            }
            else
            {
                txtSendMsg_ID->Text = txtSendMsg_ID->Text->PadLeft(8, '0');
            }
		 }

private: System::Void cboSendMsg_Mode_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {			
            if (cboSendMsg_Mode->SelectedIndex == 0) //Standard
            {
                txtSendMsg_ID->MaxLength = 3;
				txtSendMsg_ID->Text = "001";
            }
            else //Extended
            {
                txtSendMsg_ID->MaxLength = 8;
				txtSendMsg_ID->Text = "00000001";
            }
		 }

private: System::Void cboSendMsg_Type_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			cboSendMsg_DLC->Items->Clear();
            if (cboSendMsg_Type->SelectedIndex == 0 || cboSendMsg_Type->SelectedIndex == 1) //Normal CAN
            {
                cboSendMsg_DLC->Items->Add("0");
                cboSendMsg_DLC->Items->Add("1");
				cboSendMsg_DLC->Items->Add("2");
                cboSendMsg_DLC->Items->Add("3");
				cboSendMsg_DLC->Items->Add("4");
                cboSendMsg_DLC->Items->Add("5");
				cboSendMsg_DLC->Items->Add("6");
                cboSendMsg_DLC->Items->Add("7");
				cboSendMsg_DLC->Items->Add("8");
            }
            else //CAN FD
            {
                cboSendMsg_DLC->Items->Add("0");
                cboSendMsg_DLC->Items->Add("1");
				cboSendMsg_DLC->Items->Add("2");
                cboSendMsg_DLC->Items->Add("3");
				cboSendMsg_DLC->Items->Add("4");
                cboSendMsg_DLC->Items->Add("5");
				cboSendMsg_DLC->Items->Add("6");
                cboSendMsg_DLC->Items->Add("7");
				cboSendMsg_DLC->Items->Add("8");
				cboSendMsg_DLC->Items->Add("12");
                cboSendMsg_DLC->Items->Add("16");
				cboSendMsg_DLC->Items->Add("20");
                cboSendMsg_DLC->Items->Add("24");
				cboSendMsg_DLC->Items->Add("32");
                cboSendMsg_DLC->Items->Add("48");
				cboSendMsg_DLC->Items->Add("64");
                
            }
            cboSendMsg_DLC->SelectedIndex = 8;
		 }

private: System::Void btnSend_Click(System::Object^  sender, System::EventArgs^  e) {
			int iRet;

            unsigned char MODE;
            unsigned int ID;
            unsigned char TYPE;
            unsigned char DLC;
            array< unsigned char >^ DATA = gcnew array< unsigned char >(64); 
			String^ message;
			String^ caption;

            MODE = (unsigned char)cboSendMsg_Mode->SelectedIndex;
			ID = Convert::ToUInt32(txtSendMsg_ID->Text, 16);
            TYPE = (unsigned char)cboSendMsg_Type->SelectedIndex;
            DLC = (unsigned char)cboSendMsg_DLC->SelectedIndex;

			//convert data string to byte array
            String^ hexValues = txtSendMsg_Data->Text;
            array< String^ >^ hexValuesSplit = hexValues->Split('-');
            int hexValuesLen;
            hexValuesLen = (hexValuesSplit->Length > 64) ? 64 : hexValuesSplit->Length;
            for (int i = 0; i < hexValuesLen; i++)
            {
				DATA[i] = Convert::ToByte(hexValuesSplit[i], 16);
            }


			if (cboSendMsg_Port->SelectedIndex == 0)  //CAN1
            {
				iRet = PISOCANFD::CANFD_SendCANMsg(SelectedBoardNo, CANPort_P1, MODE, ID, TYPE, DLC, DATA);
                if (iRet != 0)
                {
					message = String::Format("Send CAN1 Message error. [CANFD_SendCANMsg, iRet=0x{0:X}].", iRet);
                    caption = "Error";
                    MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                }
            }
            else //CAN2
            {
				iRet = PISOCANFD::CANFD_SendCANMsg(SelectedBoardNo, CANPort_P2, MODE, ID, TYPE, DLC, DATA);
                if (iRet != 0)
                {
                    message = String::Format("Send CAN2 Message error. [CANFD_SendCANMsg, iRet=0x{0:X}].", iRet);
                    caption = "Error";
                    MessageBox::Show(message, caption, MessageBoxButtons::OK, MessageBoxIcon::Error);
                }
            }
		 }

private: System::Void tmrRxMsg_Tick(System::Object^  sender, System::EventArgs^  e) {
			int iRet;
            unsigned short DataCount =0;
            //CAN Information
            unsigned int CAN_ID = 0;
            unsigned char CAN_Mode = 0, CAN_Type = 0, CAN_Dlen = 0;
            array< unsigned char >^ CAN_Data = gcnew array< unsigned char >(64); 
            unsigned int CAN_HTime = 0, CAN_LTime = 0;
			String^ message;
			UInt64 u64Data;

			if (SelectedCardOpen == true)
            {
				//Receive CAN1 Message
				iRet = PISOCANFD::CANFD_RecvCANMsgCnt(SelectedBoardNo, CANPort_P1,  DataCount);
                if (iRet == 0 && DataCount > 0)
                {
					iRet = PISOCANFD::CANFD_RecvCANMsg(SelectedBoardNo, CANPort_P1,  CAN_Mode,  CAN_ID,  CAN_Type,  CAN_Dlen, CAN_Data,  CAN_HTime,  CAN_LTime);
                    if (iRet == 0)
                    {
						u64Data = (((UInt64)CAN_HTime << 32) + CAN_LTime);
						CAN_Dlen = CANFD_DLC2DBN[CAN_Dlen];
                        if (CAN_Mode == 0)    //2.0A
                        {
                            message = String::Format("CAN1: [{0}.{1:0000000}] Mode={2}, ID={3:X3}, Type={4}, DLC={5}, ", u64Data / 10000000, u64Data % 10000000, CAN_Mode, CAN_ID, CAN_Type, CAN_Dlen);
                        }
                        else
                        {
                            message = String::Format("CAN1: [{0}.{1:0000000}] Mode={2}, ID={3:X8}, Type={4}, DLC={5}, ", u64Data / 10000000, u64Data % 10000000, CAN_Mode, CAN_ID, CAN_Type, CAN_Dlen);
                        }

                        if (CAN_Type != 0) //Not Remote frame
                        {
							if (CAN_Dlen > 0) message = message + String::Format("DATA=");

                            for (int i = 0; i < CAN_Dlen; i++)
                            {
                                message = message + String::Format("{0:X2} ", CAN_Data[i]);
                            }
                        }

                        lstReceiveMessage->Items->Add(message);                        
                    }
                }

				//Receive CAN2 Message
				iRet = PISOCANFD::CANFD_RecvCANMsgCnt(SelectedBoardNo, CANPort_P2,  DataCount);
                if (iRet == 0 && DataCount > 0)
                {
					iRet = PISOCANFD::CANFD_RecvCANMsg(SelectedBoardNo, CANPort_P2,  CAN_Mode,  CAN_ID,  CAN_Type,  CAN_Dlen, CAN_Data,  CAN_HTime,  CAN_LTime);
                    if (iRet == 0)
                    {
						u64Data = (((UInt64)CAN_HTime << 32) + CAN_LTime);
						CAN_Dlen = CANFD_DLC2DBN[CAN_Dlen];
                        if (CAN_Mode == 0)    //2.0A
                        {
                            message = String::Format("CAN2: [{0}.{1:0000000}] Mode={2}, ID={3:X3}, Type={4}, DLC={5}, ", u64Data / 10000000, u64Data % 10000000, CAN_Mode, CAN_ID, CAN_Type, CAN_Dlen);
                        }
                        else
                        {
                            message = String::Format("CAN2: [{0}.{1:0000000}] Mode={2}, ID={3:X8}, Type={4}, DLC={5}, ", u64Data / 10000000, u64Data % 10000000, CAN_Mode, CAN_ID, CAN_Type, CAN_Dlen);
                        }

                        if (CAN_Type != 0) //Not Remote frame
                        {
							if (CAN_Dlen > 0) message = message + String::Format("DATA=");

                            for (int i = 0; i < CAN_Dlen; i++)
                            {
                                message = message + String::Format("{0:X2} ", CAN_Data[i]);
                            }
                        }

                        lstReceiveMessage->Items->Add(message);                        
                    }
                }

				lstReceiveMessage->SelectedIndex = lstReceiveMessage->Items->Count - 1;
			}
		 }
private: System::Void btnClear_Click(System::Object^  sender, System::EventArgs^  e) {
			 lstReceiveMessage->Items->Clear();
		 }

};
}

