Skip to content

Latest commit

 

History

History
131 lines (113 loc) · 6.2 KB

README_EN.md

File metadata and controls

131 lines (113 loc) · 6.2 KB

Android-Serialport

Porting Google's official serial port libraryandroid-serialport-api,only supports serial port name and baud rate. This item adds support check digit, data bit, stop bit, flow control configuration item.

Android-Serialport

GitHub forksGitHub issuesGitHub starsSource persentJcenter2.1Demo apk download AppVeyor branchGitHub license

Document

UsageDownload

  1. Gradledependency
implementation 'tp.xmaihh:serialport:2.1'
  1. Mavendependency
<dependency>
  <groupId>tp.xmaihh</groupId>
  <artifactId>serialport</artifactId>
  <version>2.1</version>
  <type>pom</type>
</dependency>

Attribute

Attributes parameter
Baud rate BAUDRATE
Data bit 5,6,7,8 ; default value 8
Check Digit No parity (NONE), odd parity (ODD), even parity (EVEN); default no parity
Stop bit 1,2 ; default value 1
Flow Control No flow control (NONE), hardware flow control (RTS/CTS), software flow control (XON/XOFF); flow control is not used by default

Function

1.List the serial port

serialPortFinder.getAllDevicesPath();

2.Serial port property settings

serialHelper.setPort(String sPort);      //set the serial port
serialHelper.setBaudRate(int iBaud);     //set the baud rate
serialHelper.setStopBits(int stopBits);  //set the stop bit
serialHelper.setDataBits(int dataBits);  //set the data bit
serialHelper.setParity(int parity);      //set the check bit
serialHelper.setFlowCon(int flowcon);    //set the flow control

Serial port property settings must be set before the function 'open()' is executed.

3. Open the serial port

serialHelper.open();

4.Close the serial port

serialHelper.close();

5.Send

serialHelper.send(byte[] bOutArray); // send byte[]
serialHelper.sendHex(String sHex);  // send Hex
serialHelper.sendTxt(String sTxt);  // send ASCII

6.Receiving

 @Override
protected void onDataReceived(final ComBean comBean) {
       Toast.makeText(getBaseContext(), new String(comBean.bRec, "UTF-8"), Toast.LENGTH_SHORT).show();
   }

7.Sticky processing

Support sticky package processing, the reason is seen in the Issue , the provided sticky package processing

  • Not processed (default)
  • First and last special character processing
  • Fixed length processing
  • Dynamic length processing

Supports custom sticky packet processing.

Step 1

The first step is to implement the AbsStickPackageHelper interface.

/**
 * Accept the message, the helper of the sticky packet processing, return the final data through inputstream, need to manually process the sticky packet, and the returned byte[] is the complete data we expected.
 * Note: This method will be called repeatedly until it resolves to a complete piece of data. This method is synchronous, try not to do time-consuming operations, otherwise it will block reading data.
 */
public interface AbsStickPackageHelper {
    byte[] execute(InputStream is);
}

Step 2

Set sticky package processing

serialHelper.setStickPackageHelper(AbsStickPackageHelper mStickPackageHelper);

Demo APK

演示效果

apk Download

PC-side debugging tools Serial debugging tool for Win

Changelog

Added

  • Add support settings to receive data sticky packet processing, support for setting custom sticky packet processing

Added

  • Add support to set check digits, data bits, stop bits, flow control configuration items

Added

  • Basic function, serial port set serial port number, baud rate, send and receive data

FAQ

  • This library does not provide ROOT permissions, please open the serial port '666' permissions yourself.
adb shell  chmod 666 /dev/ttyS1

Contribute

Please do contribute! Issues and pull requests are welcome.

Thank you for your help improving software one changelog at a time!