public bool SendCommand(sCOMMAND Comando) { try { if (this.m_PortOpened && mySerialPort.IsOpen) { this.mySerialPort.Write(AUX_Functions.StructureToByteArray(Comando), 0, Comando.ParamSize + 5); return(true); } else { return(false); } } catch (Exception ex) { return(false); } }
//private bool STX_01_ctrl, STX_02_ctrl, ETX_01_ctrl, ETX_02_ctrl; public SerialPort_Control() { mySerialPort = new SerialPort(); mySerialPort.BaudRate = 250000; mySerialPort.Parity = Parity.None; mySerialPort.StopBits = StopBits.One; mySerialPort.DataBits = 8; mySerialPort.Handshake = Handshake.None; mySerialPort.ReadTimeout = 50; mySerialPort.ReadBufferSize = 200000; mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); this.RX_Buffer = new byte[CONST.BUFFER_SIZE_RX]; this.RXptn = 0; this.RX_COMMAND = new sCOMMAND(); this.RX_COMMAND.Processed = true; this.RX_INFO = new sCOMMAND(); this.RX_INFO.Processed = true; this.RX_Status = enumRX_Status.WAITING_STX_1; this.m_PortOpened = false; }
private void Ciclo() { DEFS.sCOMMAND TXComando = new sCOMMAND(); while (!this.PararCiclo) { try { if (this.SerialPort.PortOpened) { switch (this.EstadoCiclo) { case enumCiclo.WAIT_COMAND: if (Program.TX_Commands.TryDequeue(out TXComando)) { //NEW COMMAND AUX_Functions.ShowDebugInfo(false, "PROCESANDO COMANDO: " + TXComando.CMD.ToString()); this.EstadoCiclo = enumCiclo.SEND_COMMAND; } break; case enumCiclo.SEND_COMMAND: if (this.SerialPort.SendCommand(TXComando)) { this.T_EsperaACK.Stop(); this.T_EsperaACK.Reset(); this.T_EsperaACK.Start(); this.EstadoCiclo = enumCiclo.WAIT_ACK; } else { AUX_Functions.ShowDebugInfoERROR(false, "ENVIANDO COMANDO: " + TXComando.CMD.ToString()); this.EstadoCiclo = enumCiclo.WAIT_COMAND; } break; case enumCiclo.WAIT_ACK: if (this.T_EsperaACK.Alcanzado) { this.SerialPort.Reset_RX_STATUS(); AUX_Functions.ShowDebugInfoERROR(false, "TIEMOUT ESPERANDO ACK COMANDO: " + TXComando.CMD.ToString()); this.EstadoCiclo = enumCiclo.WAIT_COMAND; } else { if (this.SerialPort.ACK_Received) { //Comprobar coherencia ComandoEnviado <=> ACK_ComandoRecibido if (TXComando.CMD == (0xFF - this.SerialPort.RX_COMMAND.CMD)) //AÑADIR VERIFICACIÓN TAMAÑO PARAMETROS Y ACK { //Proceder a procesar los datos recibidos } else { AUX_Functions.ShowDebugInfoERROR(false, "ENVIADO COMANDO: " + TXComando.CMD.ToString() + " Recibido ACK: " + (0xFF - this.SerialPort.RX_COMMAND.CMD).ToString()); } this.EstadoCiclo = enumCiclo.WAIT_COMAND; } } break; }// END SWITCH //PROCESS INFO PACKETS this.ProcessInfoPackets(); } Thread.Sleep(10); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); this.EstadoCiclo = enumCiclo.WAIT_COMAND; } } //END WHILE } //END FUNCIÓN Ciclo