佚名通过本文主要向大家介绍了android读取串口数据,android 串口通信,android 串口,android串口调试助手,android 串口编程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题:android串口开发不能发送数据
描述:
JAVA 代码
1、
public SerialPort(File device, int baudrate, int flags)
throws SecurityException, IOException {
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("su");
String cmd = "chmod 777 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
mFd = open(device.getAbsolutePath(), baudrate, flags);
if (mFd == null) {
throw new IOException();
}
System.out.println("mFd======"+mFd);
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
2、测试类
final byte[] openBJ=new byte[]{(byte)0x00,(byte)0xBB,(byte)0x19,(byte)0x00};
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String str = ed2.getText().toString();
try {
serialPort = new SerialPort(new File("/dev/ttyS1"), 9600, 0);//创建串口对象,并打开串口
serialPort.getOutputStream().write(openBJ);//获取输出流对象,并写出数据
Toast.makeText(Test.this, "写数据成功", 20).show();
} catch (IOException e) {
serialPort.close();
Toast.makeText(Test.this, "写数据错误", 2000).show();
e.printStackTrace();
}
}
});
C部分代码
JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open
(JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
{
int fd;
speed_t speed;
jobject mFileDescriptor;
/* Check arguments */
{
speed = getBaudrate(baudrate);
if (speed == -1) {
/* TODO: throw an exception */
LOGE("Invalid baudrate");
return NULL;
}
}
/* Opening device */
{
jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
fd = open(path_utf, O_RDWR | flags);
LOGD("open() fd = %d", fd);
(*env)->ReleaseStringUTFChars(env, path, path_utf);
if (fd == -1)
{
/* Throw an exception */
LOGE("Cannot open port");
/* TODO: throw an exception */
return NULL;
}
}
/* Configure device */
{
struct termios cfg;
LOGD("Configuring serial port");
if (tcgetattr(fd, &cfg))
{
LOGE("tcgetattr() failed");
close(fd);
/* TODO: throw an exception */
return NULL;
}
cfmakeraw(&cfg);
cfsetispeed(&cfg, speed);
cfsetospeed(&cfg, speed);
if (tcsetattr(fd, TCSANOW, &cfg))
{
LOGE("tcsetattr() failed");
close(fd);
/* TODO: throw an exception */
return NULL;
}
}
/* Create a corresponding file descriptor */
{
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;
}
3、Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := serial_port
LOCAL_SRC_FILES := SerialPort.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
4、Application.mk
APP_ABI := armeabi armeabi-v7a x86
运行结果 写入数据成功,不报任何异常。但是对方就是收不到任何数据。
我从平板电脑向PC端写。
描述:
android平板电脑串口
我用的平板电脑,通过485转232,平板电脑端是485。我用平板电脑自带的485测试软件可以测通,但是从网上下载的,android串口测试工具就是测试不通,但是端口可以被打开,数据过不去。我自己也写了程序,程序运行结果串口对象创建成果,输出数据流对象创建成果,写出数据成果,但对方就是收不到收据,还是不通信。JAVA 代码
1、
public SerialPort(File device, int baudrate, int flags)
throws SecurityException, IOException {
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("su");
String cmd = "chmod 777 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
mFd = open(device.getAbsolutePath(), baudrate, flags);
if (mFd == null) {
throw new IOException();
}
System.out.println("mFd======"+mFd);
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
2、测试类
final byte[] openBJ=new byte[]{(byte)0x00,(byte)0xBB,(byte)0x19,(byte)0x00};
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String str = ed2.getText().toString();
try {
serialPort = new SerialPort(new File("/dev/ttyS1"), 9600, 0);//创建串口对象,并打开串口
serialPort.getOutputStream().write(openBJ);//获取输出流对象,并写出数据
Toast.makeText(Test.this, "写数据成功", 20).show();
} catch (IOException e) {
serialPort.close();
Toast.makeText(Test.this, "写数据错误", 2000).show();
e.printStackTrace();
}
}
});
C部分代码
JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open
(JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
{
int fd;
speed_t speed;
jobject mFileDescriptor;
/* Check arguments */
{
speed = getBaudrate(baudrate);
if (speed == -1) {
/* TODO: throw an exception */
LOGE("Invalid baudrate");
return NULL;
}
}
/* Opening device */
{
jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
fd = open(path_utf, O_RDWR | flags);
LOGD("open() fd = %d", fd);
(*env)->ReleaseStringUTFChars(env, path, path_utf);
if (fd == -1)
{
/* Throw an exception */
LOGE("Cannot open port");
/* TODO: throw an exception */
return NULL;
}
}
/* Configure device */
{
struct termios cfg;
LOGD("Configuring serial port");
if (tcgetattr(fd, &cfg))
{
LOGE("tcgetattr() failed");
close(fd);
/* TODO: throw an exception */
return NULL;
}
cfmakeraw(&cfg);
cfsetispeed(&cfg, speed);
cfsetospeed(&cfg, speed);
if (tcsetattr(fd, TCSANOW, &cfg))
{
LOGE("tcsetattr() failed");
close(fd);
/* TODO: throw an exception */
return NULL;
}
}
/* Create a corresponding file descriptor */
{
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;
}
3、Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := serial_port
LOCAL_SRC_FILES := SerialPort.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
4、Application.mk
APP_ABI := armeabi armeabi-v7a x86
运行结果 写入数据成功,不报任何异常。但是对方就是收不到任何数据。
我从平板电脑向PC端写。