當前位置:首頁 » 安卓系統 » android串口工具

android串口工具

發布時間: 2022-09-06 08:42:54

Ⅰ android 串口調試 為什麼用adb shell命令找不到ttyS2

應該是你的讀寫緩沖設計的太少,或者和界面線程攪和在一起影響接收性能。 通常這類軟體都有或多或少問題,建議備用幾個進行對比測試,流行的如串口調試助手、格西烽火串口助手等,都還不錯。

Ⅱ android中怎麼打開串口功能

1/yiyaaixuexi/article/details/6803593 3.每次都在java層去提權,cmd為提權命令 public void exeShell(String cmd){ try{ Process p = Runtime.getRuntime().exec(cmd); BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream())); String line = null; while ((line = in.readLine()) != null) { Log.i("exeShell",line); } } catch(Throwable t) { t.printStackTrace(); } }

Ⅲ android如何讀取串口數據

樓主問題解決了沒?我用串口調試助手調試,安卓端能發送數據到pc端接收,但反過來pc端發數據過來安卓無法接收,求大神指導啊

Ⅳ 如何使用android studio實現串口通信

找兩根帶驅動程序的U裝RS485的線,分別接在兩電腦上並安裝好驅動程序。(在計算機硬體管理器中查看是COM幾口) 兩根U裝RS485的線A、B對接起來,形成迴路即可通訊 可從網上下載一個串口調試工具,在一端發數據,在另一台電腦的串口一端接收到其發過來的數據。

Ⅳ 如何用Android Studio修改藍牙串口助手的波特率(手機端)

BluetoothChatService.java
的第49行
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
中的字元串不同,於是把他替換成藍牙串口服務 (SPP) 的 UUID
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
保存後運行程序到手機上,把電腦上的藍牙打開,打開電腦上的串口助手,波特率隨便設置就可以了 。

Ⅵ android虛擬機串口通信

工具:Virtual Serial Port Driver.
用這個工具虛擬出一對串口。
下載地址

2.用串口調試助手,測試串口通信。
3.用這個命令啟動虛擬機:emulator @2.2 -scale auto -qemu -serial COM3 &
說明:
2.2:是虛擬機的名稱。
COM3是你要選擇的串口。
ps:在cmd中使用這個命令有兩種方式:1)將安卓的sdk的tools文件夾加入到path環境變數中,2)在安卓的sdk的tools文件夾下打開cmd。
4.虛擬機中測試串口通信用谷歌的一個開源項目:android_serialport_api
5.在虛擬機中運行項目。
說明:運行前要獲取設備的許可權
1)在cmd中用adb shell命令,進入虛擬機命令行環境。

2)打開dev文件夾:cd dev
3)獲取許可權:chmod 777 ttyS2

6.谷歌的開源項目不能導入進eclipse,我整理了一下,調通了。

Ⅶ 誰有沒有Android串口的使用例子

1 首先做的是創建新的工程然後添加一下文件


3代碼

好了 然後就是關於這個頁面的Code了,

這是我的:

package android.serialport;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.BreakIterator;
import java.util.ServiceConfigurationError;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyserialActivity extendsActivity
{
EditText sendedit;
EditText receiveedit;
FileInputStream mInStream;
FileOutputStream mOutStream;
SerialPort classserialport;
ReadThread mReadThread;

private class ReadThread extends Thread
{
public void run()
{
super.run();
while(!isInterrupted())
{
int size;
}
}
}


void onDataReceive(final byte[] buffer,finalint size)
{
runOnUiThread(new Runnable()
{

@Override
publicvoid run()
{
// TODO Auto-generated method stub
if(mReadThread != null)
{
receiveedit.append(newString(buffer,0,size));
}
}
});
}
@Override
protectedvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_myserial);

sendedit= (EditText)findViewById(R.id.editText1);
receiveedit=(EditText)findViewById(R.id.editText2);


receiveedit.setFocusable(false);//進制輸入
/*
* 打開串口
* */
finalButton openserial =(Button)findViewById(R.id.button1);
openserial.setOnClickListener(newView.OnClickListener()
{

@Override
publicvoid onClick(View arg0)
{
//TODO Auto-generated method stub
try
{
classserialport=new SerialPort(new File("/dev/ttyS2"),9600);
}catch(SecurityExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}
mInStream=(FileInputStream) classserialport.getInputStream();
Toast.makeText(MyserialActivity.this,"串口打開成功",Toast.LENGTH_SHORT).show();
}
});
/*
* 發送數據
* */
finalButton sendButton =(Button)findViewById(R.id.button2);
sendButton.setOnClickListener(newView.OnClickListener()
{

@Override
publicvoid onClick(View arg0)
{

Stringindata;
indata=sendedit.getText().toString();
//TODO Auto-generated method stub
try
{
mOutStream=(FileOutputStream) classserialport.getOutputStream();
mOutStream.write(indata.getBytes());
mOutStream.write(' ');
}
catch(IOExceptione)
{
e.printStackTrace();
}
Toast.makeText(MyserialActivity.this,"數據發送成功",Toast.LENGTH_SHORT).show();
sendedit.setText("");
}
});
/*
* 接收數據
* */
finalButton receButton= (Button)findViewById(R.id.button3);
receButton.setOnClickListener(newView.OnClickListener()
{//inttag =0;

@Override
publicvoid onClick(View arg0)
{
// TODO Auto-generated method stub
intsize;
try
{
byte[]buffer = new byte[64];
if(mInStream== null) return;
size= mInStream.read(buffer);
if(size>0)
{
receiveedit.setText("");

}
if(size>0)
{
onDataReceive(buffer,size);
}
inttag =1;

receiveedit.setText(newString(buffer, 0, size));

}catch(IOExceptione)
{
e.printStackTrace();
return;
}
}

privateboolean isInterrupted()
{
// TODO Auto-generated methodstub
returnfalse;
}
});
/*
* 清楚接收區
* */
finalButton ClearButton = (Button)findViewById(R.id.clear);
ClearButton.setOnClickListener(newView.OnClickListener()
{

@Override
publicvoid onClick(View arg0)
{
//TODO Auto-generated method stub
receiveedit.setText("");
}
});}

