コード例 #1
0
 private void ScanArduino_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     try
     {
         ArduinoMessage += ArduinoSerialPort.ReadExisting();
         ArduinoReadText.Invoke(new MethodInvoker(delegate
         {
             ArduinoReadText.Text += ArduinoMessage;
         }));
         if (ArduinoMessage.Contains("\n"))
         {
             measurementData.Add(Convert.ToInt32(ArduinoMessage));
             ArduinoMessage = "";
             ScanProgress.Invoke(new MethodInvoker(delegate
             {
                 ScanProgress.Value = 1 / scanLength;
             }));
             if (measurementData.Count < scanLength)
             {
                 SDMC1SerialPort.Write("+1\r");
             }
             else
             {
                 SDMC1SerialPort.DataReceived   -= ScanSDMC1_DataReceived;
                 SDMC1SerialPort.DataReceived   += SDMC1SerialPort_DataReceived;
                 ArduinoSerialPort.DataReceived -= ScanArduino_DataReceived;
                 ArduinoSerialPort.DataReceived += ArduinoSerialPort_DataReceived;
                 SDMC1SerialPort.Write("R" + scanStartPosition + "\r");
                 HomeButton.Invoke(new MethodInvoker(delegate
                 {
                     HomeButton.Enabled = true;
                 }));
                 ScanButton.Invoke(new MethodInvoker(delegate
                 {
                     ScanButton.Enabled = true;
                 }));
             }
             moves++;
         }
     }
     catch (Exception error)
     {
         MessageText.Invoke(new MethodInvoker(delegate
         {
             MessageText.Text = error.Message;
         }));
         ArduinoMessage = "";
     }
 }
コード例 #2
0
        private void HomeSDMC1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int StartPosition = -1;
            int EndPosition   = -1;

            try
            {
                SDMC1Message += SDMC1SerialPort.ReadExisting();
                if (SDMC1Message.Contains("\r"))
                {
                    //if it says it just moved, check the limit switches
                    if (SDMC1Message.Contains("-1"))
                    {
                        SDMC1Message = "";
                        SDMC1SerialPort.Write("]\r");
                    }
                    //if it reports the limit switch, check if it's home, if not move
                    else if (SDMC1Message.Contains("]"))
                    {
                        // get only the numeric part of the input by starting at the first character, looking for the first digit, then looking for the first non-digit after that
                        for (int i = 0; i < SDMC1Message.Length; i++)
                        {
                            if (Char.IsDigit(SDMC1Message[i]))
                            {
                                StartPosition = i - 1;
                                break;
                            }
                        }
                        for (int i = StartPosition + 1; i < SDMC1Message.Length; i++)
                        {
                            if (!Char.IsDigit(SDMC1Message[i]))
                            {
                                EndPosition = i - 1;
                                break;
                            }
                        }
                        if (StartPosition > 0 && EndPosition > 0)
                        {
                            SDMC1Message = SDMC1Message.Substring(StartPosition, EndPosition);
                        }
                        else if (StartPosition > 0)
                        {
                            SDMC1Message = SDMC1Message.Substring(StartPosition);
                        }


                        Boolean home = (Convert.ToInt32(SDMC1Message) & 128) > 0;
                        if (!home && moves < homeDistance)
                        {
                            SDMC1SerialPort.Write("-1\r");
                            moves++;
                        }
                        else
                        {
                            if (moves > homeDistance - 1)
                            {
                                MessageText.Invoke(new MethodInvoker(delegate {
                                    MessageText.Text = "Over allowed home distance";
                                }));
                            }
                            SDMC1SerialPort.DataReceived -= HomeSDMC1_DataReceived;
                            SDMC1SerialPort.DataReceived += SDMC1SerialPort_DataReceived;
                            HomeButton.Invoke(new MethodInvoker(delegate {
                                HomeButton.Enabled = true;
                            }));
                            ScanButton.Invoke(new MethodInvoker(delegate {
                                ScanButton.Enabled = true;
                            }));
                            SDMC1SerialPort.Write("O0\r");
                        }
                        SDMC1Message = "";
                    }
                }
            }
            catch (Exception error)
            {
                SDMC1SerialPort.DataReceived -= HomeSDMC1_DataReceived;
                SDMC1SerialPort.DataReceived += SDMC1SerialPort_DataReceived;
                MessageText.Invoke(new MethodInvoker(delegate {
                    MessageText.Text = error.Message + " home 2 " + StartPosition + ", " + EndPosition;
                }));
                SDMC1Message = "";
            }
        }