rpi_serial 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

rpi_serial #

rpi_serial is a Dart package for using serial communication on the Raspberry Pi.

Overview #

  • The Serial library provides the API for accessing devices using the serial protocol

  • The RpiSerial library provides implementation of the serial protocol on the Raspberry Pi derived from the WiringPi library.

Setup #

Be sure to enable serial on the Raspberry Pi using

sudo raspi-config

Also make sure to disable the console on the serial port.

RpiSerial uses a native library written in C. For security reasons, authors cannot publish binary content to pub.dev, so there are some extra steps necessary to compile the native library on the RPi before this package can be used. These two steps must be performed when you install and each time you upgrade the rpi_serial package.

  1. Activate the rpi_serial package using the pub global command.
pub global activate rpi_serial
  1. From your application directory (the application that references the rpi_serial package) run the following command to build the native library
pub global run rpi_serial:build_lib
  1. Deactivate the rpi_serial package.
pub global deactivate rpi_serial

pub global activate makes the Dart scripts in the rpi_serial/bin directory runnable from the command line. pub global run rpi_serial:build_lib runs the rpi_serial/bin/build_lib.dart program which in turn calls the build_lib script to compile the native librpi_serial_ext.so library for the rpi_serial package.

Connecting #

Refer to Raspberry Pi GPIO for connecting your serial device to the Raspberry Pi. By default pins 8 is TX and 10 is RX on the GPIO header.

Note that the Raspberry Pi actually has two UARTs. On the versions with Bluetooth (Raspberry Pi 3 and Raspberry Pi Zero W), the UART exposed to the GPIO pins is "/dev/ttyS0", while it is "/dev/ttyAMA0" on the rest of the boards. This can be changed (see Rapsberry Pi UART configuration). On the compute module, you can supposedly expose both serial interfaces on the GPIO.

Examples #

basic_device is a class containing most of the basic features that you would need to comunicate with a serial device. This is implemented using the API (Serial). No low-level interactions required. If you want to communicate with another device, you should probably create a similar class, but specific to that device.

example is for demonstrating basic read and write operations using the basic_device class.

Hopefully the examples are pretty self-explanatory.

Changelog #

0.1.0 #

  • Initial implementation

example/example.dart

import 'dart:math';
import 'dart:async';
import 'package:rpi_serial/rpi_serial.dart';
import 'basic_device.dart';

main() async {
  final serial_connection = RpiSerial();
  await testSensor(BasicSerialDevice(serial_connection));
  serial_connection.dispose();
}

// Sends a random number between [0-9] to the serial port, and reads the incoming character.
// Place a jumper between the TX and RX pins to read the characters
testSensor(BasicSerialDevice device) async {
  print(
      "Entering endless loop mode. Please connect a cable between the rx and tx pins.");
  while (true) {
    device.writeChar(Random().nextInt(10).toString());
    print(device.readChar());
    await Future.delayed(Duration(milliseconds: 500));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  rpi_serial: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:rpi_serial/rpi_serial.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: other

Primary library: package:rpi_serial/rpi_serial.dart with components: nativewrappers.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
path >=1.0.0 <2.0.0 1.6.4
Dev dependencies
grinder ^0.8.0
pedantic ^1.1.0
test ^1.2.0