@Override
publicboolean onCreateOptionsMenu(Menu menu)
{
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.myserial,menu);
returntrue;
}

}

好吧 你做好了。

3需要載入的文件

下面我把所需要添加的代碼貼一貼

第一個是Serialport.java

/*
* Copyright 2009 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.serialport;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.util.Log;

public class SerialPort {

private static final String TAG = "SerialPort";

/*
* Do not remove or rename the field mFd: it is used by native method close();
*/
private FileDescriptor mFd; //創建一個文件描述符對象 mFd
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;

public SerialPort(File device, int baudrate) throws SecurityException, IOException {
/*
* 檢查訪問許可權
* */
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {//如果設備不可讀或者設備不可寫
try {
/* Missing read/write permission, trying to chmod the file *///沒有讀寫許可權,就嘗試去掛載許可權
Process su; //流程進程 su
su = Runtime.getRuntime().exec("/system/bin/su");//通過執行掛載到/system/bin/su 獲得執行
String cmd = "chmod 777 " + device.getAbsolutePath() + " "
+ "exit ";
/*String cmd = "chmod 777 /dev/s3c_serial0" + " "
+ "exit ";*/
su.getOutputStream().write(cmd.getBytes());//進程。獲得輸出流。寫(命令。獲得二進制)
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {//如果 進程等待不是0 或者 設備不能讀寫就
throw new SecurityException();//拋出一個許可權異常
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
/*
*
* */
mFd = open(device.getAbsolutePath(), baudrate);
//device.getAbsolutePath()這是要掛載的路徑new File("/dev/ttyS2")
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();//輸入輸出異常
}
//將文件描述符 做輸入輸出流的參數 傳遞給創建的輸入輸出流
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}

// Getters and setters
public InputStream getInputStream() {
return mFileInputStream;
}

public OutputStream getOutputStream() {
return mFileOutputStream;
}

// JNI
private native static FileDescriptor open(String path, int baudrate);
public native void close();
static {
System.loadLibrary("serial_port");
}
}

第二個是SerialPortFinder.java

/*
* Copyright 2009 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.serialport;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;

import android.util.Log;

public class SerialPortFinder {

/*
* 創建一個驅動程序類
* */
public class Driver {
public Driver(String name, String root) {
mDriverName = name;//String 類型的
mDeviceRoot = root;
}
private String mDriverName;
private String mDeviceRoot;
Vector<File> mDevices = null;
/*
* Vector 類在 java 中可以實現自動增長的對象數組
* 簡單的使用方法如下:
vector<int> test;//建立一個vector
test.push_back(1);
test.push_back(2);//把1和2壓入vector這樣test[0]就是1,test[1]就是2
* */
public Vector<File> getDevices() {
if (mDevices == null) {
mDevices = new Vector<File>();
File dev = new File("/dev");
File[] files = dev.listFiles();
int i;
for (i=0; i<files.length; i++) {
if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) {
Log.d(TAG, "Found new device: " + files[i]);
mDevices.add(files[i]);
}
}
}
return mDevices;
}

public String getName() {
return mDriverName;
}
}
/*
*
*
* */
private static final String TAG = "SerialPort";

private Vector<Driver> mDrivers = null;

Vector<Driver> getDrivers() throws IOException {
if (mDrivers == null) {
mDrivers = new Vector<Driver>();
LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
String l;
while((l = r.readLine()) != null) {
String[] w = l.split(" +");
if ((w.length == 5) && (w[4].equals("serial"))) {
Log.d(TAG, "Found new driver: " + w[1]);
mDrivers.add(new Driver(w[0], w[1]));
}
}
r.close();
}
return mDrivers;
}

public String[] getAllDevices() {
Vector<String> devices = new Vector<String>();
// Parse each driver
Iterator<Driver> itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator<File> itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getName();
String value = String.format("%s (%s)", device, driver.getName());
devices.add(value);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}

