ช่วยด้วยครับ ผมเขียน JAVA อ่าน Serial port ไม่ได้ครับ

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น

Moderator: phpbb, mindphp, ผู้ดูแลกระดาน

ตอบกลับโพส
000new000
phpBBThailand Newbie
phpBBThailand Newbie
โพสต์: 1
ลงทะเบียนเมื่อ: 25 ธ.ค. 2010, 23:38

ช่วยด้วยครับ ผมเขียน JAVA อ่าน Serial port ไม่ได้ครับ

โพสต์ โดย 000new000 »

พี่ๆช่วยผมด้วยครับ ผมเรียนอยู่วิศวกรรมเครื่องกลครับ แต่ทำโปรเจ็คยานสำรวจ ผ่านสายเคเบิล ผมจะรับค่าของเซนเซอร์จากบอร์ด arduino ครับแต่มันมีหลายค่า ตอนนี้ค่าที่ส่งมามันรวมกันเป็นชุดเดียว
ผมจะทำอย่างไรให้ค่าที่อ่านได้มาแสดงแยกกันครับ ผมเอาตัวอย่างมาให่ดูด้วยครับ อันแรกจะเป็นของ arduino ซึ่งเป็น C++ และอันที่สองเป็น JAVA ครับ
******************************* arduino*********************************
unsigned long timer=0; //timer
unsigned long dt; //delta t - how long it takes to go through main loop

int sens_value[6] = {0,0,0,0,0,0}; // array of sensor data
int sens_offset[6] = {0,0,0,0,0,0}; // array of offset values for sensors
int gyro4x[3] = {0,0,0}; // array of Gyro amplifed (4x) values - relative data
int accel[3] = {0,0,0}; // array of accelerometer data - relative data

void setup()

{
analogReference(EXTERNAL); //using external analog ref of 3.3V for ADC scaling
Serial.begin(9600); //setup serial baud rate
DDRC = B00000000; // make all analog ports as inputs - just in case....

Serial.print("Calculating offset. Plase wait ");
for (int a=0; a<75; a++) // acquire data a lot of times in order to produce
{ // good offset value - "warm-up" ADC
read_ADC(); //read ADC sensor values
Serial.print("."); //print dot to show that Arduino is doing something
//in this case acquiring data from 6 DOF Razor board
}

for (int c=0; c<6; c++)
{
sens_offset[c] = sens_value[c]; // store offset values in offset array
}
sens_offset[3] = sens_offset[3] + 101; //adding 101 (raw value of 1g) to z accelerometer
//depending on your wanted axes orientation this
//can be eitehr + 101 or - 101

Serial.println("Done"); //Done calculating gyro and accelerometer offsets
Serial.println("--------- ");
Serial.println("t[ms] \t gx \t gy \t gz \t ax \t ay \t az "); //print data header

timer=millis(); //set timer
}

void loop()
{
dt = millis() - timer; // calculate time through loop i.e. acquisition rate
timer=millis(); // reset timer
Serial.print(dt); //print delta time
Serial.print ("\t");
read_ADC(); //read values from gyro and accelerometer sensors

//assign data to gyro and accelerometer arrays
gyro4x[0] = delta_ADC(1); //gyro x
gyro4x[1] = delta_ADC(0); //gyro y
gyro4x[2] = delta_ADC(2); //gyro z
accel[0] = delta_ADC(5); //accel x
accel[1] = delta_ADC(4); //accel y
accel[2] = delta_ADC(3); //accel x

print_data(); //print delta data - i.e. difference from offset
delay(1000); //
}

void read_ADC() //read sensor data
{
for (int i=0; i<6; i++)
{
sens_value = ((analogRead(i) * 0.9)+ (sens_value * 0.1)) ; // read the input pin and reduce noise
}
}

int delta_ADC(int index) //subtract current value from offset
{
return (sens_value[index] - sens_offset[index]);
}

void print_data() //print data
{
for (int j=0; j<3; j++)
{
Serial.print(gyro4x[j]);
Serial.print ("\t");
}

for (int k=0; k<3; k++)
{
Serial.print(accel[k]);
Serial.print ("\t");
}

Serial.println("");
}

************************************************JAVA**************************************************************

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;


public class SerialTest extends Frame implements SerialPortEventListener {
/**
*
*/
private static final long serialVersionUID = 1L;
SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
"COM5", // Windows
};
/** Buffered input stream from the port */
private InputStream input;
/** The output stream to the port */
@SuppressWarnings("unused")
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
Label compass = new Label();
Label adruino = new Label();
public void initialize() {
CommPortIdentifier portId = null;
@SuppressWarnings("rawtypes")
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// iterate through, looking for the port
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}

if (portId == null) {
System.out.println("Could not find COM port.");
return;
}

try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);

// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

// open the streams
input = serialPort.getInputStream();
output = serialPort.getOutputStream();

// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}

/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}

/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {

int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk);
// Displayed results are codepage dependent
System.out.print(new String(chunk));
adruino.setText(new String(chunk));
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}
public void ROVFrame(){
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
setTitle("Adruino");
setLayout(new GridLayout(2,1));
add(new Label("t[ms] \t gy \t gx \t gz \t az \t ay \t ax ",Label.CENTER));
add(adruino);
}

public static void main(String arg[]) throws Exception {
SerialTest main = new SerialTest();
main.initialize();
main.setTitle("Adruino");
main.setSize(500,100);
main.setVisible(true);
main.ROVFrame();


}
}

ช่วยดูให้หน่อยนะครับ ผมพยายามทำมาหลายสัปดาห์แล้ว รบกวนด้วยนะครับ " ขอบคุณครับ"
ตอบกลับโพส

ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 12 และ บุคคลทั่วไป 0 ท่าน