Example #1
0
        public void GoToPosition(int X, int Y, int Z)
        {
            //string send_message;
            if (!has_started_port)
            {
                return;
            }
            if (is_reading_from_file)
            {
                return;
            }
            int delta_x = X - position_x;
            int delta_y = Y - position_y;
            int delta_z = Z - position_z;
            //send_message = string.Format("X{0}Y{1}Z{2}", delta_x, delta_y, delta_z);
            MotionMessage send_message = new MotionMessage(delta_x, delta_y, delta_z, step_time, is_punching);
            string        ss           = send_message.GetSerialString();

            Rhino.RhinoApp.WriteLine(ss);
            lock (send_message_list)
            {
                send_message_list.AddLast(send_message);
            }
            //should_send = true;
        }
Example #2
0
        public void Read()
        {
            while (_continue && has_started_port)
            {
                //Rhino.RhinoApp.WriteLine("serial port read thread working");
                lock (send_message_list)
                {
                    if (send_message_list.Count > 0)
                    {
                        if (has_reached && turntable.has_reached)
                        {
                            //string send_message = send_message_list.First.Value;
                            MotionMessage send_message = send_message_list.First.Value;

                            /*
                             * if (send_message[0] == 'x')
                             * {
                             *
                             *  LinkedListNode<string> send_message_list_node = send_message_list.First;
                             *  for (int i = 0; i < send_message_list.Count; i++)
                             *  {
                             *      Rhino.RhinoApp.Write("{0}, ", send_message_list_node.Value);
                             *      send_message_list_node = send_message_list_node.Next;
                             *  }
                             *  Rhino.RhinoApp.WriteLine(" ");
                             *
                             *  watch.Stop();
                             *  Rhino.RhinoApp.WriteLine("order queueing time: {0}", watch.Elapsed);
                             *  watch.Reset();
                             * }
                             */

                            //send message to arduino
                            send_message_list.RemoveFirst();
                            _serialPort.WriteLine(send_message.GetSerialString());
                            if (send_message.GetSerialString() != "G")
                            {
                                Rhino.RhinoApp.WriteLine(send_message.GetSerialString());
                            }
                            turntable.SetRotation(send_message.GetRotateIngerval(), send_message.GetRotateAngle());

                            string sent_str = send_message.GetSerialString();
                            if (sent_str[0] == 'X' || sent_str[0] == 'Y' || sent_str[0] == 'Z')
                            {
                                has_reached = false;
                            }
                        }
                        else
                        {
                            string send_message = send_message_list.First.Value.GetSerialString();
                            if (send_message[0] == 'G')
                            {
                                send_message_list.RemoveFirst();
                            }
                        }
                        //should_send = false;
                    }
                }



                //bool keep_read = true;
                string message = null;
                //while (keep_read)
                //{
                try
                {
                    message = _serialPort.ReadLine();
                    //Console.WriteLine(message);
                    //keep_read = false;
                }
                catch (TimeoutException) {     //keep_read = true;
                }
                //}
                if (message != null)
                {
                    string submessage = message.Substring(1);
                    switch (message[0])
                    {
                    case 'E':
                    {
                        has_reached_end = true;
                        has_reached     = true;
                        _serialPort.WriteLine("G");
                        break;
                    }

                    case 'F':
                    {
                        has_reached = true;
                        break;
                    }

                    case 'P':
                    {
                        int X, Y, Z;
                        ConvertPosition(submessage, out X, out Y, out Z);
                        position_x = X;
                        position_y = Y;
                        position_z = Z;
                        break;
                    }

                    case 'R':
                    {
                        int X, Y, Z;
                        ConvertPosition(submessage, out X, out Y, out Z);
                        range_x = X;
                        range_y = Y;
                        range_z = Z;
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }

                Thread.Sleep(1);
            }
        }