Error receiving bytes from u-controller PIC32

77 views
Skip to first unread message

Adrian Rendón Nava

unread,
Jul 12, 2012, 1:06:50 PM7/12/12
to xbee-api
Hi to all,

I'm building an application in which on one hand, i send data to an
Xbee transceiver connected to a PC from a java program. On the other
side i have the other transceiver connected via a UART port to a u-
controller PIC32. The first thing i want to do is an echo function. I
have already proved this with the X-CTU software provided by Digi and
it works fine but when I try it with my java program I do receive some
bytes but an error arises.

I've tried to echo the character I receive from the PC, to send a
fixed value, I added a delay but nothing seems to work.

I paste my java code and my interrupt function code from the u-
controller for your consideration. Any help will be much appreciated!
Thank you.

/* This is the error i get from Eclipse console */

Stable Library
=========================================
Native lib Version = RXTX-2.1-7.mw1
Java lib Version = RXTX-2.1-7
WARNING: RXTX Version mismatch
Jar version = RXTX-2.1-7
native lib Version = RXTX-2.1-7.mw1
[2012-07-12 12:03:57,993] [AWT-EventQueue-0] [INFO]
[com.rapplogic.xbee.RxTxSerialComm] sending packet to XBee 0x78
[2012-07-12 12:03:58,542] [Thread-3] [DEBUG]
[com.rapplogic.xbee.RxTxSerialComm] serialEvent: 1 bytes available
[2012-07-12 12:03:58,546] [Thread-3] [ERROR]
[com.rapplogic.xbee.RxTxSerialComm] Error in handleSerialData method
java.lang.NullPointerException
at com.rapplogic.xbee.RxTxSerialComm.writeData(RxTxSerialComm.java:
261)
at com.rapplogic.xbee.RxTxSerialComm.getPacket(RxTxSerialComm.java:
216)
at com.rapplogic.xbee.RxTxSerialComm.serialEvent(RxTxSerialComm.java:
243)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
[2012-07-12 12:03:58,553] [Thread-3] [DEBUG]
[com.rapplogic.xbee.RxTxSerialComm] Ignoring serial port event type:
1
[2012-07-12 12:03:58,573] [Thread-3] [DEBUG]
[com.rapplogic.xbee.RxTxSerialComm] serialEvent: 2 bytes available
[2012-07-12 12:03:58,573] [Thread-3] [ERROR]
[com.rapplogic.xbee.RxTxSerialComm] Error in handleSerialData method
java.lang.NullPointerException
at com.rapplogic.xbee.RxTxSerialComm.writeData(RxTxSerialComm.java:
261)
at com.rapplogic.xbee.RxTxSerialComm.getPacket(RxTxSerialComm.java:
216)
at com.rapplogic.xbee.RxTxSerialComm.serialEvent(RxTxSerialComm.java:
243)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
[2012-07-12 12:03:58,573] [Thread-3] [DEBUG]
[com.rapplogic.xbee.RxTxSerialComm] Ignoring serial port event type:
1


/*
*************************************************************************************
*/
/* This is the class I use to connect, send and receive data from/to
the Xbee transceiver connected to the PC */

/**
* Copyright (c) 2008 Andrew Rapp. All rights reserved.
*
* This file is part of XBee-API.
*
* XBee-API is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XBee-API is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBee-API. If not, see <http://www.gnu.org/licenses/>.
*/

package com.rapplogic.xbee;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;

import java.awt.Cursor;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.util.ByteUtils;
import com.rapplogic.xbee.sensorpresion.ColectorDatos;

/**
* This class encapsulates a RXTX serial port, providing access to
input/output streams,
* and notifying the subclass of new data events via the
handleSerialData method.
*
* @author andrew
*
*/
public class RxTxSerialComm implements XBeeConnection,
SerialPortEventListener {

private final static Logger log =
Logger.getLogger(RxTxSerialComm.class);

private InputStream inputStream;
private OutputStream outputStream;

private SerialPort serialPort;

private ColectorDatos colector;

private FileWriter file;
private BufferedWriter writer;

private int iFileCount = 0;

public RxTxSerialComm() {

}

public void openSerialPort(String port, int baudRate) throws
PortInUseException, UnsupportedCommOperationException,
TooManyListenersException, IOException, XBeeException {
this.openSerialPort(port, "XBee", 0, baudRate,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
SerialPort.FLOWCONTROL_NONE);
}

public void openSerialPort(String port, String appName, int timeout,
int baudRate) throws PortInUseException,
UnsupportedCommOperationException, TooManyListenersException,
IOException, XBeeException {
this.openSerialPort(port, appName, timeout, baudRate,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
SerialPort.FLOWCONTROL_NONE);
}

@SuppressWarnings("unchecked")
public void openSerialPort(String port, String appName, int timeout,
int baudRate, int dataBits, int stopBits, int parity, int flowControl)
throws PortInUseException, UnsupportedCommOperationException,
TooManyListenersException, IOException, XBeeException {
// Apparently you can't query for a specific port, but instead must
iterate
Enumeration<CommPortIdentifier> portList =
CommPortIdentifier.getPortIdentifiers();

CommPortIdentifier portId = null;

boolean found = false;

while (portList.hasMoreElements()) {

portId = portList.nextElement();

if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

//log.debug("Found port: " + portId.getName());

if (portId.getName().equals(port)) {
//log.debug("Using Port: " + portId.getName());
found = true;
break;
}
}
}

if (!found) {
throw new XBeeException("Could not find port: " + port);
}

serialPort = (SerialPort) portId.open(appName, timeout);

serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);

