예제 #1
0
 private void NextCommand(MotorState ms)
 {
     CurrentLine++;
     if (CurrentLine < Commands.Length)
     {
         CncController.SendCommand(CurrentCommand);
         LastSended = CurrentCommand;
     }
     else
     {
         Stop();
     }
 }
예제 #2
0
 public static MotorState Deserialize(byte[] data)
 {
     if (data == null) return null;
     if (data.Length >= 33)
     {
         MotorState obj = new MotorState();
         obj.date = DateTime.Now;
         obj.address = data[0];
         obj.command = data[1];
         obj.state = data[2];
         obj.line = MotorState.loadInt(data, 3);
         obj.x = loadInt(data, 7);
         obj.y = loadInt(data, 11);
         obj.z = loadInt(data, 15);
         obj.xLimit = loadInt(data, 19);
         obj.yLimit = loadInt(data, 23);
         obj.zLimit = loadInt(data, 27);
         obj.stateA = data[31];
         obj.stateB = data[32];
         return obj;
     }
     return null;
 }
예제 #3
0
 public static void ProcessMessage(byte[] data, SerialManager manager)
 {
     MotorState obj = MotorState.Deserialize(data);
     WriteLog(obj);
     if (obj != null)
     {
         /*if (!initialized && LastState != null)
         {
             if (LastState.x != obj.x || LastState.y != obj.y || LastState.z != obj.z)
             {
                 Uart.SendCommand(MotorCommand.Rebase(LastState.x, LastState.y, LastState.z));
             }
         }*/
         if (OnMessage != null)
         {
             OnMessage(obj);
         }
         LastState = obj;
         /*StreamWriter writer = new StreamWriter(cfgPath + "\\config.json", false, Encoding.UTF8);
         writer.WriteLine(Uart.PortName);
         writer.WriteLine(JsonConvert.SerializeObject(obj));
         writer.Close();*/
         initialized = true;
         
     }
     else
     {
         if (data.Length == 1 && (data[0] >= 50 || data[0] < 100))
         {
             if (LastState != null)
             {
                 //SendCoordCommand(CoordMotorCommand.Rebase(LastState.x, LastState.y, LastState.z));
             }
         }
     }
 }
예제 #4
0
 private void CncControllerOnOnMessage(MotorState obj)
 {
     if (state != CncProgramState.NotStarted && state != CncProgramState.Aborted)
     {
         if (obj.Command == CommandType.Stop)
         {
             Abort();
             return;
         }
         if (obj.line > 0)
         {
             switch (obj.State)
             {
                 case CncState.Completed:
                     NextCommand(obj);
                     break;
                 case CncState.Error:
                 case CncState.Aborted:
                     Stop();
                     break;
             }
         }
     }
 }
예제 #5
0
 private static void WriteLog(MotorState obj)
 {
     StreamWriter writer = new StreamWriter("log.txt", true, Encoding.UTF8);
     writer.WriteLine(obj.ToString());
     writer.Close();
 }
예제 #6
0
 public static void Init(SerialAddressedManager serialPort, string cfgPath, byte cncAddress)
 {
     CncController.cfgPath = cfgPath;
     CncAddress = cncAddress;
     Uart = serialPort;
     Uart.OnReceive += ProcessMessage;
     if (!File.Exists(cfgPath + "\\config.json"))
     {
         StreamWriter writer = new StreamWriter(cfgPath + "\\config.json", false, Encoding.UTF8);
         writer.Close();
         GetStateAsync();
         return;
     }
     StreamReader reader = new StreamReader(cfgPath + "\\config.json");
     var cfgStr = reader.ReadToEnd();
     reader.Close();
     if (cfgStr.Length > 0)
     {
         LastState = JsonConvert.DeserializeObject<MotorState>(cfgStr);
     }
     else
     {
         LastState = new MotorState();
     }
     initialized = true;
     
 }
