暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class...

16
暑暑暑暑暑暑

Transcript of 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class...

Page 1: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

暑期專題進度

Page 2: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

• import java.io.*;

• import java.util.*;

• import javax.comm.*;

• public class SimpleRead implements Runnable, SerialPortEventListener {

• static CommPortIdentifier portId;

• static Enumeration portList;

• InputStream inputStream;

• SerialPort serialPort;

• Thread readThread;

• public static void main(String[] args) {

• portList = CommPortIdentifier.getPortIdentifiers();

• while (portList.hasMoreElements()) {

• portId = (CommPortIdentifier) portList.nextElement();

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

• if (portId.getName().equals("COM1")) {

• // if (portId.getName().equals("/dev/term/a")) {

• SimpleRead reader = new SimpleRead();

• }

• }

• }

• }

程式碼

Page 3: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

• public SimpleRead() {

• try {

• serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);

• } catch (PortInUseException e) {System.out.println(e);}

• try {

• inputStream = serialPort.getInputStream();

• } catch (IOException e) {System.out.println(e);}

• try {

• serialPort.addEventListener(this);

• } catch (TooManyListenersException e) {System.out.println(e);}

• serialPort.notifyOnDataAvailable(true);

• try {

• serialPort.setSerialPortParams(9600,

• SerialPort.DATABITS_8,

• SerialPort.STOPBITS_1,

• SerialPort.PARITY_NONE);

• } catch (UnsupportedCommOperationException e) {System.out.println(e);}

• readThread = new Thread(this);

• readThread.start();

• }

• public void run() {

• try {

• Thread.sleep(20000);

• } catch (InterruptedException e) {System.out.println(e);}

• }

• public void serialEvent(SerialPortEvent event) {

• 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:

• byte[] readBuffer = new byte[20];

• try {

• while (inputStream.available() > 0) {

• int numBytes = inputStream.read(readBuffer);

• }

• System.out.print(new String(readBuffer));

• } catch (IOException e) {System.out.println(e);}

• break;

• }

• }

• }

Page 4: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.
Page 5: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

這程式碼的用意是把 arduino 上面超聲波的值用介面顯示的方式顯示出來,但是遇到進度是相關程式碼找到了,JAVA相關控制PORT的JDK也裝了。而當我RUN的時候它顯示了Error loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialParallel in java.library.pathCaught java.lang.UnsatisfiedLinkError: com.sun.comm.SolarisDriver.readRegistrySerial(Ljava/util/Vector;Ljava/lang/String;)I while loading driver com.sun.comm.SolarisDriverError loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialParallel in java.library.pathCaught java.lang.UnsatisfiedLinkError: com.sun.comm.SolarisDriver.readRegistrySerial(Ljava/util/Vector;Ljava/lang/String;)I while loading driver com.sun.comm.SolarisDriver

Page 6: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

新的工作是我的資料只要傳到 com port 即可 !接下來三軸的值也比照這個工作為主,接下來的三軸值的誤差值還在測試中。

接資料的部分和藍芽配合 !

最後我發現我的錯誤是下面的訊息應該是相關的 Library 未載入 !

(Project-Property-Library 內設定 )

Page 7: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

基本介面

Page 8: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

Android 調整音量/* 規格布局 */

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

android:id="@+id/widget33"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android"

><ImageButton

android:id="@+id/btnDown"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/down"

android:layout_alignLeft="@+id/up"

android:layout_centerHorizontal="true"

/>

<ImageButton

android:id="@+id/btnUp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/up"

android:layout_toLeftOf="@+id/btnDown"

/>

Page 9: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

Android 調整音量<TextView

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:id="@+id/barInfo"

android:layout_alignBottom="@+id/ProgressBar"

android:text=" 音量大小 "/>

<ProgressBar

android:id="@+id/ProgressBar"

android:layout_width="200px"

android:layout_height="wrap_content"

android:layout_marginTop="200sp"

android:layout_marginLeft="140sp"

style="?android:attr/progressBarStyleHorizontal"

/>

Page 10: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

Android 調整音量 </RelativeLayout>

/* 取得目前音量 */

volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);

/* 設定音量條 */

volumeBar.setMax(8);

volumeBar.setProgress(volume);

/* 透過 AudioManager.ADJUST_RAISE 調大音量 */

audioManager.adjustVolume(AudioManager.ADJUST_RAISE, 0);

volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);

volumeBar.setProgress(volume);

/* 透過 AudioManager.ADJUST_LOWER 調小音量 */

audioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0);

volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);

volumeBar.setProgress(volume);

Page 11: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

Android 調整音量 ( 預定畫面 )

Page 12: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

9/6/11

下面程式是藍芽接收端的程式碼,主要是藉由 commport 來做自動接收的動作,程式碼上有問題再解決

import java.io.*;

import java.util.*;

import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {

static CommPortIdentifier portId;

static Enumeration portList;

InputStream inputStream;

SerialPort serialPort;

Thread readThread;

public static void main(String[] args) {

portList = CommPortIdentifier.getPortIdentifiers();

Page 13: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

9/6/11

while (portList.hasMoreElements()) {

portId = (CommPortIdentifier) portList.nextElement();

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

if (portId.getName().equals("COM1")) {

// if (portId.getName().equals("/dev/term/a")) {

SimpleRead reader = new SimpleRead();

}

}

}

}

public SimpleRead() {

try {

serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);

} catch (PortInUseException e) {System.out.println(e);}

Page 14: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

9/6/11

try {

inputStream = serialPort.getInputStream();

} catch (IOException e) {System.out.println(e);}

try {

serialPort.addEventListener(this);

} catch (TooManyListenersException e) {System.out.println(e);}

serialPort.notifyOnDataAvailable(true);

try {

serialPort.setSerialPortParams(9600,

SerialPort.DATABITS_8,

SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE);

} catch (UnsupportedCommOperationException e) {System.out.println(e);}

readThread = new Thread(this);

readThread.start();

}

Page 15: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

9/6/11

public void run() {

try {

Thread.sleep(20000);

} catch (InterruptedException e) {System.out.println(e);}

}

public void serialEvent(SerialPortEvent event) {

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:

Page 16: 暑期專題進度. import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier.

9/6/11

break;

case SerialPortEvent.DATA_AVAILABLE:

byte[] readBuffer = new byte[20];

try {

while (inputStream.available() > 0) {

int numBytes = inputStream.read(readBuffer);

}

System.out.print(new String(readBuffer));

} catch (IOException e) {System.out.println(e);}

break;

}

}

}