コード例 #1
0
ファイル: Form1.cs プロジェクト: szechyjs/pitbox
        public Form1()
        {
            InitializeComponent();
            mPacket = new Packet();
            Form1.CheckForIllegalCrossThreadCalls = false;

            if (ApplicationDeployment.IsNetworkDeployed)
                lblVersion.Text = "Version: " + ApplicationDeployment.CurrentDeployment.CurrentVersion;
            else
                lblVersion.Text = "Version: " + Application.ProductVersion;
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: szechyjs/pitbox
        public void ReadComm()
        {
            int bytes_available;
            UInt16 headerBytesRead = 0, payloadBytesRemain = 0;
            bool havePacket = false;

            byte[] prefix = {80, 66, 83, 80, 32, 118, 49, 58};
            var encoding = new ASCIIEncoding();
            while (true)
            {
                mPauseEvent.WaitOne(Timeout.Infinite);

                if (mShutdownEvent.WaitOne(0))
                    break;

                bytes_available = mSerialPort.BytesToRead;

                if (havePacket)
                    continue;

                if (headerBytesRead < 8)
                {
                    while (bytes_available > 0)
                    {
                        var next_read = mSerialPort.ReadByte();
                        var next_char = Convert.ToChar(next_read);
                        byte next_byte = (byte)next_char;
                        bytes_available--;

                        if (next_byte == prefix[headerBytesRead])
                        {
                            headerBytesRead++;
                            if (headerBytesRead == 8)
                            {
                                // Found start of packet, break
                                break;
                            }
                        }
                        else
                        {
                            // Wrong character in prefix; reset framing.
                            if (next_byte == prefix[0])
                            {
                                headerBytesRead = 1;
                            }
                            else
                            {
                                headerBytesRead = 0;
                            }
                        }
                    }
                }

                // Read the remainder of the header, if not yet found.
                if (headerBytesRead < 12)
                {
                    if (bytes_available < 4)
                    {
                        continue;
                    }
                    var ptype = new byte[2];
                    mSerialPort.Read(ptype, 0, 2);
                    mPacket.Type = BitConverter.ToUInt16(ptype, 0);
                    var payloadBytes = new byte[2];
                    mSerialPort.Read(payloadBytes, 0, 2);
                    payloadBytesRemain = BitConverter.ToUInt16(payloadBytes, 0);
                    bytes_available -= 4;
                    headerBytesRead += 4;

                    // Check that the length field is not bogus
                    if (payloadBytesRemain > 112)
                        continue;
                }

                if (bytes_available == 0 || headerBytesRead < 12)
                    continue;

                if (payloadBytesRemain > 0)
                {
                    int bytes_to_read = (payloadBytesRemain >= bytes_available) ? bytes_available : payloadBytesRemain;
                    var read_buff = new byte[bytes_to_read];
                    mSerialPort.Read(read_buff, 0, bytes_to_read);
                    mPacket.AppendByte(read_buff, bytes_to_read);
                    payloadBytesRemain -= (UInt16)bytes_to_read;
                    bytes_available -= bytes_to_read;
                }

                if (payloadBytesRemain > 0)
                    continue;

                if (!havePacket)
                {
                    if (bytes_available < 4)
                        continue;

                    var footer_bytes = new byte[4];
                    mSerialPort.Read(footer_bytes, 0, 4);

                    havePacket = true;
                }

                if (havePacket)
                {
                    var packet = mPacket.GetPacket();
                    textBox1.AppendText(encoding.GetString(packet, 0, 8));
                    textBox1.AppendText(" Payload length: " + BitConverter.ToInt16(packet, 10));
                    textBox1.AppendText(" Type: " + mPacket.Type + Environment.NewLine);

                    switch (mPacket.Type)
                    {
                        case 32:
                            UpdateConfigValues();
                            break;

                        case 01:
                            UpdateHardwareVersion();
                            mConnectionEstablished = true;
                            lblConnectionStatus.Text = "Connected";
                            break;

                        default:
                            break;
                    }

                    mPacket = new Packet();
                    havePacket = false;
                    headerBytesRead = 0;
                    payloadBytesRemain = 0;
                }
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: szechyjs/pitbox
        private void SendSetConfig()
        {
            var packet = new Packet();

            packet.Type = 32;         // 16 = 0x10, 32 = 0x20, 129 = 0x81
            var red = UInt16.Parse(txtRed.Text);
            var green = UInt16.Parse(txtGreen.Text);
            var delay = UInt16.Parse(txtDelay.Text);

            packet.AddTag(0x01, red);
            packet.AddTag(0x02, green);
            packet.AddTag(0x03, delay);
            packet.AddTag(0x04, 0); // Red mode
            packet.AddTag(0x05, 0); // Green mode

            SendPacket(packet);
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: szechyjs/pitbox
 private void SendPing()
 {
     var packet = new Packet();
     packet.Type = 129;
     SendPacket(packet);
 }
コード例 #5
0
ファイル: Form1.cs プロジェクト: szechyjs/pitbox
 private void SendPacket(Packet packet)
 {
     mSerialPortMutex.WaitOne();
     if (mSerialPort != null)
         if (mSerialPort.IsOpen)
         {
             var bytes = packet.GetPacket();
             mSerialPort.Write(bytes, 0, bytes.Length);
         }
     mSerialPortMutex.ReleaseMutex();
 }
コード例 #6
0
ファイル: Form1.cs プロジェクト: szechyjs/pitbox
 private void SendGetConfig()
 {
     var packet = new Packet();
     packet.Type = 16;
     SendPacket(packet);
 }