예제 #7
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            context.Response.AddHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
            context.Response.AddHeader("Access-Control-Request-Header", "X-Prototype-Version, x-requested-with");
            context.Response.AddHeader("Access-Control-Allow-Origin", "*");
            //context.Response.Headers.Remove("content-length");
            context.Response.Flush();
            CncController.OnMessage += Uart_OnMessage;
            CncProgram.OnCommand += Uart_OnCommand;
            CncProgram.OnStateChange += new ProgramStateHandler(Program_OnStateChange);
            CncController.GetStateAsync();
            if (CncController.LastCommand != null) OutCommand = CncController.LastCommand;
            ProgramState = CncProgram.State;
            var timeout = 10000;
            while (timeout > 0 && context.Response.IsClientConnected)
            {
                Thread.Sleep(100);
                timeout--;
                List<string> states = new List<string>();
                if (OutCommand != null)
                {
                    states.Add(OutCommand.ToString());
                    OutCommand = null;
                }
                if (ProgramState != null)
                {
                    states.Add("{\"state\":\"" + ProgramState.Value.ToString() + "\", \"line\":" + CncProgram.CurrentLine + ",\"type\" : \"program-state\"}");
                    ProgramState = null;
                }
                if (StateMessage != null)
                {
                    states.Add(StateMessage.ToString());
                    StateMessage = null;
                }
                if (states.Count > 0)
                {
                    context.Response.Write("[" + String.Join(",", states.ToArray()) + "]");
                    context.Response.Flush();
                }
            }
            /*            while (Uart.LastState == null)
                        {
                            var dt = DateTime.Now;
                            Uart.SendStateCommand();
                            var timeout = 2000;
                            while ((Uart.LastState == null || Uart.LastState.date < dt) && timeout > 0)
                            {
                                Thread.Sleep(200);
                                timeout -= 200;
                            }
                            if (Uart.LastState != null && timeout > 0)
                            {
                                context.Response.Write(Uart.LastState.ToString());
                            }
                            else
                            {
                                context.Response.StatusCode = 504;
                                context.Response.Write(" ");
                            }
                            return;
                        }
                        if (context.Request.HttpMethod == "GET")
                        {
                            var ld = DateTime.Now;
                            if (context.Request["lastdate"] != null)
                            {
                                ld = DateTime.Parse(context.Request["lastdate"]);
                                if (ld >= Uart.LastState.date && context.Request["wait"] == null)
                                {
                                    return;
                                }
                            }
                            if (context.Request["wait"] != null)
                            {
                                if (context.Request["ping"] != null)
                                {
                                    Uart.SendStateCommand();
                                }
                                var timeout = 2000;
                                while ((Uart.LastState == null || Uart.LastState.date < ld) && timeout > 0)
                                {
                                    Thread.Sleep(200);
                                    timeout -= 200;
                                }
                                if (Uart.LastState != null && timeout > 0)
                                {
                                    context.Response.Write(Uart.LastState.ToString());
                                }
                                else
                                {
                                    context.Response.StatusCode = 504;
                                }
                                return;
                            }
                            context.Response.Write(Uart.LastState.ToString());
                        }
                        if (context.Request.HttpMethod == "POST")
                        {

                        }*/
        }
예제 #8
0
 void Uart_OnMessage(MotorState state)
 {
     StateMessage = state;
 }
예제 #9
0
 void UART_OnMessage(MotorState state)
 {
     try
     {
         Invoke(new Action<MotorState>(UartMessage), state);
     }
     catch (Exception e)
     {
     }
 }
예제 #10
0
 protected void UartMessage(MotorState dState)
 {
     SetState(dState);
     Program.HttpServer.Send(dState.ToString());
 }
예제 #11
0
        protected void SetState(MotorState dState)
        {
            lblCommand.ForeColor = Color.Black;
            if (dState.Command >= CommandType.Null && dState.Command <= CommandType.Error)
            {
                if (dState.state == 0 || dState.state == 9)
                {
                    lblX.Text = dState.x + "";
                    lblY.Text = dState.y + "";
                    lblZ.Text = dState.z + "";
                    if (dState.state == 9)
                    {
                        lblProgram.Text = "INITIALIZED " + dState.line;
                        lblProgram.ForeColor = Color.DarkGoldenrod;
                    }
                    else
                    {
                        lblProgram.Text = "IDLE";
                        lblProgram.ForeColor = Color.Gray;
                    }

                }
                if (dState.state == 1)
                {
                    if (dState.Command != CommandType.State)
                    {
                        lblCommand.Text = dState.Command.ToString();
                        lblProgram.Text = "(WORKING)";
                    }
                    lblX.Text = dState.x + " -> " + dState.xLimit;
                    lblY.Text = dState.y + " -> " + dState.yLimit;
                    lblZ.Text = dState.z + " -> " + dState.zLimit;
                }
                if (dState.state == 2)
                {
                    if (dState.Command != CommandType.State)
                    {
                        lblCommand.Text = dState.Command.ToString();
                        lblProgram.Text = "(STOP)";
                    }
                    lblX.Text = dState.x + " - " + dState.xLimit;
                    lblY.Text = dState.y + " - " + dState.yLimit;
                    lblZ.Text = dState.z + " - " + dState.zLimit;
                }
                if (dState.state == 3)
                {
                    lblCommand.Text = dState.Command.ToString();
                    lblCommand.ForeColor = Color.Orange;
                    lblProgram.Text = "(PAUSE) " + dState.line;
                    lblX.Text = dState.x + " - " + dState.xLimit;
                    lblY.Text = dState.y + " - " + dState.yLimit;
                    lblZ.Text = dState.z + " - " + dState.zLimit;
                    //log.AppendText(DateTime.Now.ToString("HH:mm:ss") + "<" + format + "\n");
                }
                if (dState.state == 4)
                {
                    lblCommand.Text = dState.Command.ToString();
                    lblCommand.ForeColor = Color.Red;
                    lblProgram.Text = "(ERROR) " + dState.line;
                    lblX.Text = dState.x + " - " + dState.xLimit;
                    lblY.Text = dState.y + " - " + dState.yLimit;
                    lblZ.Text = dState.z + " - " + dState.zLimit;
                    //log.AppendText(DateTime.Now.ToString("HH:mm:ss") + "<" + format + "\n");
                }
                point = new Point(dState.x/5, dState.y/5);
                color = Color.Green;
                //pBox.Refresh();
                lblA.Text = dState.stateA.ToString();
                lblB.Text = dState.stateB.ToString();
            }
            else
            {
                /*if (dState.Command == CommandType.Internal)
                {
                    lblProgram.Text = "CONTACT " + dState.state;
                }*/
            }
            
        }