通常對於我們這些窮人來說(買不起IAR或是TI的CCS)的人,更不可能會想去花錢買M$的 WINDOWS 作業系統。
難道,就這樣我們就不能開發單晶片(MSP430)的應用了嗎?
找到了一份 ubuntu 下安裝 mspgcc 的程序。
照著做了,還算正常,雖然它的程序有些檔案找不到了,自己手動去找下來放也可以完成。
實測 RF2500 的結果,在 mspdebug 下可以燒錄跟 reset, run 了。
等玩一下再跟 eclipse 結合,這樣敏哥會更上手。
參考網址:
1. http://hackaday.com/2010/08/11/how-to-launchpad-programming-with-linux/
2. http://blog.wikifotos.org/2010/11/15/msp430-launchpad-in-ubuntu/
3. http://mylightswitch.com/2010/06/21/installing-mpsgcc4-and-mspdebug-on-kubuntu-1004/
這裏有 MSPGCC 4.6.2 的製作法,在 ubuntu 10.04 上成功安裝的方法
http://nonefree.blogspot.com/2012/03/mspgcc-462-and-ubuntu-1004.html
本社群由Nantou.py使用者社群以及國立虎尾科技大學電機資訊學院負責維護,它是一群熱愛智慧生活科技以及Python的專業教師所組成,大家一同快樂地研究有關數位生活中人工智慧、大數據、物聯網、雲端服務、APPS、福祉科技、感知網路服務、車載網路服務、及網際網路等資通訊技術,並運用這些資通訊以及Python技術來提升我們的日常生活品質,建立更好的生活環境。
2012年1月31日 星期二
2012年1月27日 星期五
Android Bluetooth 應用之 HelloBTUart(RS-232)
在NDK這篇HelloUart中,有人詢問到開發平台轉移至智慧型手機上應該如何實現呢??
另一個答案就是藍牙(Bluetooth)。
因此,今天的主題就是在Android手機上實現藍牙規範中的 Serial Port Profile(SPP)作為RS-232的通訊。
另一個答案就是藍牙(Bluetooth)。
因此,今天的主題就是在Android手機上實現藍牙規範中的 Serial Port Profile(SPP)作為RS-232的通訊。
2012年1月16日 星期一
[ Android Design ] ActionBar上的選單=功能表的選單
我們可以在ActionBar上放置一些常的的選單,不管您是將選單放置在ActionBar或功能表上,其事件處理程序都是共用同一個,
就是onOptionsItemSelected()函式,其原始程式如下:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Tapped home", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_refresh:
Toast.makeText(this, "Fake refreshing...", Toast.LENGTH_SHORT).show();
getActionBarHelper().setRefreshActionItemState(true);
getWindow().getDecorView().postDelayed(
new Runnable() {
public void run() {
getActionBarHelper().setRefreshActionItemState(false);
}
}, 1000);
break;
case R.id.menu_search:
Toast.makeText(this, "Tapped search", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_share:
Toast.makeText(this, "Tapped share", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
[ Android Design ] 決定是否要顯示在ActionBar上
在上圖中我們可發現有些功能選項(menu_refresh, menu_search)可以顯示在ActionBar上,有些(menu_share)則沒有,其關鍵的屬是showAsAction,若使用"alway"則會放在ActionBar上,但若用"never"則不會。
[ Android Design ] ActionBar初次接觸
在Android SDK 4.0的範例中,ActionBarCompat就是來說明如何設計ActionBar。下圖即為此範例的執行結果。
當我們按下"Toggle title"的按鈕後,您會發現在ActionBar在的標題被改變了。而當我們按下搜尋鈕後,在內容中將浮出"Tapped search"文字。
而按下重整鈕後,將顯示"Fake refreshing"。
當您按下功能表鍵時,則會顯示"Share"的功能表單。
2012年1月15日 星期日
Google公布官方的Android裝置設計參考規範「Android Design」網站
Android Design:
http://developer.android.com/design/index.html
網站中內容包括 Android 從基礎理念、設計概念甚至是色彩、圖示、字型的美學應用,提供開發者作為參考。
http://developer.android.com/design/index.html
網站中內容包括 Android 從基礎理念、設計概念甚至是色彩、圖示、字型的美學應用,提供開發者作為參考。
[ Android RS232 ] 在Android系統下實作RS232應用程式
有讀者反應希望能公開RS232 UART的原始程式碼,經本人向長高科技反應,得到該公司的授權,將該公司提供的UART原始程式碼公開。
首先,在UART Android應用程式是由兩支程式所組成:Uart_Control.java及Linuxc.java。Uart_Control.java 是UART應用程式的主程式,其原始程式如下:
package tw.com.dmatek.dma6410xp.uart;
import java.util.Timer;
import java.util.TimerTask;
import tw.com.dmatek.dma6410xp.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Uart_Control extends Activity {
TextView view1,view2,view;
Spinner spinner1;
String receiveMsg;
String Msg,str="liwei",ReStr;
EditText editmsg;
Button myButton0,myButton1,myButton2,myButton3,myButton4;
private boolean sign1;
public int num=1;
public int fd,set,i=-1,ss=0;
int p;
private static final String[] countriesStr =
{ "B1200", "B2400", "B4800", "B9600","B19200","B38400","B57600","B115200","B230400","B921600"};
private Spinner mySpinner;
private ArrayAdapter adapter;
private Button.OnClickListener myButton0_listener=new Button.OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(i>0)
{
Linuxc.closeUart(0);
}
i=1;
fd=Linuxc.openUart(1);
if(fd>0)
{
setTitle("open device sucess!"+String.valueOf(fd));
Linuxc.setUart(ss);
sign1=true;
}
else
{
setTitle("open device false!"+fd);
sign1=false;
}
view1.setText("The current UART 1?");
}
};
private Button.OnClickListener myButton1_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
if(i>0)
{
Linuxc.closeUart(0);
}
i=2;
fd=Linuxc.openUart(2);
if(fd>0)
{
setTitle("open device sucess!"+String.valueOf(fd));
Linuxc.setUart(ss);
sign1=true;
}
else{
setTitle("open device false!"+fd);
sign1=false;
}
view1.setText("The current UART 2?");
}
};
private Button.OnClickListener myButton2_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
view.setText(null);
}
};
private Button.OnClickListener myButton3_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
timer.cancel();
Linuxc.closeUart(0);
finish();
// System.exit(0);
}
};
private Button.OnClickListener myButton4_listener=new Button.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Msg=editmsg.getText().toString();
Linuxc.sendMsgUart(Msg);
}
};
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
if (sign1)
{
ReStr=Linuxc.receiveMsgUart();
if(ReStr!=null)
{
view.append(ReStr);
view.append("\n");
ReStr=null;
}
}
}
});
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uart);
timer.schedule(task, 1000, 10);
view1=(TextView)findViewById(R.id.uart_textview);
view=(TextView)findViewById(R.id.uart_view);
editmsg=(EditText)findViewById(R.id.uart_edit);
editmsg.setWidth(200);
mySpinner=(Spinner)findViewById(R.id.uart_mode);
myButton0=(Button)findViewById(R.id.uart_button0);
myButton1=(Button)findViewById(R.id.uart_button1);
myButton2=(Button)findViewById(R.id.uart_button2);
myButton3=(Button)findViewById(R.id.uart_button3);
myButton4=(Button)findViewById(R.id.uart_button4);
myButton0.setOnClickListener(myButton0_listener);
myButton1.setOnClickListener(myButton1_listener);
myButton2.setOnClickListener(myButton2_listener);
myButton3.setOnClickListener(myButton3_listener);
myButton4.setOnClickListener(myButton4_listener);
adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, countriesStr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
{
public void onItemSelected(AdapterView arg0, View arg1, int arg2,
long arg3)
{
if(fd>0) Linuxc.setUart(arg2);
ss=arg2;
arg0.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView arg0)
{
// TODO Auto-generated method stub
}
});
}
}
Linuxc.java則是用來連接SO的檔案,其原始程式如下:
package tw.com.dmatek.dma6410xp.uart;
import android.util.Log;
public class Linuxc {
static
{
try
{
System.loadLibrary("uart");
Log.i("JIN","Trying to load libuart.so");
}
catch(UnsatisfiedLinkError ule)
{
Log.e("JIN","WARNING:could not load libuart.so");
}
}
public static native int openUart(int i);
public static native void closeUart(int i);
public static native int setUart(int i);
public static native int sendMsgUart(String msg);
public static native String receiveMsgUart();
}
資源檔uart.xml原始程式如下:
上述三個檔案可以產生UART.apk
除了APK檔案外,還需要SO函式庫,其原始程式包括:tw_com_dmatek_dma6410xp_uart_Linuxc及tw_com_dmatek_dma6410xp_uart_Linuxc.h
tw_com_dmatek_dma6410xp_uart_Linuxc.c的原始程式如下:
#include
#include
#include
#include
#include
#include "tw_com_dmatek_dma6410xp_uart_Linuxc.h"
#include
#include
#include
#include
#include
#include
//#include
#include
#undef TCSAFLUSH
#define TCSAFLUSH TCSETSF
#ifndef _TERMIOS_H_
#define _TERMIOS_H_
#endif
int fd;
struct termios newtio,oldtio;
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_openUart(JNIEnv *env,jobject mc,jint i)
{
if(i==1)
{
fd=open("/dev/ttySAC1",O_RDWR|O_NOCTTY|O_NDELAY);//O_NOCTTY
return fd;
}
else if(i==2)
{
fd=open("/dev/ttySAC2",O_RDWR|O_NOCTTY|O_NDELAY);
return fd;
}
else if(i==3)
{
fd=open("/dev/ttySAC3",O_RDWR|O_NDELAY|O_NOCTTY);
return fd;
}
return fd;
}
JNIEXPORT void JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_closeUart(JNIEnv *env,jobject mc,jint i)
{
close(fd);
}
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_setUart(JNIEnv *env,jobject mc,jint i)
{
int speed_arr[]={B1200,B2400,B4800,B9600,B19200,B38400,B57600,B115200,B230400,B921600};
tcgetattr(fd,&oldtio);
tcgetattr(fd,&newtio);
cfsetispeed(&newtio,speed_arr[i]);
cfsetospeed(&newtio,speed_arr[i]);
newtio.c_lflag=0;
newtio.c_cflag = speed_arr[i] | CS8 | CREAD | CLOCAL;
newtio.c_iflag= BRKINT | IGNPAR | IGNCR | IXON | IXOFF | IXANY ;
newtio.c_oflag=02;
newtio.c_line=0;
newtio.c_cc[7]=255;
newtio.c_cc[4]=0;
newtio.c_cc[5]=0;
if(tcsetattr(fd,TCSANOW,&newtio)<0)
{
printf("tcsetattr fail !\n");
exit(1);
}
return fd;
}
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_sendMsgUart(JNIEnv *env,jobject mc,jstring str)
{
int len;
const char *buf;
buf=(*env)->GetStringUTFChars(env,str,NULL);
len= (*env)->GetStringLength(env,str );
write(fd,buf,len);
(*env)->ReleaseStringUTFChars(env, str, buf);
}
JNIEXPORT jstring JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_receiveMsgUart(JNIEnv *env,jobject mc)
{
char buffer[255];
char buf[255];
int len,i=0,k=0;
memset(buffer,0,sizeof(buffer));
memset(buf,0,sizeof(buf));
printf("\n");
len=read(fd,buffer,255);
if (len>0)
{
__android_log_print(ANDROID_LOG_INFO,"len","len =%d, buffer[0]=%c,buffer[1]=%c\n",len,buffer[0],buffer[1]);
__android_log_print(ANDROID_LOG_INFO,"buffer","buffer=%s\n",buffer);
//return (*env)->NewStringUTF(env,buffer);
}
else
return NULL;
}
tw_com_dmatek_dma6410xp_uart_Linuxc.h的原始程式如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class tw_com_dmatek_dma6410xp_uart_Linuxc */
#ifndef _Included_tw_com_dmatek_dma6410xp_uart_Linuxc
#define _Included_tw_com_dmatek_dma6410xp_uart_Linuxc
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: openUart
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_openUart
(JNIEnv *, jobject, jint);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: closeUart
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_closeUart
(JNIEnv *, jobject, jint);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: setUart
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_setUart
(JNIEnv *, jobject,jint);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: sendMsgUart
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_sendMsgUart
(JNIEnv *, jobject, jstring);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: receiveMsgUart
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_receiveMsgUart
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
首先,在UART Android應用程式是由兩支程式所組成:Uart_Control.java及Linuxc.java。Uart_Control.java 是UART應用程式的主程式,其原始程式如下:
package tw.com.dmatek.dma6410xp.uart;
import java.util.Timer;
import java.util.TimerTask;
import tw.com.dmatek.dma6410xp.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Uart_Control extends Activity {
TextView view1,view2,view;
Spinner spinner1;
String receiveMsg;
String Msg,str="liwei",ReStr;
EditText editmsg;
Button myButton0,myButton1,myButton2,myButton3,myButton4;
private boolean sign1;
public int num=1;
public int fd,set,i=-1,ss=0;
int p;
private static final String[] countriesStr =
{ "B1200", "B2400", "B4800", "B9600","B19200","B38400","B57600","B115200","B230400","B921600"};
private Spinner mySpinner;
private ArrayAdapter
private Button.OnClickListener myButton0_listener=new Button.OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(i>0)
{
Linuxc.closeUart(0);
}
i=1;
fd=Linuxc.openUart(1);
if(fd>0)
{
setTitle("open device sucess!"+String.valueOf(fd));
Linuxc.setUart(ss);
sign1=true;
}
else
{
setTitle("open device false!"+fd);
sign1=false;
}
view1.setText("The current UART 1?");
}
};
private Button.OnClickListener myButton1_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
if(i>0)
{
Linuxc.closeUart(0);
}
i=2;
fd=Linuxc.openUart(2);
if(fd>0)
{
setTitle("open device sucess!"+String.valueOf(fd));
Linuxc.setUart(ss);
sign1=true;
}
else{
setTitle("open device false!"+fd);
sign1=false;
}
view1.setText("The current UART 2?");
}
};
private Button.OnClickListener myButton2_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
view.setText(null);
}
};
private Button.OnClickListener myButton3_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
timer.cancel();
Linuxc.closeUart(0);
finish();
// System.exit(0);
}
};
private Button.OnClickListener myButton4_listener=new Button.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Msg=editmsg.getText().toString();
Linuxc.sendMsgUart(Msg);
}
};
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
if (sign1)
{
ReStr=Linuxc.receiveMsgUart();
if(ReStr!=null)
{
view.append(ReStr);
view.append("\n");
ReStr=null;
}
}
}
});
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uart);
timer.schedule(task, 1000, 10);
view1=(TextView)findViewById(R.id.uart_textview);
view=(TextView)findViewById(R.id.uart_view);
editmsg=(EditText)findViewById(R.id.uart_edit);
editmsg.setWidth(200);
mySpinner=(Spinner)findViewById(R.id.uart_mode);
myButton0=(Button)findViewById(R.id.uart_button0);
myButton1=(Button)findViewById(R.id.uart_button1);
myButton2=(Button)findViewById(R.id.uart_button2);
myButton3=(Button)findViewById(R.id.uart_button3);
myButton4=(Button)findViewById(R.id.uart_button4);
myButton0.setOnClickListener(myButton0_listener);
myButton1.setOnClickListener(myButton1_listener);
myButton2.setOnClickListener(myButton2_listener);
myButton3.setOnClickListener(myButton3_listener);
myButton4.setOnClickListener(myButton4_listener);
adapter = new ArrayAdapter
android.R.layout.simple_spinner_item, countriesStr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
{
public void onItemSelected(AdapterView arg0, View arg1, int arg2,
long arg3)
{
if(fd>0) Linuxc.setUart(arg2);
ss=arg2;
arg0.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView arg0)
{
// TODO Auto-generated method stub
}
});
}
}
Linuxc.java則是用來連接SO的檔案,其原始程式如下:
package tw.com.dmatek.dma6410xp.uart;
import android.util.Log;
public class Linuxc {
static
{
try
{
System.loadLibrary("uart");
Log.i("JIN","Trying to load libuart.so");
}
catch(UnsatisfiedLinkError ule)
{
Log.e("JIN","WARNING:could not load libuart.so");
}
}
public static native int openUart(int i);
public static native void closeUart(int i);
public static native int setUart(int i);
public static native int sendMsgUart(String msg);
public static native String receiveMsgUart();
}
資源檔uart.xml原始程式如下:
xml
version="1.0" encoding="utf-8" ?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/uart_index">
<TextView android:id="@+id/uart_textview" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="DMA-6410XP desk to UART item" android:textSize="30px" android:textColor="#ffffffff"
android:layout_centerHorizontal="true" android:layout_alignParentTop="true" />
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/uart_scrollview"
android:layout_width="520px" android:layout_height="230px"
android:layout_marginTop="35px" android:layout_below="@+id/uart_textview"
android:layout_alignParentRight="true" android:layout_marginRight="30px"
android:background="#ffffffff">
<TextView android:id="@+id/uart_view" android:layout_above="@+id/uart_text"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="#ff000000" android:textSize="20px"
android:text="" />
ScrollView>
<TextView android:id="@+id/uart_view1" android:layout_width="wrap_content"
android:layout_height="40px" android:layout_below="@+id/uart_scrollview"
android:layout_centerHorizontal="true" android:layout_marginTop="30px"
android:layout_marginLeft="50px" android:layout_marginRight="50px"
android:text="" />
<EditText android:id="@+id/uart_edit" android:layout_width="350px"
android:layout_height="65px" android:layout_marginTop="40px"
android:layout_marginRight="40px" android:layout_below="@+id/uart_scrollview"
android:layout_alignLeft="@+id/uart_scrollview"
/>
<Button android:id="@+id/uart_button1" android:layout_width="100px"
android:layout_height="wrap_content" android:layout_below="@+id/uart_button0"
android:layout_marginTop="15px" android:layout_marginLeft="70px"
android:text="UART 2" />
<Button android:id="@+id/uart_button0" android:layout_width="100px"
android:layout_height="wrap_content" android:layout_alignTop="@+id/uart_scrollview"
android:layout_marginLeft="70px" android:text="UART
1" />
<Button android:id="@+id/uart_button2" android:layout_width="100px"
android:layout_height="wrap_content" android:layout_marginLeft="70px"
android:layout_alignBottom="@+id/uart_scrollview"
android:text="CLEAN" />
<Button android:id="@+id/uart_button3" android:layout_width="100px"
android:layout_height="wrap_content" android:layout_alignBaseline="@+id/uart_edit"
android:layout_marginBottom="30dip" android:layout_marginLeft="70dip"
android:text="EXIT" />
<Spinner android:id="@+id/uart_mode" android:layout_width="100px"
android:layout_height="wrap_content" android:layout_below="@+id/uart_button1"
android:layout_marginTop="15px" android:layout_marginLeft="70px" />
<Button android:id="@+id/uart_button4" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignBaseline="@+id/uart_edit"
android:layout_toRightOf="@+id/uart_edit" android:layout_alignRight="@+id/uart_scrollview"
android:text="SEND" />
RelativeLayout>
上述三個檔案可以產生UART.apk
除了APK檔案外,還需要SO函式庫,其原始程式包括:tw_com_dmatek_dma6410xp_uart_Linuxc及tw_com_dmatek_dma6410xp_uart_Linuxc.h
tw_com_dmatek_dma6410xp_uart_Linuxc.c的原始程式如下:
#include
#include
#include
#include
#include
#include "tw_com_dmatek_dma6410xp_uart_Linuxc.h"
#include
#include
#include
#include
#include
#include
//#include
#include
#undef TCSAFLUSH
#define TCSAFLUSH TCSETSF
#ifndef _TERMIOS_H_
#define _TERMIOS_H_
#endif
int fd;
struct termios newtio,oldtio;
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_openUart(JNIEnv *env,jobject mc,jint i)
{
if(i==1)
{
fd=open("/dev/ttySAC1",O_RDWR|O_NOCTTY|O_NDELAY);//O_NOCTTY
return fd;
}
else if(i==2)
{
fd=open("/dev/ttySAC2",O_RDWR|O_NOCTTY|O_NDELAY);
return fd;
}
else if(i==3)
{
fd=open("/dev/ttySAC3",O_RDWR|O_NDELAY|O_NOCTTY);
return fd;
}
return fd;
}
JNIEXPORT void JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_closeUart(JNIEnv *env,jobject mc,jint i)
{
close(fd);
}
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_setUart(JNIEnv *env,jobject mc,jint i)
{
int speed_arr[]={B1200,B2400,B4800,B9600,B19200,B38400,B57600,B115200,B230400,B921600};
tcgetattr(fd,&oldtio);
tcgetattr(fd,&newtio);
cfsetispeed(&newtio,speed_arr[i]);
cfsetospeed(&newtio,speed_arr[i]);
newtio.c_lflag=0;
newtio.c_cflag = speed_arr[i] | CS8 | CREAD | CLOCAL;
newtio.c_iflag= BRKINT | IGNPAR | IGNCR | IXON | IXOFF | IXANY ;
newtio.c_oflag=02;
newtio.c_line=0;
newtio.c_cc[7]=255;
newtio.c_cc[4]=0;
newtio.c_cc[5]=0;
if(tcsetattr(fd,TCSANOW,&newtio)<0)
{
printf("tcsetattr fail !\n");
exit(1);
}
return fd;
}
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_sendMsgUart(JNIEnv *env,jobject mc,jstring str)
{
int len;
const char *buf;
buf=(*env)->GetStringUTFChars(env,str,NULL);
len= (*env)->GetStringLength(env,str );
write(fd,buf,len);
(*env)->ReleaseStringUTFChars(env, str, buf);
}
JNIEXPORT jstring JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_receiveMsgUart(JNIEnv *env,jobject mc)
{
char buffer[255];
char buf[255];
int len,i=0,k=0;
memset(buffer,0,sizeof(buffer));
memset(buf,0,sizeof(buf));
printf("\n");
len=read(fd,buffer,255);
if (len>0)
{
__android_log_print(ANDROID_LOG_INFO,"len","len =%d, buffer[0]=%c,buffer[1]=%c\n",len,buffer[0],buffer[1]);
__android_log_print(ANDROID_LOG_INFO,"buffer","buffer=%s\n",buffer);
//return (*env)->NewStringUTF(env,buffer);
}
else
return NULL;
}
tw_com_dmatek_dma6410xp_uart_Linuxc.h的原始程式如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class tw_com_dmatek_dma6410xp_uart_Linuxc */
#ifndef _Included_tw_com_dmatek_dma6410xp_uart_Linuxc
#define _Included_tw_com_dmatek_dma6410xp_uart_Linuxc
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: openUart
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_openUart
(JNIEnv *, jobject, jint);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: closeUart
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_closeUart
(JNIEnv *, jobject, jint);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: setUart
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_setUart
(JNIEnv *, jobject,jint);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: sendMsgUart
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_sendMsgUart
(JNIEnv *, jobject, jstring);
/*
* Class: tw_com_dmatek_dma6410xp_uart_Linuxc
* Method: receiveMsgUart
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_tw_com_dmatek_dma6410xp_uart_Linuxc_receiveMsgUart
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
2012年1月6日 星期五
SOS 感測器觀測伺服器的安裝
訂閱:
文章 (Atom)