SparkFun Forums 

Where electronics enthusiasts find answers.

All things pertaining to wireless and RF links
By Cannonball2134
#35184
I have a problem that i hope someone can help me with.

I have been trying to connect My PIC 16f877a to the BR-SC11A Bluetooth module, i have attempted to use the PIC's USART in asynchronous mode to connect to the Bluetooth module.
I have connected power to the Bluetooth module and i have connected the RX to the TX and the TX to the RX on the PIC.
I can search and find the Bluetooth module and it tells me that i have com5 as my outgoing connection and com6 as my incoming connection.
I have code on the PIC (ASM) that I’m pretty sure is correct and i have a java program which i was trying to use to send data down com5 (have also tried com6) but it can't find com5 and it freeze's after it's found com6. I have been using baud rate of 9600, i have tried using a few different terminals but they can't find com5 and don't work on com6.

Can anyone help me?
I am really lost, even the smallest suggestion would be a great help.
Thanks
By Cannonball2134
#35200
Hello

I should probably show you the code.


import java.io.*;
import java.util.*;
import gnu.io.*; // for rxtxSerial library

//J2C2 is the name of the robot.

public class J2C2 implements Runnable
{
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
static OutputStream outputStream;

public static void main(String[] args)
{
boolean portFound = false;
String defaultPort = "COM6";

portList = CommPortIdentifier.getPortIdentifiers(); //gets all aviable ports
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) //if the port is a serial port then enter
{
if (portId.getName().equals(defaultPort)) //if the defaultPort == the serial port name enter
{
System.out.println("Found port: "+defaultPort);
portFound = true;
// init reader thread
J2C2 reader = new J2C2();
}
}
}
if (!portFound)
{
System.out.println("port " + defaultPort + " not found.");
}
}

public void initwritetoport()
{
try
{
outputStream = serialPort.getOutputStream(); // get the outputstream
} catch (IOException e) {}

try
{
serialPort.notifyOnOutputEmpty(true);// activate the OUTPUT_BUFFER_EMPTY notifier
} catch (Exception e)
{
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
}

public void writetoport()
{
System.out.println("Writing to "+serialPort.getName());
try
{
outputStream.write(255); // The data i'm trying to write to the USART via the bluetooth module
} catch (IOException e) {}
}

public J2C2()
{
try // initalize serial port (opens the port)
{
serialPort = (SerialPort) portId.open("J2C2 Control", 2000); //Aplication currently that will use the port doesn't have to be a method or class
} catch (PortInUseException e) {}

try // Gets an inputStream from the opened port
{
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println("HERE1");}

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

try // sets up port parameters baud rate etc..
{
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.notifyOnOutputEmpty(true);
} catch (UnsupportedCommOperationException e) {}

// start the read thread
readThread = new Thread(this);
readThread.start();
}

public void run()
{
// first thing in the thread, we initialize the write operation
initwritetoport();
try
{
while (true)
{
writetoport();// write string to port, the serialEvent will read it
Thread.sleep(1000);
}
} catch (InterruptedException e) {}
}
}


It has worked before on the serial port when communicating with sc322 servo board. If anyone can comfirm that this code looks ok then at least that's one less place to look for any problems. Thanks