Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and write data to COM/Serial ports in Android? [duplicate]

I have to read and write data to the COM ports of a device using Android. I am using javax.comm package for that, but when I installed the apk file, it is not displaying any ports of the device. Is there any permission which I need to add in the manifest file?

like image 677
piks Avatar asked Jun 13 '12 09:06

piks


People also ask

How do I find COM ports on Android?

From the App menu, choose Port ScannerEnter the destination DNS/hostname/IP address of the server hosting the EveryonePrint Mobile Gateway and the port to test, ie port 9444 for the Mobile Gateway TCP port. Verify that the port is available.

How do you read a COM port value?

In Serial Port Reader go to the “Main menu”, choose “Session -> New session”. Alternately, you can click on the “New” icon on the main toolbar or press “Ctrl + N”. This invokes the “New monitoring session” screen. Terminal view – all received data is displayed in ASCII characters on a text console.

Can JavaScript access serial port?

JavaScript itself doesn't have any built in functionality to allow you to access the serial port. However, various JavaScript engines (v8, rhino, etc) allow you to write your own custom native objects.


2 Answers

Your problem is one with operating systems. Android runs Linux under the hood, and Linux treats serial ports differently than Windows does. javax.comm also contains win32com.dll, a driver file, which you won't be able to install on an Android device. If you do find a way to achieve what you're trying to do, you can't actually look for a "COM" port in a Linux environment. The serial ports will go by different names.

 Windows Com Port   Linux equivalent  
      COM 1           /dev/ttyS0  
      COM 2           /dev/ttyS1
      COM 3           /dev/ttyS2 

So, hypothetically, if your idea were to work, you have to look for these names.

Luckily for you, Android does have provisions for interfacing with USB devices (Which I assume you want to connect to, as opposed to parallel or RS-232 ports). To do this, you will set up your device as a USB Host. Here's what you'll want to do:

  1. Get a USBManager.
  2. Find your device.
  3. Get the USBInterface and USBEndpoint.
  4. Open a connection.
  5. Transfer data.

Here's my rough estimate of how you'll do it. Your code will, of course, have a more mature way of doing things.

String YOUR_DEVICE_NAME;
byte[] DATA;
int TIMEOUT;

USBManager manager = getApplicationContext().getSystemService(Context.USB_SERVICE);
Map<String, USBDevice> devices = manager.getDeviceList();
USBDevice mDevice = devices.get(YOUR_DEVICE_NAME);
USBDeviceConnection connection = manager.openDevice(mDevice);
USBEndpoint endpoint = device.getInterface(0).getEndpoint(0);

connection.claimInterface(device.getInterface(0), true);
connection.bulkTransfer(endpoint, DATA, DATA.length, TIMEOUT);

Extra material for your reading pleasure: http://developer.android.com/guide/topics/connectivity/usb/host.html

like image 165
gobernador Avatar answered Sep 17 '22 21:09

gobernador


I am no expert, but for all those who are looking to connect serial RS-232 ports or open a serial port and can't find their device trough the UsbManager, you can find all your devices using an approach like this:

mDrivers = new Vector<Driver>();
LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
String l;
while ((l = r.readLine()) != null) {
    String drivername = l.substring(0, 0x15).trim();
    String[] w = l.split(" +");
    if ((w.length >= 5) && (w[w.length - 1].equals("serial"))) {
        mDrivers.add(new Driver(drivername, w[w.length - 4]));
    }
}

After finding all drivers, iterate trough all the drivers to get all of your devices, using something like this:

mDevices = new Vector<File>();
File dev = new File("/dev");

File[] files = dev.listFiles();


if (files != null) {
    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]);
        }
    }
}

Here is the Driver class constructor, with two member variables:

public Driver(String name, String root) {
    mDriverName = name;
    mDeviceRoot = root;
}

For opening a serial port you can use the Android SerialPort API. Simply open the serial port at your device and write. (You must know your device path and baud rate. For example, my device is ttyMt2 with baud Rate 96000.)

int baudRate = Integer.parseInt("96000");
mSerialPort = new SerialPort(mDevice.getPath(), baudRate, 0);
mOutputStream = mSerialPort.getOutputStream();
byte[] bytes = hexStr2bytes("31CE");
mOutputStream.write(bytes);

Instead of wasting time on this code, you can download the complete project from https://github.com/licheedev/Android-SerialPort-Tool.

like image 44
Muhammad Ali Avatar answered Sep 18 '22 21:09

Muhammad Ali