Developers commonly use serial ports as a means of exchanging data between devices. In this article, we will explore how to establish a communication channel between a C# application running on a Windows computer and a Java Android phone, using the SerialPort class and the UsbSerial library.
Prerequisites
Before we get started, make sure you have the following prerequisites:
- A Windows computer with a serial port.
- Visual Studio or another C# development environment.
- A Java development environment, such as Android Studio.
- An Android phone with a USB port.
Setting up the C# application
To communicate with the Android phone, we’ll use the SerialPort class in C#. Here’s how to set it up:
- Open Visual Studio and create a new C# project.
- Add a reference to the
System.IO.Ports
namespace. - In the
Form_Load
event, create an instance of theSerialPort
class and configure it with the appropriate settings, such as the port name, baud rate, and data bits.
using System.IO.Ports;
private void Form1_Load(object sender, EventArgs e)
{
SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
serialPort.Open();
}In the Form_Closing event, close the serial port:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
serialPort.Close();
}Setting up the Android application
To communicate with the C# application, we’ll use the UsbSerial library in Java. Here’s how to set it up:
- Open Android Studio and create a new Android project.
- Add the following dependency to your app-level
build.gradle
file:
dependencies { implementation 'com.github.felHR85:UsbSerial:6.1.3
dependencies {
implementation 'com.github.felHR85:UsbSerial:6.1.3'
}In the onCreate method of your main activity, create an instance of the UsbSerialDevice class and configure it with the appropriate settings, such as the baud rate and data bits:
import com.felhr.usbserial.*;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbDevice usbDevice = ... // get a reference to the USB device
UsbSerialDevice serialDevice = UsbSerialDevice.createUsbSerialDevice(usbDevice, usbManager);
serialDevice.open();
serialDevice.setBaudRate(9600);
serialDevice.setDataBits(UsbSerialInterface.DATA_BITS_8);
}
temasre.quora.com
- To send and receive data, use the
read
andwrite
methods:
byte[] data = "@temasre".getBytes();
serialDevice.write(data);
byte[] buffer = new byte[16];
int numBytes = serialDevice.read(buffer);
String receivedData = new String(buffer, 0, numBytes);
Conclusion
In this article, we explored how to establish a communication channel between a C# application running on a Windows computer and a Java Android phone, using the SerialPort class and the UsbSerial library. By exchanging data over a serial port, you can enable your applications to communicate with a wide range of devices, such as sensors, actuators, and other micro