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_x64;

namespace ReceiveMsg
{
    public partial class Form1 : Form
    {
        bool IsActiveFlag = false;
        byte BoardNo = 0;
        byte PortNo = 1;
        byte CANBaud = 8;
        uint ListCount = 0;
        string ListStr;
        short sRet;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cboBoard.SelectedIndex = 0;     //Board0
            cboBaudrate.SelectedIndex = 8;  //CAN baud rate: 1000 kbps

            BoardNo = (byte)cboBoard.SelectedIndex;
            CANBaud = (byte)cboBaudrate.SelectedIndex;

        }

        private void btnActive_Click(object sender, EventArgs e)
        {          
            if (IsActiveFlag)
            {
                CM100.CM100_CloseBoard(BoardNo);
            }

            sRet = CM100.CM100_ActiveBoard(BoardNo);
            if (sRet != 0)
            {
                ListStr = "CM100_ActiveBoard error, error code = " + sRet;
                listBox1.Items.Add(ListStr);
                return;
            }

            sRet = CM100.CM100_Init(BoardNo, PortNo);
            if (sRet != 0)
            {
                ListStr = "CM100_Init error, error code = " + sRet;
                listBox1.Items.Add(ListStr);
                return;
            }

            //Set AccCode=0x0, AccMask=0xFFFFFFFF, BT0=0x0, BT1=0x0
            sRet = CM100.CM100_ConfigWithoutStruct(BoardNo, PortNo, 0x0, 0xFFFFFFFF, CANBaud, 0x0, 0x0);
            if (sRet != 0)
            {
                ListStr = "CM100_ConfigWithoutStruct error, error code = " + sRet;
                listBox1.Items.Add(ListStr);
                return;
            }

            ListStr = "Board " + BoardNo + " is active with " + cboBaudrate.Text + " kbps.";
            listBox1.Items.Add(ListStr);

            IsActiveFlag = true;
            timer1.Enabled = true;
        }

        private void cboBoard_SelectedIndexChanged(object sender, EventArgs e)
        {
            BoardNo = (byte)cboBoard.SelectedIndex;
        }

        private void cboBaudrate_SelectedIndexChanged(object sender, EventArgs e)
        {
            CANBaud = (byte)cboBaudrate.SelectedIndex;
        }

        private void btnReceive_Click(object sender, EventArgs e)
        {
            short MsgCount = 0, i = 0;            
            byte CAN_Mode,CAN_Rtr, CAN_DLen;
            byte [] CAN_Data = new byte[8];
            uint CAN_Id,CAN_UpperTime, CAN_LowerTime;
            UInt64 CAN_Time;

            if (IsActiveFlag == true)
            {
                MsgCount = CM100.CM100_RxMsgCount(BoardNo, PortNo);
                if (MsgCount > 0)
                {
                    for (i = 0; i < MsgCount; i++)
                    {
                        sRet = CM100.CM100_ReceiveWithoutStruct(BoardNo, PortNo, out CAN_Mode, out CAN_Id, out CAN_Rtr, out CAN_DLen, CAN_Data, out CAN_UpperTime, out CAN_LowerTime);
                        if (sRet != 0)
                        {
                            ListStr = "CM100_ReceiveWithoutStruct error, error code = " + sRet;
                            listBox1.Items.Add(ListStr);
                        }
                        else
                        {
                            CAN_Time = (CAN_UpperTime * 2 ^ 32 + CAN_LowerTime) / 10;  //ms
                            ListStr = ListCount.ToString("D3") + ", Time = " + CAN_Time.ToString("D8") + " ms" + ", Mode=" + CAN_Mode.ToString() + ", ID=" + CAN_Id.ToString("X") +
                                        ", RTR=" + CAN_Rtr.ToString() + ", DLen=" + CAN_DLen.ToString();
                            if (CAN_Rtr == 0)
                            {
                                ListStr += ", Data=";
                                for (ushort j = 0; j < CAN_DLen; j++)
                                    ListStr += CAN_Data[j].ToString("X2") + " ";
                            }
                            listBox1.Items.Add(ListStr);
                            listBox1.SelectedIndex = listBox1.Items.Count - 1;

                            ListCount++;

                            if (ListCount > 100)
                            {
                                ListCount = 0;
                                listBox1.Items.Clear();
                            }
                        }

                        this.Refresh();
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ListCount = 0;
            listBox1.Items.Clear();
        }

        private void btnClearStatus_Click(object sender, EventArgs e)
        {
            if (IsActiveFlag == true)
            {
                sRet = CM100.CM100_ClearDataOverrun(BoardNo, PortNo);
                if (sRet != 0)
                {
                    ListStr = "CM100_ClearDataOverrun error, error code = " + sRet;
                    listBox1.Items.Add(ListStr);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            byte bStatus;

            if (IsActiveFlag == true)
            {
                sRet = CM100.CM100_Status(BoardNo, PortNo, out bStatus);
                if (sRet != 0)
                {
                    ListStr = "CM100_Status error, error code = " + sRet;
                    listBox1.Items.Add(ListStr);
                }
                else
                {
                    txtStatus.Text = bStatus.ToString("X2");
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            IsActiveFlag = false;
            timer1.Enabled = false;
            CM100.CM100_CloseBoard(BoardNo);
            this.Dispose();
        }

    }
}
