using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using PISOCM100_Net;

namespace TransmitMsg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
        }

        private bool IsActiveFlag = false;
        static Byte SelectedBoardNo;

        private void Active_Click(object sender, EventArgs e)
        {
            UInt32 AccMask,AccCode;
            Byte BoardNo, Baud,BT0,BT1;
            int Ret;
            UInt16[] BaudMap = { 10, 20, 50, 125, 250, 500, 800, 1000 };

            BoardNo = (Byte)comboBox1.SelectedIndex;
            Baud = (Byte)(comboBox2.SelectedIndex+1);

         
            //baud 1:10K, 2:20K, 3:50K, 4:125K, 5:250K, 6:500K, 7:800K, 8:1M bps
            if (IsActiveFlag)
            { //If board is active before, close it first.
                CM100.CM100_CloseBoard(BoardNo);
                IsActiveFlag = false;
            }
            Ret = CM100.CM100_ActiveBoard(BoardNo); //Active board
            if (Ret != 0)
            {
                MessageBox.Show("Active Board Error!");
                this.Text = "TransmitMsg --- No board is active";
                return;
            }

            Ret = CM100.CM100_Init(BoardNo, 1);  //Init board
            if (Ret != 0)
            {
                MessageBox.Show("Init Board Error!");
                CM100.CM100_CloseBoard(BoardNo);
                this.Text = "TransmitMsg --- No board is active";
                return;
            }

       
            AccMask = 0xffffffff;
            AccCode = 0x0;
            BT0 = 0; 
            BT1 = 0;
            //BT0 and BT1 are only useful when user-defined baud is used.


            Ret = CM100.CM100_ConfigWithoutStruct(BoardNo, 1, AccCode, AccMask, Baud, BT0, BT1); //Configure board parameters
            if (Ret != 0)
            {
                MessageBox.Show("Configure Board Error!");
                CM100.CM100_CloseBoard(BoardNo);
                this.Text = "TransmitMsg --- No board is active";
                return;
            }
            SelectedBoardNo = BoardNo;
            this.Text = "TransmitMsg --- Board " + BoardNo.ToString() + " is active with baud " + BaudMap[comboBox2.SelectedIndex].ToString() + " K pbs";
            IsActiveFlag = true;
        }

        private void textBox1_KeyPress(Object o, KeyPressEventArgs e)
        {
            e.Handled = "0123456789ABCDEF\b".IndexOf(char.ToUpper(e.KeyChar)) < 0;
        }
  

        private void Exit_Click(object sender, EventArgs e)
        {
            if (IsActiveFlag)
                CM100.CM100_CloseBoard(SelectedBoardNo);
            Application.Exit();
        }

        private void Send_Click(object sender, EventArgs e)
        {
            int Ret;
            UInt32 MsgID;
            Byte Mode,RTR,DataLen;
            Byte[] Data = new Byte[8];
            if (radioButton1.Checked)
            { //Check the CAN message mode
                Mode = 0;
                if (this.textBox1.Text == "")
                {
                    MessageBox.Show("Msg ID can't null.");
                    return;
                }
                if (((Convert.ToUInt32(this.textBox1.Text, 16)) >= 0) && ((Convert.ToUInt32(this.textBox1.Text, 16)) <= 0x7ff))
                { MsgID = Convert.ToUInt32(this.textBox1.Text, 16); }
                //MessageBox.Show((Convert.ToUInt32(this.textBox1.Text,16)).ToString());
                else
                {  
                    MessageBox.Show("11bits ID Range=0~7ff(hex)");
                    return;
                }
            }
            else if (radioButton1.Checked)
            {
                Mode = 1;
                if (this.textBox1.Text == "")
                {
                    MessageBox.Show("Msg ID can't null.");
                    return;
                }
                
                if (Convert.ToUInt32(this.textBox1.Text,16) >= 0 && Convert.ToUInt32(this.textBox1.Text,16) <= 0x1fffffff)
                { MsgID = Convert.ToUInt32(this.textBox1.Text,16) & 0x1fffffff; }
                else
                {
                    MessageBox.Show("29bits ID Range=0~1fffffff(hex)");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Check 11bits / 29 bits ID, please.");
                return;
            }


            if (UseRTR.Checked) RTR = 1; //Check if the RTR is used or not
            else RTR = 0; 

            if (this.textBox2.Text == "")
                this.textBox2.Text = "00";
            if (this.textBox3.Text == "")
                this.textBox3.Text = "00";
            if (this.textBox4.Text == "")
                this.textBox4.Text = "00";
            if (this.textBox5.Text == "")
                this.textBox5.Text = "00";
            if (this.textBox6.Text == "")
                this.textBox6.Text = "00";
            if (this.textBox7.Text == "")
                this.textBox7.Text = "00";
            if (this.textBox8.Text == "")
                this.textBox8.Text = "00";
            if (this.textBox9.Text == "")
                this.textBox9.Text = "00";
            DataLen = (Byte)comboBox3.SelectedIndex;  //Get data length parameter
            Data[0] = Convert.ToByte(this.textBox2.Text, 16);
            Data[1] = Convert.ToByte(this.textBox3.Text, 16);
            Data[2] = Convert.ToByte(this.textBox4.Text, 16);
            Data[3] = Convert.ToByte(this.textBox5.Text, 16);
            Data[4] = Convert.ToByte(this.textBox6.Text, 16);
            Data[5] = Convert.ToByte(this.textBox7.Text, 16);
            Data[6] = Convert.ToByte(this.textBox8.Text, 16);
            Data[7] = Convert.ToByte(this.textBox9.Text, 16);
           // e.handled


            Ret = CM100.CM100_SendWithoutStruct(SelectedBoardNo, 1, Mode, MsgID, RTR, DataLen, Data); //Send CAN message to CAN bus
            if (Ret!=0)
                MessageBox.Show("Send Message Error, Error Code " + Ret.ToString());
        }
    }
}
