Android開發(35) 使用android_serialport_api 操作串口斑馬印表機
- 2020 年 3 月 16 日
- 筆記
概述
使用android設備操作串口的 斑馬GK888T印表機,使用印表機列印二維碼。
硬體設備連接方式:
Android設備 通過 串口RS232 連接 斑馬印表機的串口
串口操作類庫 android_serialport_api
使用Android設備操作串口的問題。 我找到一個框架:android_serialport_api,這個框架被託管在:
https://code.google.com/p/android-serialport-api/ Google的程式碼庫,無奈中國無法下載
https://github.com/cepr/android-serialport-api GITHUB的地址,這個可以下載
步驟
下載後,閱讀下源程式碼,準備使用。
1.拷貝 jni 文件夾下的文件到 你的project中, 這些是jni調用的設定文件,包括:
Android.mk Application.mk gen_SerialPort_h.sh SerialPort.c SerialPort.h
2.拷貝libs 下的文件到你的 project中,這些是原生庫,包括
armeabi/libserial_port.so armeabi-v7a/libserial_port.so x86/libserial_port.so
3.在你的項目下新建 package: android_serialport_api,拷貝下列src下的class 到這個package下
Application.java SerialPort.java SerialPortActivity.java SerialPortFinder.java
注意, package名稱一定要是android_serialport_api。或者你需要修改Android.mk下對應的模組配置項。不然會提示找不到jni調用的庫
4.拷貝資源文件等:
string.xml 的內容:
<string name="error_configuration">Please configure your serial port first.</string> <string name="error_security">You do not have read/write permission to the serial port.</string> <string name="error_unknown">The serial port can not be opened for an unknown reason.</string>
5.修改AndroidManifest.xml,在application節點指定對應的 "android:name" 配置,如下面紅色文字所示
<application android:allowBackup="true" android:name="android_serialport_api.Application" android:theme="@style/AppTheme" >
6.下面寫測試的activity。我的設備連接在Android設備的埠 」ttyS2」上,下面是個演示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:keepScreenOn="true" android:orientation="vertical" > <EditText android:id="@+id/EditTextReception" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="7" android:gravity="top" android:hint="Reception" android:isScrollContainer="true" android:scrollbarStyle="insideOverlay" > </EditText> <EditText android:id="@+id/EditTextEmission" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Emission" android:lines="4" android:text="" > </EditText> <Button android:id="@+id/btnSend" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Send" /> </LinearLayout> /* * 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 copy 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 zyf.serialportdemo; import java.io.IOException; import zyf.serialportdemo.R; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android_serialport_api.SerialPortActivity; public class ConsoleActivity extends SerialPortActivity { Button btnSend; EditText mReception; EditText mEmission; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.console); // setTitle("Loopback test"); mReception = (EditText) findViewById(R.id.EditTextReception); mEmission = (EditText) findViewById(R.id.EditTextEmission); btnSend = (Button)findViewById(R.id.btnSend); btnSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String text = mEmission.getText().toString(); try { mOutputStream.write(new String(text).getBytes()); mOutputStream.write('n'); } catch (IOException e) { e.printStackTrace(); } } }); //發送指令到斑馬印表機 mEmission.setText("^XA^A0N,40,30^FO50,150^FDHELLO WORLD^FS^XZ"); /*二維碼指令 ^XA ^PMY ^FO200,200^BQ,2,10 ^FDD03040C,LA,012345678912AABBqrcode^FS ^XZ */ } @Override protected void onDataReceived(final byte[] buffer, final int size) { runOnUiThread(new Runnable() { public void run() { if (mReception != null) { mReception.append(new String(buffer, 0, size)); } } }); } } /* * 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 copy 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_api; import java.io.File; import java.io.IOException; import java.security.InvalidParameterException; import android.content.SharedPreferences; public class Application extends android.app.Application { public SerialPortFinder mSerialPortFinder = new SerialPortFinder(); private SerialPort mSerialPort = null; public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException { if (mSerialPort == null) { /* Read serial port parameters */ //SharedPreferences sp = getSharedPreferences("android_serialport_api.sample_preferences", MODE_PRIVATE); //String path = sp.getString("DEVICE", ""); //String path = "ttyS2"; String path = "/dev/ttyS2";//指定埠 //int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1")); int baudrate = 9600;//指定速率 /* Check parameters */ if ( (path.length() == 0) || (baudrate == -1)) { throw new InvalidParameterException(); } /* Open the serial port */ mSerialPort = new SerialPort(new File(path), baudrate, 0); } return mSerialPort; } public void closeSerialPort() { if (mSerialPort != null) { mSerialPort.close(); mSerialPort = null; } } }
最後別忘了一個操作許可權的問題,很多設備直接操作串口,會提示無許可權 read/write 的問題,需要java層去提權,方法如下:
使用下面的方法執行指令: chmod 777 /dev/ttyS2
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(); } }
手動解決方法:
打開cmd,進入 adb shell,執行:chmod 777 /dev/ttyS2
https://code.google.com/p/android-serialport-api/
https://github.com/cepr/android-serialport-api
http://blog.csdn.net/imyang2007/article/details/8331800
http://blog.csdn.net/imyang2007/article/details/8331800
http://bbs.csdn.net/topics/380234030