串口通信
我仿android-serial-api做的串口通信APP,对ttyS0~3进行读写操作发现并不能对ttyS0~3存取数据。jni的代码JNIEXPORT jobject JNICALL Java_com_example_liangzhanpeng_urat_SerialPort_open
(JNIEnv *env, jclass thiz, jstring path, jint baudrate){
int fd;
speed_t speed;
jclass mFileDescriptor;
{
speed = getBaudrate(baudrate);
if (speed == -1){
LOGE("invalid baudrate");
return NULL;
}
}
{
jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
LOGE("OPENING SERIAL PORT %s",path_utf);
fd = open(path_utf,O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
LOGD("open() fd = %d", fd);
(*env)->ReleaseStringUTFChars(env,path,path_utf);
if(fd == -1)
{
LOGE("cannot open port");
return NULL;
}
}
{
struct termios cfg;
LOGD("Configuring serial port");
if(tcgetattr(fd,&cfg))
{
LOGE("tcgetattr() failed");
close(fd);
return NULL;
}
cfmakeraw(&cfg);
cfsetispeed(&cfg,speed);
cfsetospeed(&cfg,speed);
if(tcsetattr(fd,TCSANOW,&cfg))
{
LOGE("tcsetattr() failed");
close(fd);
return NULL;
}
}
{
jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor");
jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "<init>", "()V");
jfieldID descriptorID = (*env)->GetFieldID(env, cFileDescriptor, "descriptor", "I");
mFileDescriptor = (*env)->NewObject(env, cFileDescriptor, iFileDescriptor);
(*env)->SetIntField(env, mFileDescriptor, descriptorID, (jint)fd);
}
return mFileDescriptor;
}
串口操作代码为 private FileDescriptor mfd;
private FileInputStream mFileInputStream;
private FileOutputStream outputStreaml;
public SerialPort(File device,int baudrate) throws SecurityException,IOException{
if (!device.canRead() || !device.canWrite()){
try{
Process su;
su = Runtime.getRuntime().exec("/system/xbin/su");
Log.i(TAG, "SerialPort: "+su.toString());
String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";
Log.i(TAG, "SerialPort: " + cmd.toString());
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()){
throw new SecurityException();
}
}catch (Exception e){
e.printStackTrace();
throw new SecurityException();
}
}
Log.i(TAG, "SerialPort is can read?"+device.canRead());
Log.i(TAG,"SerialPort is can writter" + device.canWrite());
mfd = open(device.getAbsolutePath(),baudrate);
if(mfd == null){
Log.e(TAG,"native open return null");
}
Log.i(TAG, "SerialPort>>>"+ mfd);
mFileInputStream = new FileInputStream(mfd);
outputStreaml = new FileOutputStream(mfd);
}
public InputStream getInputStream(){
return mFileInputStream;
}
public OutputStream getOutputStream(){
return outputStreaml;
}
private native static FileDescriptor open(String path, int baudrate);
public native void close();
static {
System.loadLibrary("serialport");
}
MainActivity代码为 public void onClick(View view) {
switch (view.getId()){
case R.id.setup:
try{
sp = new SerialPort(new File("/dev/ttyS0"),115200);
}catch (SecurityException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
fileInputStream = sp.getInputStream();
fileOutputStream = sp.getOutputStream();
break;
case R.id.send:
try {
String str = "abc123";
byte[] a = str.getBytes();
byte b = 1;
Log.i(TAG, "send>>> "+a+a+a);
fileOutputStream.write(a);
fileOutputStream.write('\n');
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "send",
Toast.LENGTH_SHORT).show();
break;
case R.id.recevicd:
int size;
try {
byte[] test = new byte;
fileInputStream.read(test);
byte[] buffer = new byte;
if (fileInputStream == null)
return;
Log.i(TAG, "onClick: "+ buffer.length);
Log.i(TAG, "recevied"+buffer.toString());
size = fileInputStream.read(buffer);
Log.i(TAG, "size>>>"+size);
if (size > 0) {
onDataReceived(buffer, size);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
void onDataReceived(final byte[] buffer, final int size) {
runOnUiThread(new Runnable() {
public void run() {
if (editText != null) {
Log.i(TAG, "run: "+new String(buffer, 0, size));
textView.setText(new String(buffer, 0, size));
}
}
});
}
页:
[1]