I am trying to make a strain gauge display numbers in my java interface.
I have the strain gauge circuit working, and it sends its voltage into a microcontroller (PIC16F877A) program to do an analog to digital conversion and then output the numbers on a serial port. If you are interested, here is the code:
unsigned short analog; //Variable for analog voltage
long tlong;
unsigned char ch; //
void main() {
USART_Init(19200); //set baude rate
ADCON1 = 0; //All PORTA pins as analog, VDD as Vref
TRISA = 0xFF; //All PORTA is input
do {
analog = ADC_Read(2) >> 2; //Read 10-bit ADC from AN2 and discard 2 LS bit
tlong = (long)analog * 5000; // Convert the result in millivolts
tlong = tlong / 1023; // 0..1023 -> 0-5000mV
ch = tlong / 1000; // Extract volts (thousands of millivolts) from result
USART_Write(48+ch); // Write result in ASCII format
USART_Write('.');
ch = (tlong / 100) % 10; // Extract hundreds of millivolts
USART_Write(48+ch); // Write result in ASCII format
ch = (tlong / 10) % 10; // Extract tens of millivolts
USART_Write(48+ch); // Write result in ASCII format
ch = tlong % 10; // Extract digits for millivolts
USART_Write(48+ch); // Write result in ASCII format
USART_Write(' ');
USART_Write(' ');
USART_Write(' ');
Delay_ms(1000);
} while (1);
}
That does not work perfectly, because as I move the strain gauge, the output numbers don't change the way that they should. If anyone has any ideas for that, it will be much appreciated.
But I moved on anyway and sent those numbers into my java program. I found this online and modified it to fit my design:
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
static byte[] readBuffer;
public static int result2;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("portList... " + portList);
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("port identified is Serial.. "
+ portId.getPortType());
if (portId.getName().equals("COM4")) {
System.out.println("port identified is COM4.. "
+ portId.getName());
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
} else {
System.out.println("unable to open port");
}
}
}
}
public SimpleRead() {
try {
System.out.println("In SimpleRead() contructor");
serialPort = (SerialPort) portId.open("SimpleReadApp1111",500);
System.out.println(" Serial Port.. " + serialPort);
} catch (PortInUseException e) {
System.out.println("Port in use Exception");
}
try {
inputStream = serialPort.getInputStream();
System.out.println(" Input Stream... " + inputStream);
} catch (IOException e) {
System.out.println("IO Exception");
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println("Tooo many Listener exception");
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// no handshaking or other flow control
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
// timer on any read of the serial port
serialPort.enableReceiveTimeout(500);
System.out.println("................");
} catch (UnsupportedCommOperationException e) {
System.out.println("UnSupported comm operation");
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
System.out.println("In run() function ");
Thread.sleep(500);
// System.out.println();
} catch (InterruptedException e) {
System.out.println("Interrupted Exception in run() method");
}
}
public void serialEvent(SerialPortEvent event) {
// System.out.println("In Serial Event function().. " + event +
// event.getEventType());
switch (event.getEventType()) {
/*
* case SerialPortEvent.BI: case SerialPortEvent.OE: case
* SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD:
* case SerialPortEvent.CTS: case SerialPortEvent.DSR: case
* SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break;
*/
case SerialPortEvent.DATA_AVAILABLE:
readBuffer = new byte[500];
try {
while (inputStream.available()>0) {
int numBytes = inputStream.read(readBuffer);
// System.out.println("Number of bytes read " + numBytes);
System.out.print(new String(readBuffer));
}
} catch (IOException e) {
System.out.println("IO Exception in SerialEvent()");
}
break;
}
// System.out.println();
/* String one = new String(readBuffer);
char two = one.charAt(0);
System.out.println("Character at three: " + two);*/
}
}
It reads the same range of numbers (one serial port, so I can't test if it's the same numbers) as the previous code, although it sometimes gives me weird numbers like instead of .692, it'll say 320, then the next number is 43, then maybe only a '.' will show up, but it will soon go back to normal. I assumed it was a buffer problem where it says "readBuffer = new byte[500]", where i changed the size to 500 (originally was 8) and it got slightly better.
I have to turn the output into an integer to do some math with it anyway, and as an integer I can easily filter out all the weird data. So ultimately, my question is now would I turn this output data into usable integers? The main problem is that I'm not even sure where in my java program it's reading the serial port numbers. I assume the variable is readBuffer, but I get errors when ever I try and use it places, usually a type mismatch.
So any help would be great, thanks!