internal XBeeTxTransaction(XBeeTrLayer _layer, XBeePDU _pdu, int timeout) { Stack._created_transactions++; layer = _layer; pdu = _pdu; id = pdu.Sequence; lost_timer = new Timer(TimedOut, null, timeout, Timeout.Infinite); }
public void RemoteATCommand(ushort address, string command) { lock (uartLocker) { if (uart == null || Estado == Estados.PUERTO_CERRADO) { return; } // to do: el 0x00 al final de los strings. var data = Encoding.ASCII.GetBytes(command); var pdu = new XBeePDU(address, 0x17, data); tr_layer.Create(pdu, active_tx_timeout); var buf = pdu.ToByteArray(); try { STrace.Debug(GetType().FullName, "REMOTE AT"); Stack._writed_bytes += buf.GetLength(0); Write(buf); return; } catch (UnauthorizedAccessException e) { STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; Stack._write_unautorized_access_exceptions++; return; } catch (IOException e) { STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; Stack._write_io_exceptions++; return; } catch (InvalidOperationException e) { STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; Stack._invalid_operation_exception++; return; } catch (TimeoutException e) { STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; Stack._timeout_exceptions++; return; } } }
internal void Create(XBeePDU pdu, int timeout) { #if ENABLE_XBEE_TR_LAYER lock (this) { pdu.Sequence = NextSequence; var tr = new XBeeTxTransaction(this, pdu, timeout); STrace.Debug(GetType().FullName, String.Format("Creando Transaccion id={0} timeout={1}", pdu.Sequence, timeout)); Stack._active_transactions++; tr_layer.Add(pdu.Sequence, tr); } #else pdu.Sequence = 0; #endif }
private void ATCommand(byte[] data) { lock (uartLocker) { if (uart == null || Estado == Estados.PUERTO_CERRADO) { return; } // to do: el 0x00 al final de los strings. var pdu = new XBeePDU(0, 0x08, data); tr_layer.Create(pdu, active_tx_timeout); var buf = pdu.ToByteArray(); try { STrace.Debug(GetType().FullName, "Enviando AT "); Stack._writed_bytes += buf.GetLength(0); Write(buf); return; } catch (UnauthorizedAccessException) { STrace.Debug(GetType().FullName, "Exception: UnauthorizedAccessException."); Estado = Estados.PUERTO_CERRADO; Stack._write_unautorized_access_exceptions++; return; } catch (IOException) { STrace.Debug(GetType().FullName, "Exception: IOException."); Estado = Estados.PUERTO_CERRADO; Stack._write_io_exceptions++; return; } catch (InvalidOperationException) { STrace.Debug(GetType().FullName, "Exception: InvalidOperationException."); Estado = Estados.PUERTO_CERRADO; Stack._invalid_operation_exception++; return; } catch (TimeoutException) { STrace.Debug(GetType().FullName, "Exception: TimeoutException."); Estado = Estados.PUERTO_CERRADO; Stack._timeout_exceptions++; return; } } }
public bool Send(byte[] data, ushort address) { lock (uartLocker) { try { if (uart == null || Estado == Estados.PUERTO_CERRADO) { return(false); } var pdu = new XBeePDU(address, 0x01, data); // tr_layer.Create(pdu, active_tx_timeout); var buf = pdu.ToByteArray(); STrace.Debug(GetType().FullName, String.Format("TX Request 16 bits, Enviando {0} bytes. (sin cabeceras)", buf.GetLength(0))); Stack._writed_bytes += buf.GetLength(0); Write(buf); //Write(buf); return(true); } catch (UnauthorizedAccessException e) { STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; Stack._write_unautorized_access_exceptions++; return(false); } catch (IOException e) { STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; Stack._write_io_exceptions++; return(false); } catch (Exception e) { STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; Stack._write_unknow_exceptions++; return(false); } } }
private void CompleteReceive(XBeePDU pdu) { try { STrace.Debug(GetType().FullName, "Recibo PDU"); if (pdu.Decode()) { STrace.Debug(GetType().FullName, String.Format("Recibo PDU - Decodificada APIID={0:X}", pdu.IdAPI)); if (pdu.IdAPI == 0x88) // AT Command Response. { var tr = tr_layer.Get(pdu.Sequence); // Respuestas a comandos AT eliminan la transaccion.- if (tr != null) { tr.End(); tr_layer.Remove(pdu.Sequence); } // A que comando AT se referia? if (pdu.Data[0] == 'M' && pdu.Data[1] == 'Y') { test_failure = 0; MyXbeeAddress = (ushort)((pdu.Data[2] << 8) + pdu.Data[3]); // Aqui determinamos ENLINEA Estado = Estados.MODULO_ENLINEA; } else if (pdu.Data[0] == 'N' && pdu.Data[1] == 'D') { var cursor = 2; while ((pdu.Data.GetLength(0) - cursor) > 0) { var nodo = new XBeeNode { Address = ((ushort)(pdu.Data[cursor++] << 8)) }; nodo.Address += pdu.Data[cursor]; cursor += 9; nodo.Signal = (char)pdu.Data[cursor++]; var id = new byte[20]; var chr = pdu.Data[cursor++]; var idpos = 0; while (chr != 0) { id[idpos++] = chr; chr = pdu.Data[cursor++]; } nodo.Id = Encoding.ASCII.GetString(id, 0, idpos); nodo.Trace("NODO DETECTADO:"); UpdateNode(nodo); } } } if (pdu.IdAPI == 0x89) { var tr = tr_layer.Get(pdu.Sequence); if (tr != null) { if (pdu.Ack) { tr.End(); } else { tr.Cancel(); } tr_layer.Remove(pdu.Sequence); } else { Stack._acks_too_late++; } } else if (pdu.IdAPI == 0x81) { if (PDURecibida != null) { PDURecibida(this, pdu); } return; } else if (pdu.IdAPI == 0x8A) { if (HardwareStatus != null) { HardwareStatus(this, pdu); } return; } } else { STrace.Debug(GetType().FullName, "XBEE: Recibo PDU - Error al decodificar"); } } catch (Exception e) { STrace.Exception(GetType().FullName, e); } }
private void DataReceived(object sender, SerialDataReceivedEventArgs args) { //Stack.DebugHigh("Datos Recibidos"); lock (uartLocker) { // este metodo deserializa el puerto y convierte los paquetes recibidos // en bloques de datos, en PDUs. if (uart == null) { STrace.Debug(GetType().FullName, "DataReceived: UART Invalido, amortiguador."); return; } try { while (uart.BytesToRead > 0) { // leo un caracter. var b = Convert.ToByte(uart.ReadByte()); Stack._readed_bytes++; last_received_data = DateTime.Now; var unescape_prev = false; // Secuencias de Escape if (b == 0x7D) { unescape_next = true; continue; } if (unescape_next) { switch (b) { case 0x5E: b = 0x7E; break; case 0x5D: b = 0x7D; break; case 0x31: b = 0x11; break; case 0x33: b = 0x13; break; default: throw new Exception("XBEEAPI: secuencia de escape incorrecta."); } unescape_next = false; unescape_prev = true; } if (b == 0x7E && !unescape_prev) { if (active_pdu != null) { Stack._decoder_unexpected_x7E++; CompleteReceive(active_pdu); } active_pdu = new XBeePDU(); // necesita al menos de 4 bytes para dar true... var status = active_pdu.PushReadedByte(b); switch (status) { case XBeePDU.PushStatus.DISPATCH: CompleteReceive(active_pdu); active_pdu = null; break; case XBeePDU.PushStatus.RESET: active_pdu = null; break; default: break; } } else { if (active_pdu == null) { Stack._padding_bytes++; } else { var status = active_pdu.PushReadedByte(b); switch (status) { case XBeePDU.PushStatus.DISPATCH: CompleteReceive(active_pdu); active_pdu = null; break; case XBeePDU.PushStatus.RESET: active_pdu = null; break; default: break; } } } } } catch (UnauthorizedAccessException e) { Stack._read_unautorized_access_exceptions++; STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; return; } catch (IOException e) { Stack._read_io_exceptions++; STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; return; } catch (Exception e) { Stack._read_unknow_exceptions++; STrace.Exception(GetType().FullName, e); Estado = Estados.PUERTO_CERRADO; return; } } }