arduino part
=============================================================
#include <SoftwareSerial.h>
int rxPin = 10;
int txPin = 11;
SoftwareSerial mySerial(rxPin, txPin); // RX, TX
int ledPin =13;
bool flag = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
mySerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(mySerial.available()){
char str = (mySerial.read());
Serial.print(String(str));
digitalWrite(ledPin, flag);
flag = !flag;
if(String(str) =="\n")
{
Serial.println("send back");
mySerial.print("o");
}
}
}
==============================================================
compile :
$ ndk-build
Compile thumb : uart <= tw_com_dmatek_dma210xp_uart_Linuxc.c
SharedLibrary : libuart.so
Install : libuart.so => libs/armeabi/libuart.so
android jni/tw_com_dmatek_dma210xp_uart_Linuxc.c
==============================================================
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "tw_com_dmatek_dma210xp_uart_Linuxc.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include <termios.h>
#include <android/log.h>
#include <sys/ioctl.h>
#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_dma210xp_uart_Linuxc_openUart
(JNIEnv *env, jclass mc, jint i)
{
fd=open("/dev/ttyUSB0",O_RDWR|O_NDELAY|O_NOCTTY);
return fd;
}
(JNIEnv *env, jclass mc, jint i)
{
close(fd);
}
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma210xp_uart_Linuxc_setUart
(JNIEnv *env, jclass mc, jint i)
{
int speed_arr[]={B1200,B2400,B4800,B9600,B19200,B38400,B57600,B115200,B230400,B921600};
//int speed_arr[]={B115200,B230400,B921600};
tcgetattr(fd,&oldtio);
tcgetattr(fd,&newtio);
cfsetispeed(&newtio,speed_arr[i]);
cfsetospeed(&newtio,speed_arr[i]);
// non standard mode and non echo and
// no Perfore a Backspace, Space, Backspace combination on receiving ERASE
// no start bit
newtio.c_lflag = 0;
//newtio.c_cflag = speed_arr[i] | CS8 | CREAD | CLOCAL;
newtio.c_cflag = B9600 | 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;
newtio.c_cc[VTIME]= 0;
newtio.c_cc[VMIN] = 0;
if(tcsetattr(fd,TCSANOW,&newtio)<0)
{
printf("tcsetattr fail !\n");
exit(1);
}
return fd;
}
(JNIEnv *env, jclass 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_dma210xp_uart_Linuxc_receiveMsgUart
(JNIEnv *env, jclass 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
{
__android_log_print(ANDROID_LOG_INFO,"receive num","receive =%d\n",len);
return NULL;
}
}
===============================================================
jni/tw_com_dmatek_dma210xp_uart_Linuxc.h
===============================================================/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class tw_com_dmatek_dma210xp_uart_Linuxc */
#ifndef _Included_tw_com_dmatek_dma210xp_uart_Linuxc
#define _Included_tw_com_dmatek_dma210xp_uart_Linuxc
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: tw_com_dmatek_dma210xp_uart_Linuxc
* Method: openUart
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma210xp_uart_Linuxc_openUart
(JNIEnv *, jclass, jint);
/*
* Class: tw_com_dmatek_dma210xp_uart_Linuxc
* Method: closeUart
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_tw_com_dmatek_dma210xp_uart_Linuxc_closeUart
(JNIEnv *, jclass, jint);
/*
* Class: tw_com_dmatek_dma210xp_uart_Linuxc
* Method: setUart
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma210xp_uart_Linuxc_setUart
(JNIEnv *, jclass, jint);
/*
* Class: tw_com_dmatek_dma210xp_uart_Linuxc
* Method: sendMsgUart
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_tw_com_dmatek_dma210xp_uart_Linuxc_sendMsgUart
(JNIEnv *, jclass, jstring);
/*
* Class: tw_com_dmatek_dma210xp_uart_Linuxc
* Method: receiveMsgUart
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_tw_com_dmatek_dma210xp_uart_Linuxc_receiveMsgUart
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
======================================================================
app 部分 :
======================================================================
package tw.com.dmatek.dma210xp.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();
}
===================================
package tw.com.dmatek.dma210xp.uart;
import java.util.Timer;
import java.util.TimerTask;
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;
TextView view1,view2,view;
Spinner spinner1;
String receiveMsg;
String Msg,str="liwei",ReStr;
EditText editmsg;
Button myButton0,myButton2,myButton3,myButton4;
private boolean sign1;
public int num=1; //
public int fd,set,i=-1,k=0;
int p;
private static final String[] countriesStr =
{ "B1200", "B2400", "B4800", "B9600","B19200","B38400","B57600","B115200","B230400","B921600"};
// { "B115200","B230400","B921600"};
private Spinner mySpinner;
private ArrayAdapter<String> 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(3);
if(fd>0)
{
setTitle("open device sucess!"+String.valueOf(fd));
Linuxc.setUart(k);
sign1=true;
}
else
{
setTitle("open device false!"+fd);
sign1=false;
}
view1.setText("The current UART�G3");
}
private Button.OnClickListener myButton2_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
view.setText(null);
}
};
// close
private Button.OnClickListener myButton3_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
timer.cancel();
Linuxc.closeUart(0);
finish();
}
};
// send message
private Button.OnClickListener myButton4_listener=new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
//Msg=editmsg.getText().toString();
Msg = "Hello world\n";
Linuxc.sendMsgUart(Msg);
/*
ReStr=Linuxc.receiveMsgUart();
//Linuxc.receiveMsgUart();
if(ReStr!=null)
{
view.append(ReStr);
view.append("\n");
ReStr=null;
}
*/
}
};
Timer timer = new Timer();
/* task */
TimerTask task = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (sign1==true) {
ReStr=Linuxc.receiveMsgUart();
//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.main);
mySpinner=(Spinner)findViewById(R.id.uart_mode);
// timer.schedule(task, 100, 1);
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);
myButton0=(Button)findViewById(R.id.uart_button0);
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);
myButton2.setOnClickListener(myButton2_listener);
myButton3.setOnClickListener(myButton3_listener);
myButton4.setOnClickListener(myButton4_listener);
adapter = new ArrayAdapter<String>(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);
k=arg2;
arg0.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
}
沒有留言:
張貼留言