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

namespace TCPClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public  byte[] GetByteFromInt16(Int16 _int16)
        {
            char[] cc = new char[1] { '.' };
            string[] sp;
            byte[] _bytes = new byte[2];
            sp = Convert.ToString(_int16 / Math.Pow(2, 8)).Split(cc);
            _bytes[0] = Convert.ToByte(sp[0]);
            _bytes[1] = Convert.ToByte(_int16 & 255);
            return _bytes;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
            byte[] sd = new byte[12];
            char[] cc = new char[1]{'-'};
            string[] split;
            if (textBox1.Text.Length < 1) return;
            byte[] temp = new byte[2];
            try
            {
                split = textBox1.Text.Split(cc);
                for (int i = split.GetLowerBound(0); i <= split.GetUpperBound(0); i++)
                {
                    temp = GetByteFromInt16(Convert.ToInt16(split[i]));
                    sd[2*i] = temp[1];
                    sd[2*i+1] = temp[0];
                }
                
                if (CClientTCPIP.Send("192.168.1.100",1000,sd)==false)
                {
                    label1.Text = CClientTCPIP.TcpError;
                }
                timer1.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.Message+CClientTCPIP.TcpError);
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                textBox2.Text = "";
                byte[] bb = null;
                bb = CClientTCPIP.Receive("192.168.1.100", 1000);
                bb = CClientTCPIP.ByteData;
                CClientTCPIP.ByteData = null;
                if (bb != null)
                {
                    if (bb[0] == 82)//accpet 0 yes 1 no
                    {
                        this.label2.Text = bb[0].ToString() + bb[4].ToString() + bb[2].ToString();
                    }
                    if (bb[0] == 70)//over 
                    {
                        this.label4.Text = bb[0].ToString() + bb[2].ToString();
                    }
                    if (bb[0] == 71)//start
                    {
                        this.label3.Text = bb[0].ToString() + bb[2].ToString();
                    }
                    for (int i = 0; i <= 5; i++)
                    {
                        textBox2.Text = textBox2.Text + bb[i].ToString();
                    }
                }
                timer1.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + CClientTCPIP.TcpError);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = CClientTCPIP.TcpError;
            button2_Click(sender, e);
        }

        
    }
}