Serial Port RS232 and ANDROID

Download PDF
The article below is dedicated to the description of the possibility to use COM port in applications for OS Android. It is needed when your application must receive/send data from the other device having only Serial port RS 232 as means of communication.

What do we need?

First, it is necessary to download Android NDK to work with native code in Java.
 http://developer.android.com/tools/sdk/ndk/index.html
It will be necessary to call code on C to work with the serial port; Second, it is necessary to check-out source code of the project from svn to work with Serial port RS 232.
svn checkout

https://code.google.com/archive/p/android-serialport-api/

Organization of the project

It is necessary for us to create a folder /jni and to copy the contents of a folder /jni of the project, downloaded from svn (or simply to copy all folder /jni), in Android project. After that we need to add the following files from the downloaded project:
  • SerialPort.java
  • Application.java
  • SerialPortActivity.java
  • SerialPortFinder.java
The given files allow us to work with the COM port but you will need to edit them to serve the needs of the project. It is known all devices in the systems similar to a *nix one are on a path /dev. To establish path to the device (COM port) and baud rate it is necessary to set appropriate values of fields in Application.java file:
String path = -path to device-;
int baudrate = -baud rate-;
Class SerialPortActivity.java is an extension of Activity class and contains an abstract method protected abstract void onDataReceived (final byte [] buffer, final int size). You can inherit the Activity from this class where there will be work with Serial Port, and to process data acquisition from the port having redefined the method onDataReceived.
@Override
	protected void onDataReceived(final byte[] buffer, final int size) {
		runOnUiThread(new Runnable() {
			public void run() {
					//TO DO your logic

			}
		});
	}
It is possible to extract logic of information system of port in your own class/classes and not to use the inheritance from SerialPortActivity.java. You can receive the lists of all devices and their paths by means of class SerialPortFinder.java with its methods getAllDevices () and getAllDevicesPath () accordingly. Write-In port is carried out by using a simple record in OutputStream created by means of class SerialPort.java, COM port.
mOutputStream.write(new String(“text”).getBytes());
mOutputStream.write('\n');

JNI & NDK

Native code loading in Android application may be found in class SerialPort.java by calling System.loadLibrary (‘ serial_port ‘):
private native static FileDescriptor open(String path, int baudrate, int flags);
	public native void close();
	static {
		System.loadLibrary("serial_port");
	}
The parameter serial_port is a module that has occurred as a result of code C compilation through/via NDK. It is specified in file make /jni/Android.mk. File SerialPort.c in /jni folder contains native functions of the system calls to work with Serial port. Such parameters for COM port as Data bits, Parity, Stop bits and the others may be changed in this file by means of structure termios, for example:
                cfg.c_cflag |= ~PARENB;
		cfg.c_cflag &= ~CSTOPB;
		cfg.c_cflag &= ~CSIZE;
		cfg.c_cflag |= CS8;
(Data bits=8, Parity=none, Stop bits=1)
This information about termios.h may be found under the following link:  http://pubs.opengroup.org/onlinepubs/007908775/xsh/termios.h.html After changing the file SerialPort.c it is necessary to compile libraries as follows:
  • Open command line
  • Go to the NDK folder
  • Set path to the Android project – set NDK_PROJECT_PATH= -path to your android project-
  • Run – ndk-build
The libraries will be compiled and added in a folder /libs of your project. After that it is possible to perform the following command in order to install Android application in a device:
adb install -path to you .apk file-
Chief Product Officer
With a passion for innovation and a keen understanding of market trends, Alexander plays a pivotal role in shaping Magora's product development strategy and ensuring the delivery of cutting-edge solutions to clients.
open
related
What is Icon Font? Android vs iOS App Development: Which is Better for your Business Project? What Cars and Mobile Apps Have in Common: Figuring the Right Functionality, Design and Cost. Access to Oracle Data from SharePoint 2010 Using Business Data Connectivity Model
recent
3 Ways AI is Shaping the Future of Sustainability How to Develop an AI Chatbot for Your Real Estate Business Generational nuances: crafting the user experience in 2024 VisionOS App Development: The Era of Spatial Computing
recommended
Everything You Want to Know About Mobile App Development App Development Calculator Infographics: Magora development process Dictionary
categories
News Technologies Design Business Development HealthTech IoT AI/ML PropTech FinTech EdTech Mobile Apps Discovery Transport&Logistics AR/VR Big Data Sustainability Startup Enterprise Security
Logo Magora LTD
close
Thank you very much.
Magora team

Grab your e-book: Design to attract more buyers

Logo Magora LTD
close
Get in touch
Open list
Open list
Logo Magora LTD
close
Thank you very much.

Your registration to the webinar on the 27th of September at 2 p.m. BST was successfuly completed.
We will send you a reminder on the day before the event.
Magora team
Registration for a webinar

"Let Smart Bots Speed up your Business"
Date: 27.09.2018 Time: 2 p.m. BST
Do you agree to the personal data processing?


Logo Magora LTD
close
Download our curated selection of resources for accelerating your software development journey.