public String[] getAllDevicesPath() {
Vector<String> devices = new Vector<String>();
// Parse each driver
Iterator<Driver> itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator<File> itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getAbsolutePath();
devices.add(device);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}
}

第三個是Android.mk

#
# Copyright 2009 Cedric Priscal
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

TARGET_PLATFORM := android-3
LOCAL_MODULE := serial_port
LOCAL_SRC_FILES := SerialPort.c
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

第四個是SerialPort.c

/*
* Copyright 2009 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h>

#include "android/log.h"
static const char *TAG="serial_port";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)

static speed_t getBaudrate(jint baudrate)
{
switch(baudrate) {
case 0: return B0;
case 50: return B50;
case 75: return B75;
case 110: return B110;
case 134: return B134;
case 150: return B150;
case 200: return B200;
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 1800: return B1800;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
case 230400: return B230400;
case 460800: return B460800;
case 500000: return B500000;
case 576000: return B576000;
case 921600: return B921600;
case 1000000: return B1000000;
case 1152000: return B1152000;
case 1500000: return B1500000;
case 2000000: return B2000000;
case 2500000: return B2500000;
case 3000000: return B3000000;
case 3500000: return B3500000;
case 4000000: return B4000000;
default: return -1;
}
}

/*
* Class: cedric_serial_SerialPort
* Method: open
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT jobject JNICALL Java_android_serialport_SerialPort_open
(JNIEnv *env, jobject thiz, jstring path, jint baudrate)
{
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 is;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &is);
LOGD("Opening serial port %s", path_utf);
fd = open(path_utf, O_RDWR | O_DIRECT | O_SYNC);
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;
}

/*
* Class: cedric_serial_SerialPort
* Method: close
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_android_serialport_SerialPort_close
(JNIEnv *env, jobject thiz)
{
jclass SerialPortClass = (*env)->GetObjectClass(env, thiz);
jclass FileDescriptorClass = (*env)->FindClass(env, "java/io/FileDescriptor");

jfieldID mFdID = (*env)->GetFieldID(env, SerialPortClass, "mFd", "Ljava/io/FileDescriptor;");
jfieldID descriptorID = (*env)->GetFieldID(env, FileDescriptorClass, "descriptor", "I");

jobject mFd = (*env)->GetObjectField(env, thiz, mFdID);
jint descriptor = (*env)->GetIntField(env, mFd, descriptorID);

LOGD("close(fd = %d)", descriptor);
close(descriptor);
}

Ⅷ android things 怎麼使用串口通信方式,進入或登錄系統

網路上搜索「串口調試助手」或者「串口調試精靈」,進行簡單的安裝。 點擊運行串口調試助手,進入相應的界面。 設置串口通訊的介面,波特率校驗位等相關信息。 在發送欄輸入要發送的信息,選擇手動發送或者自動發送,這時如果沒有什麼錯誤,對方的串口通信工具就會收到發送的內容。 設置接收區域的相關信息,這時對方如果發送信息,接收區就會收到相應的內容。 另外也可以進行文件的發送,但是串口通信的速率過慢,傳輸文件的話會很耗時,不過可以用來檢測通信的正確性。

Ⅸ Android studio藍牙app的串口是什麼

Androidstudio藍牙app的串口是一種通信的方式。類似於USB只是比USB低級多了,但是手機等設備他沒外置這個串口,解決方式就是手機用藍牙連接一個小硬體,小硬體有個串口他的和單片機連接,來達到手機和單片機的串口連接這種方式就是藍牙串口。

Androidstudio藍牙app的串口特點

藍牙串口是基於SPP協議SerialPortProfile,能在藍牙設備之間創建串口進行數據傳輸的一種設備,藍牙串口的目的是針對如何在兩個不同設備通信的兩端上的應用之間保證一條完整的通信路徑,需要在系統設置里連接上HC05的藍牙,默認配對密碼為1234默認波特率為9600。

默認名為HC05最好在連接後再看一下對應的MAC地址,因為我做的是單一連接單片機同一時間只能接入一個藍牙設備,所以需求對更改連接藍牙模塊需求要求不大,如果用戶不在創建項目時更改設備MAC地址,則使用的就是默認的MAC地址所以也沒有做選擇藍牙設備的模塊。

熱點內容
碧藍航線pc掛機腳本 發布:2024-05-19 02:30:03 瀏覽:588
腳本fir 發布:2024-05-19 02:28:57 瀏覽:260
阿里雲獨享伺服器 發布:2024-05-19 02:23:54 瀏覽:253
織夢源碼ga 發布:2024-05-19 02:23:20 瀏覽:571
java文件名後綴 發布:2024-05-19 02:14:39 瀏覽:956
快手點榜腳本 發布:2024-05-19 02:08:44 瀏覽:163
pythonforinkeys 發布:2024-05-19 01:55:44 瀏覽:793
電腦如何區域網共享文件夾 發布:2024-05-19 01:25:01 瀏覽:69
手機存儲越大性能越好嗎 發布:2024-05-19 01:14:28 瀏覽:177
我的世界hyp伺服器怎麼玩 發布:2024-05-19 00:51:25 瀏覽:801