// activate the OUTPUT_BUFFER_EMPTY notifier
//serialPort.notifyOnOutputEmpty(true);

serialPort.addEventListener(this);

inputStream = serialPort.getInputStream();
outputStream = new
BufferedOutputStream(serialPort.getOutputStream());
}

/**
* Shuts down RXTX
*/
public void close() {
try {
serialPort.getInputStream().close();
} catch (Exception e) {
log.warn("Exception while closing input stream", e);
}

try {
serialPort.getOutputStream().close();
} catch (Exception e) {
log.warn("Exception while closing output stream", e);
}

try {
// this call blocks while thread is attempting to read from
inputstream
serialPort.removeEventListener();
serialPort.close();
} catch (Exception e) {
log.warn("Exception while closing serial port");
}
}

public void sendPacket(int[] packet) throws IOException {
try{
if (log.isInfoEnabled()) {
log.info("sending packet to XBee " + ByteUtils.toBase16(packet));
}

for (int aPacket : packet) {
this.getOutputStream().write(aPacket);
}

this.getOutputStream().flush();
}catch(Exception e){System.out.println(e);}
}

public void getPacket() throws IOException {
byte[] readBuffer = new
byte[serialPort.getInputStream().available()];
// byte[] readBuffer = new byte[this.getInputStream().available()];
String sBuffer, sFileName;
int indice;
boolean bIsPureData = true;

try {
while (inputStream.available() > 0) {
inputStream.read(readBuffer);
}

// guardo datos en archivo temporal
sBuffer = new String(readBuffer);

// encontró fin de archivo
indice = sBuffer.indexOf("?");
if(indice != -1){
if(indice > 0){
writeData(sBuffer.substring(0,indice));
}

writer.close();
bIsPureData = false;
}

// encontró inicio de archivo
indice = sBuffer.indexOf("/");
if(indice != -1){
sFileName = "C:/eclipse-jee-indigo-SR1-win32/workspace/
datos/" + colector.getPaciente() + "_" + Integer.toString(iFileCount)
+ ".txt";
file = new FileWriter(sFileName);
writer = new BufferedWriter(file);

if(indice < sBuffer.length()-1){
writeData(sBuffer.substring(indice + 1));
}

bIsPureData = false;
iFileCount++;
}

// encontró fin de transmisión
if(sBuffer.indexOf("#") != -1){
colector.jfrm.setCursor(Cursor.getDefaultCursor());
colector.jprogressBar.setIndeterminate(false);
colector.jprogressBar.setString("Datos recopilados");
bIsPureData = false;
}

if(bIsPureData){
writeData(sBuffer);
}
} catch (IOException e) {System.out.println(e);}
}

public OutputStream getOutputStream() {
return outputStream;
}

public InputStream getInputStream() {
return inputStream;
}

public void serialEvent(SerialPortEvent event) {

switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:

try {
if (this.getInputStream().available() > 0) {
try {
log.debug("serialEvent: " +
serialPort.getInputStream().available() + " bytes available");

synchronized (this) {
this.notify();
}

this.getPacket();
} catch (Exception e) {
log.error("Error in handleSerialData method", e);
// colector.setTexto(new String(new
byte[serialPort.getInputStream().available()]));
}
} else {
log.warn("We were notified of new data but available() is
returning 0");
}
} catch (IOException ex) {
// it's best not to throw the exception because the RXTX thread
may not be prepared to handle
log.error("RXTX error in serialEvent method", ex);
}
default:
log.debug("Ignoring serial port event type: " +
event.getEventType());
}
}

public void writeData(String texto){
try{
writer.write(texto);
}catch(IOException e){
colector.jfrm.setCursor(Cursor.getDefaultCursor());
colector.jprogressBar.setIndeterminate(false);
colector.jprogressBar.setString("Datos no recopilados");
JOptionPane.showMessageDialog(null,"Error al escribir
archivo.");
}
}

public void setColector(ColectorDatos datos){
this.colector = datos;
}
}


/*
*************************************************************************************
*/
/* This is the function that handles UART port interrupts from the u-
controller */

// UART 1 interrupt handler
// it is set at priority level 4
void __ISR(_UART1_VECTOR, ipl4) IntUart1Handler(void)
{

// Is this an RX interrupt?
if(mU1RXGetIntFlag())
{

// Echo what we just received.
int dato = getcUART1();
putcUART1(0x61);
// putcUART1(dato);
// putcUART1(97);

// Toggle LED to indicate UART activity
ToggleOrange();

// Clear the RX interrupt Flag
mU1RXClearIntFlag();
}
}
Reply all
Reply to author
Forward
0 new messages