Skip to content

Latest commit

History

301 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Serial

Build binaries

the deno mascot dinosaur standing in the rain

A serial library written in TypeScript for Deno without any third party modules.

This library provides an interface for the communication with serial devices and doesn't use any third party modules. It uses C++ functions which are included in a dynamic link library or shared object. These functions are then loaded by deno to establish a serial connection and talk to the devices.


Warning

This library and the documentation are still work in progress!

Danger

Currently the library does not work correctly!

Features

  • Communication with serial devices.
  • Create multiple serial connections at the same time.
  • List available ports and their properties.
  • Set timeouts for both reading and writing.
  • All functions are async (currently not implemented yet).
  • Uses no third party modules.
  • Works on different operating systems (check compatibility for mor info).

Compatibility

OSTested versionCurrent state
WindowsWindows 10 (x64)implemented
LinuxUbuntu Server 22.04 LTSimplemented

Examples - How to use

To use this library you need the following flags to run it:

  • --unstable
  • --allow-ffi

Ports

Get a list with all serial ports and their info that are currently available on your system.

main.ts

import{Serial}from"./mod.ts";// create new instance of a serial objectconstserial=newSerial();// get all available portsconstavailablePorts=serial.getPortsInfo();// console log the listconsole.log(availablePorts);

Example output:

[
{ name: 'COM1' },
{ name: 'COM2' },
...
{ name: 'tty/USB1' }
]

Sending

Send data to a serial device. For exampe to an Arduino.

main.ts

import{Serial,baudrate}from"./mod.ts";// create new instance of a serial objectconstserial=newSerial();// open the connectionserial.open('COM1',baudrate.B9600);// encode the message to a Uint8ArrayconsttextToSend='Hello from TypeScript!';constencodedTextToSend=newTextEncoder().encode(textToSend);// send the messageserial.send(encodedTextToSend,encodedTextToSend.length);

Reading

Read data from a serial device. Again, in our example from an Arduino.

Depending on your Arduino board you may need to press the reset button on the board after uploading the sketch, in order to receive data.

sketch.ino

voidsetup() {
Serial.begin(9600);
while (!Serial) {
;
}
}
voidloop() {
Serial.println("Hello from Arduino!");
}

main.ts

import{Serial,baudrate}from"./mod.ts";// create new instance of a serial objectconstserial=newSerial();// open the connectionserial.open('COM1',baudrate.B9600);// create a new buffer to store incoming bytes,// in this example we want to read a maximum of 100 bytesconstbufferToRead=newUint8Array(100);// read data into the bufferserial.read(bufferToRead,bufferToRead.length);// decode the data from the bufferconstdecodedTextToRead=newTextDecoder().decode(bufferToRead);// console log the textconsole.log(decodedTextToRead);

Example output:

Hello from Arduino!

WIP

import{Serial,baudrate}from"./mod.ts";constserial=newSerial();constavailablePorts=serial.getAvailablePorts()console.log('serial properties (available ports):',availablePorts);console.log('serial properties (isOpen):',serial.isOpen);console.log('serial.open():',serial.open(availablePorts[1].name,baudrate.B9600));consttextToSend='Hello World!'constdataToSend=newTextEncoder().encode(textToSend);console.log('serial.write():',serial.write(dataToSend,dataToSend.length,10,10));console.log('writing:',textToSend);constdataToRead=newUint8Array(20);console.log('serial.read()',serial.read(dataToRead,dataToRead.length,10,10));console.log('reading:',newTextDecoder().decode(dataToRead));console.log('serial.close()',serial.close());

Arduino:

char incomingBytes[10];
int charsRead = 0;
voidsetup() {
Serial.begin(9600);
while (!Serial) {
;
}
}
voidloop() {
int available = Serial.available();
if (available > 0) {
charsRead = Serial.readBytes(incomingBytes, available);
Serial.print(incomingBytes);
}
}

Credits

  • Big thanks goes out to @Katze719 who wrote most of the C++ files and functions!
  • Thanks to @AapoAlas for the great support and help on the Deno Discord!

Licence

Apache-2.0. Check LICENSE for more details. Feel free to contribute to this project.

Copyright 2023 © Max

About

A serial library written in TypeScript for Deno without any third party libraries.

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages