private void ShowNewForm(object sender, EventArgs e) { // Create a new instance of the child form. if (this.MdiChildren.Length == 0) { frmProj = new ProjetoLadder(); frmProj.MdiParent = this; frmProj.Show(); frmProj.Text = "Sem Nome"; } else { DialogResult _result = MessageBox.Show(RecursoVisual.STR_QUESTIONA_SALVAR_PROJETO.Replace("%%", frmProj.Text.Trim()).Trim(), "EditorLadder", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (_result == DialogResult.Yes) { frmProj.Close(); } } }
private void OpenFile(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); try { openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); } catch (SecurityException ex) { MessageBox.Show("Erro na segurança. Impossível continuar." + ex.Message + " " + ex.PermissionState); return; } openFileDialog.Filter = "Arquivos Ladder (*.xml;*.a43)|*.xml;*.a43|Arquivos XML (*.xml)|*.xml|Executável MSP430 (*.a43)|*.a43"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { string FileName = ""; FileName = openFileDialog.FileName; // TODO: Add code here to open the file. switch (FileName.Substring(FileName.Length - 4, 4).ToLower()) { case ".xml": try { // Using a FileStream, create an XmlTextReader. Stream fs = new FileStream(FileName, FileMode.Open); XmlReader reader = new XmlTextReader(fs); XmlSerializer serializer = new XmlSerializer(typeof(ProgramaBasico)); if (serializer.CanDeserialize(reader)) { Object o = serializer.Deserialize(reader); ((ProgramaBasico)o).StsPrograma = ProgramaBasico.StatusPrograma.ABERTO; frmProj = new ProjetoLadder((ProgramaBasico)o); frmProj.programa.PathFile = FileName; frmProj.MdiParent = this; frmProj.Show(); frmProj.SetText(); } fs.Close(); } catch (Exception ex) { MessageBox.Show("Erro ao ler o arquivo! " + ex.InnerException.Message, "Abrir arquivos ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; case ".a43": try { ModuloIntegracaoMSP430 p = new ModuloIntegracaoMSP430(); String strLido = p.ConvertHex2String(FileName); if (VerificaSenha(strLido)) { LerExecutavel(strLido, FileName.Substring(FileName.LastIndexOf(@"\") + 1, FileName.Length - FileName.LastIndexOf(@"\") - 1)); } } catch { MessageBox.Show("Formato desconhecido!", "Abrir arquivos ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; default: MessageBox.Show("Formato desconhecido!", "Abrir arquivos ...", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } } }
private void LerExecutavel(String DadosConvertidosChar, String strNomeProjeto) { List <int> lstCodigosLidos = new List <int>(); CodigosInterpretaveis guarda = CodigosInterpretaveis.NENHUM; if (DadosConvertidosChar.IndexOf("@laddermic.com") != -1) { Int32 intContaFim = 0; Int32 intIndiceLinha = 0; Int32 iNumOperandos = 0; EnderecamentoLadder _endLido; TipoEnderecamentoDispositivo _tpEndLido; Int32 _iIndiceEndLido = 0; /// Cria um programa novo vazio ProgramaBasico programa = new ProgramaBasico(); programa.StsPrograma = ProgramaBasico.StatusPrograma.NOVO; programa.Nome = strNomeProjeto; programa.dispositivo = new DispositivoLadder(1); programa.endereco.AlocaEnderecamentoIO(programa.dispositivo); programa.endereco.AlocaEnderecamentoMemoria(programa.dispositivo, programa.endereco.lstMemoria, TipoEnderecamentoDispositivo.DIGITAL_MEMORIA, 10); programa.endereco.AlocaEnderecamentoMemoria(programa.dispositivo, programa.endereco.lstTemporizador, TipoEnderecamentoDispositivo.DIGITAL_MEMORIA_TEMPORIZADOR, 10); programa.endereco.AlocaEnderecamentoMemoria(programa.dispositivo, programa.endereco.lstContador, TipoEnderecamentoDispositivo.DIGITAL_MEMORIA_CONTADOR, 10); intIndiceLinha = programa.InsereLinhaNoFinal(new LinhaCompleta()); for (int i = DadosConvertidosChar.IndexOf("@laddermic.com") + 15; i < DadosConvertidosChar.Length; i++) { guarda = (CodigosInterpretaveis)Convert.ToChar(DadosConvertidosChar.Substring(i, 1)); switch (guarda) { case CodigosInterpretaveis.NENHUM: intContaFim++; iNumOperandos = 0; break; case CodigosInterpretaveis.FIM_DA_LINHA: intContaFim++; iNumOperandos = 0; if ((CodigosInterpretaveis)Convert.ToChar(DadosConvertidosChar.Substring(i + 1, 1)) != CodigosInterpretaveis.NENHUM) { intIndiceLinha = programa.InsereLinhaNoFinal(new LinhaCompleta()); } break; //case CodigosInterpretaveis.INICIO_DA_LINHA: case CodigosInterpretaveis.CONTATO_NA: case CodigosInterpretaveis.CONTATO_NF: intContaFim = 0; iNumOperandos = 2; { SimboloBasico _sb = new SimboloBasico((CodigosInterpretaveis)guarda); //_sb.setOperando(0, programa.endereco.Find((TipoEnderecamentoDispositivo)Convert.ToChar(DadosConvertidosChar.Substring(i + 1, 1)), (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 2, 1)))); _tpEndLido = (TipoEnderecamentoDispositivo)Convert.ToChar(DadosConvertidosChar.Substring(i + 1, 1)); _iIndiceEndLido = (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 2, 1)); _endLido = programa.endereco.Find(_tpEndLido, _iIndiceEndLido); if (_endLido == null) { programa.dispositivo.lstBitPorta[_iIndiceEndLido - 1].TipoDefinido = _tpEndLido; programa.dispositivo.RealocaEnderecoDispositivo(); programa.endereco.AlocaEnderecamentoIO(programa.dispositivo); _endLido = programa.endereco.Find(_tpEndLido, _iIndiceEndLido); } _sb.setOperando(0, _endLido); i += 2; programa.linhas[intIndiceLinha].simbolos.Add(_sb); } break; case CodigosInterpretaveis.BOBINA_SAIDA: case CodigosInterpretaveis.RESET: intContaFim = 0; iNumOperandos = 2; { ListaSimbolo _lstSB = new ListaSimbolo(); _lstSB.Add(new SimboloBasico((CodigosInterpretaveis)guarda)); _tpEndLido = (TipoEnderecamentoDispositivo)Convert.ToChar(DadosConvertidosChar.Substring(i + 1, 1)); _iIndiceEndLido = (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 2, 1)); _endLido = programa.endereco.Find(_tpEndLido, _iIndiceEndLido); if (_endLido == null) { programa.dispositivo.lstBitPorta[_iIndiceEndLido - 1].TipoDefinido = _tpEndLido; programa.dispositivo.RealocaEnderecoDispositivo(); programa.endereco.AlocaEnderecamentoIO(programa.dispositivo); _endLido = programa.endereco.Find(_tpEndLido, _iIndiceEndLido); } _lstSB[_lstSB.Count - 1].setOperando(0, _endLido); i += 2; programa.linhas[intIndiceLinha].Insere2Saida(_lstSB); _lstSB.Clear(); } break; case CodigosInterpretaveis.PARALELO_INICIAL: case CodigosInterpretaveis.PARALELO_FINAL: case CodigosInterpretaveis.PARALELO_PROXIMO: intContaFim = 0; iNumOperandos = 0; programa.linhas[intIndiceLinha].simbolos.Add(new SimboloBasico((CodigosInterpretaveis)guarda)); break; case CodigosInterpretaveis.CONTADOR: intContaFim = 0; iNumOperandos = 3; { ListaSimbolo _lstSB = new ListaSimbolo(); _lstSB.Add(new SimboloBasico((CodigosInterpretaveis)guarda)); _lstSB[_lstSB.Count - 1].setOperando(0, programa.endereco.Find(TipoEnderecamentoDispositivo.DIGITAL_MEMORIA_CONTADOR, (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 1, 1)))); ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Contador.Tipo = (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 2, 1)); ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Contador.Preset = (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 3, 1)); _lstSB[_lstSB.Count - 1].setOperando(1, ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Contador.Tipo); _lstSB[_lstSB.Count - 1].setOperando(2, ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Contador.Preset); i += 3; programa.linhas[intIndiceLinha].Insere2Saida(_lstSB); _lstSB.Clear(); } break; case CodigosInterpretaveis.TEMPORIZADOR: intContaFim = 0; iNumOperandos = 4; { ListaSimbolo _lstSB = new ListaSimbolo(); _lstSB.Add(new SimboloBasico((CodigosInterpretaveis)guarda)); _lstSB[_lstSB.Count - 1].setOperando(0, programa.endereco.Find(TipoEnderecamentoDispositivo.DIGITAL_MEMORIA_TEMPORIZADOR, (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 1, 1)))); ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Temporizador.Tipo = (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 2, 1)); ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Temporizador.BaseTempo = (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 3, 1)); ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Temporizador.Preset = (Int32)Convert.ToChar(DadosConvertidosChar.Substring(i + 4, 1)); _lstSB[_lstSB.Count - 1].setOperando(1, ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Temporizador.Tipo); _lstSB[_lstSB.Count - 1].setOperando(2, ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Temporizador.Preset); _lstSB[_lstSB.Count - 1].setOperando(4, ((EnderecamentoLadder)_lstSB[_lstSB.Count - 1].getOperandos(0)).Temporizador.BaseTempo); i += 4; programa.linhas[intIndiceLinha].Insere2Saida(_lstSB); _lstSB.Clear(); } break; } /// fim dos códigos if (intContaFim >= 2) { /// grava os dados lidos do codigo intepretavel ModuloIntegracaoMSP430 p = new ModuloIntegracaoMSP430(); p.CriaArquivo("codigosinterpretaveis.txt", DadosConvertidosChar.Substring(DadosConvertidosChar.IndexOf("@laddermic.com"), i - DadosConvertidosChar.IndexOf("@laddermic.com") + 1)); /// força saída do loop i = DadosConvertidosChar.Length; } } frmProj = new ProjetoLadder(programa); frmProj.MdiParent = this; frmProj.Show(); frmProj.SetText(); } else { MessageBox.Show("O arquivo não foi reconhecido pelo sistema!", "Abrir Arquivos ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } }