DEV Community

Cover image for how to communicate with a serial port using flutter_libserialport package
Ahmed Mannai
Ahmed Mannai

Posted on • Edited on

how to communicate with a serial port using flutter_libserialport package

Hello everyone! Don't you just hate when documentation is scarce and tutorial blog posts are almost non-existent for something you're working on? I've been there. I was working on a Flutter app pertinent to serial port communication and faced the same issue. But now that I've figured it out, you don't have to feel the frustration that comes with the lack of guidance.

By the end of this blog post, you'll learn how to send and receive data through a serial port using Flutter. Let's dive in!

## Getting Started

To start with, we need to add the package named flutter_lib_serial_port to our empty Flutter project. We will then import the lib_serial_port package that we just downloaded.

Here's a bit of code to assist you:



import 'lib_serial_port.dart';


Enter fullscreen mode Exit fullscreen mode

Identifying Available Ports

Once the package is successfully imported, the first thing we need to do is to identify the available ports connected to our PC.

To do that, we will declare a list of strings that will receive the names of the available ports.



List<String> availablePorts;availablePorts = SerialPort.availablePorts;print (availablePorts);


Enter fullscreen mode Exit fullscreen mode

On running this code, you should see a list of the available ports. In my case, there were only three ports, labelled as serial port.

Creating a Serial Port Object

Now we'll need to create a serial port object, let's call it portOne. This object requires the name of the port we got from our list of available ports.



SerialPort portOne = SerialPort('serial_port_three');


Enter fullscreen mode Exit fullscreen mode

The above code will create an object referenced to 'serial_port_three'

After creating the object, it's best to use a try-catch block to handle any serial port errors. If an error occurs while reading or writing to/from the port, we simply print it to the console.

Opening the Port for Communication

Now we can open the port for communication. The openReadWrite method enables opening the port for both reading and writing.

Let's implement that in our code:



portOne.openReadWrite();


Enter fullscreen mode Exit fullscreen mode

Writing to the Port

After successfully opening the port, we can write to it. We have a problem though; the write method takes a Uint8List but what we have is a string.

To make the conversion, a method needs to be created that converts a string to unit8List. The return type of the write method will be the bytes written. Let's print these bytes written to check if the data was written successfully.

Here is how to do it:



String str = 'Hello';Uint8List uint8list = Uint8List.fromList(str.codeUnits);print (portOne.write(uint8list));


Enter fullscreen mode Exit fullscreen mode

On executing this code, a number (5) will be printed, which corresponds to the length of the string 'Hello', indicating the successful write operation.

Reading from the Port

Now let's implement the functionality to read from the serial port. To achieve this goal, we need to create a serial port reader object.

This object provides us with a stream, which we can listen to, and then print the incoming data. The problem here is that the stream returns unit8List data, thereby necessitating the conversion back to a string.

Here is the code snippet to do this:



SerialPortReader reader = SerialPortReader(portOne);reader.stream.listen((data) {print(String.fromCharCodes(data));});


Enter fullscreen mode Exit fullscreen mode

If you've followed along correctly and everything goes without a hitch, you should be able to read data from the serial port successfully!

"Don't be discouraged by what you don't know. That can be your greatest strength and ensure that you do things differently from everyone else." - Sara Blakely

I hope you've found this tutorial useful. Remember, in technology, the best way to learn something new is by doing. Happy coding, and until next time, take care!

Top comments (3)

Collapse
 
chwong profile image
CH.W

I would like to ask, for developing USB-serial communication on a mobile phone, how can I use only one interface to simultaneously transmit serial data and software debug? Through a USB hub? Tks!

Collapse
 
ahmedmannai profile image
Ahmed Mannai

Hey buddy. Honestly, I'm not sure about a single-interface solution. I haven't thought about it. If I come across a solution, I will reach back.

Collapse
 
athikur1 profile image
Athikur • Edited

Is this package support IOS serial communication also? And just sharing my experience, I am using usb_serial flutter package that too works good for android, but I am not sure it will work for IOS because I don't have IOS physical device. Finally, is there any flutter package that supports USB serial communication on IOS.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.