How to use UART Serial Port Using BoneScript Library

5,635 views
Skip to first unread message

Nick Farrell

unread,
Dec 19, 2013, 1:49:30 AM12/19/13
to beagl...@googlegroups.com
I am a newbie to BeagleBone Black(BBB) but have good knowledge about Arduino. I would like to know how to open a serial port in BBB using the 4 UARTs available in BBB using BoneScript library and use cloud9 ide to see the serial data on the console. Can anyone help me on this issue.

Mark A. Yoder

unread,
Mar 27, 2014, 4:59:00 PM3/27/14
to beagl...@googlegroups.com
Nick:
  I have some bonescript code that works with the UART, but I'm not using the built-in bonescript calls. It works fine with a GPS, though I don't use it to transmit.

I took would like to see an example that uses the bonescript calls.  Before ruing the code you need to:

beagle# npm install -g serialport

beagle# echo BB-UART4 > /sys/devices/bone_capemgr.*/slots

--Mark
#!/usr/bin/env node

var b = require('bonescript');
var nmea = require('nmea');

//console.log(b.serialOpen);

//var sp = b.serialOpen('/dev/ttyO4', {baudrate: 9600} );
// parser: b.serialParsers.readline("\n")});


var serialport = require("serialport");
var SerialPort = serialport.SerialPort; // localize object constructor

var sp = new SerialPort("/dev/ttyO4", {
    parser: serialport.parsers.readline("\n")
});

sp.on("data", function (data) {
    console.log("here: "+data);
    console.log(nmea.parse(data));
    });

Jason Kridner

unread,
Apr 7, 2014, 1:13:13 PM4/7/14
to beagl...@googlegroups.com


On Thursday, March 27, 2014 4:59:00 PM UTC-4, Mark A. Yoder wrote:
Nick:
  I have some bonescript code that works with the UART, but I'm not using the built-in bonescript calls. It works fine with a GPS, though I don't use it to transmit.

I took would like to see an example that uses the bonescript calls.

I haven't had a chance to try it out as I don't have a device easy to wire-up until later today, but can you try out this live in-a-webpage example at: http://jsfiddle.net/jkridner/AjnJs/

 
 Before ruing the code you need to:

beagle# npm install -g serialport

BoneScript simply uses this same library, so using BoneScript avoids needing to install it.
 

beagle# echo BB-UART4 > /sys/devices/bone_capemgr.*/slots

BoneScript loads this same overlay for you.

A basic listening example would be:

var b = require('bonescript');
var port = '/dev/ttyO4';
var options = {
    baudrate: 115200
};

b.serialOpen(port, options, onSerial);

function onSerial(x) {
    if (x.err) {
        console.log('***ERROR*** ' + JSON.stringify(x));
    }
    if (x.event == 'open') {
       console.log('***OPENED***');
    }
    if (x.event == 'data') {
        console.log(String(x.data));
    }
}

To write, you'd do:

b.serialWrite(port, data);


Hopefully you'll see some value in the simplicity.

Mark A. Yoder

unread,
Apr 9, 2014, 10:55:57 AM4/9/14
to beagl...@googlegroups.com
It almost works out of the box. I changed baudrate to 9600 for my GPS and data shows up.  However I wanted one line of data at a time so I added:

var options = {
    baudrate: 9600,
    parser: b.serialParsers.readline("\n")
};

But serialParsers isn't defined!  I added the following line at the end of /usr/local/lib/node_modules/bonescript/serial.js

exports.serialParsers = m.module.parsers;

and it works!

--Mark

p.s.  How do I do a pull request on bonescript?

On Thursday, December 19, 2013 1:49:30 AM UTC-5, Nick Farrell wrote:

naik...@gmail.com

unread,
Jun 24, 2014, 2:47:59 PM6/24/14
to beagl...@googlegroups.com
This thread saved my sanity.  Thank you so much.

amer.a...@gmail.com

unread,
Sep 2, 2014, 9:02:35 AM9/2/14
to beagl...@googlegroups.com
Hello Mark and Jason,
  I tried using your script in cloud9 as well as using node and I get the same error:

b.serialOpen(port, options, onSerial);
  ^
TypeError: Object #<Object> has no method 'serialOpen'


What am I missing?


Thank you,

Amer

mxst...@gmail.com

unread,
Nov 12, 2014, 12:10:50 AM11/12/14
to beagl...@googlegroups.com
While not the same, I thought your error looked a little like this one.  

"The GPIO pins are only accessible by 'root'." (with sudo it works).

Hope it helps.


On Friday, September 12, 2014 7:44:58 PM UTC-7, jfsbac wrote:
I tried to use this example to write to a device I have connected to serial port /dev/ttyO1.  I know the device answers because I can use screen and get responses.  My code to send out "/sh/s" is:

var b = require('bonescript');

var cmd = '/sh/s';

var port = '/dev/ttyO1';

var options = {

    baudrate: 9600

};


b.serialOpen(port, options, onSerial);


b.serialWrite(port, cmd);


function onSerial(x) {

    if (x.err) {

        console.log('***ERROR*** ' + JSON.stringify(x));

    }

    if (x.event == 'open') {

       console.log('***OPENED***');

    }

    if (x.event == 'data') {

        console.log(String(x.data));

    }

}


I get the following response:

debian@beaglebone:~/nodews$ node shedrd.js 


/usr/local/lib/node_modules/bonescript/my.js:55

            var slotRegex = new RegExp('\\d+(?=\\s*:.*,bs.*' + pin.key + ')', 

                                                                  ^

TypeError: Cannot read property 'key' of undefined

    at Object.exports.load_dt (/usr/local/lib/node_modules/bonescript/my.js:55:67)

    at Object.newFunction [as serialOpen] (/usr/local/lib/node_modules/bonescript/my.js:228:25)

    at Object.<anonymous> (/home/debian/nodews/shedrd.js:8:3)

    at Module._compile (module.js:456:26)

    at Object.Module._extensions..js (module.js:474:10)

    at Module.load (module.js:356:32)

    at Function.Module._load (module.js:312:12)

    at Function.Module.runMain (module.js:497:10)

    at startup (node.js:119:16)

    at node.js:902:3

debian@beaglebone:~/nodews$


Thanks for the help

Jonathan


On Monday, April 7, 2014 1:13:13 PM UTC-4, Jason Kridner wrote:

Yoder, Mark A

unread,
Nov 12, 2014, 11:52:09 AM11/12/14
to beagl...@googlegroups.com

Is that /dev/tty oh one /dev/tty zero one

 

--Mark

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/r1jMChzokaM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark A. Yoder

unread,
Apr 9, 2014, 10:30:34 AM4/9/14
to beagl...@googlegroups.com
Jason:
  I just cut and pasted your code and it worked!  Thanks...

--Mark


On Monday, April 7, 2014 1:13:13 PM UTC-4, Jason Kridner wrote:

jfsbac

unread,
Sep 12, 2014, 10:44:58 PM9/12/14
to beagl...@googlegroups.com
I tried to use this example to write to a device I have connected to serial port /dev/ttyO1.  I know the device answers because I can use screen and get responses.  My code to send out "/sh/s" is:

var b = require('bonescript');

var cmd = '/sh/s';

var port = '/dev/ttyO1';

var options = {

    baudrate: 9600

};


b.serialOpen(port, options, onSerial);


b.serialWrite(port, cmd);


function onSerial(x) {

    if (x.err) {

        console.log('***ERROR*** ' + JSON.stringify(x));

    }

    if (x.event == 'open') {

       console.log('***OPENED***');

    }

    if (x.event == 'data') {

        console.log(String(x.data));

    }

}


I get the following response:

debian@beaglebone:~/nodews$ node shedrd.js 


/usr/local/lib/node_modules/bonescript/my.js:55

            var slotRegex = new RegExp('\\d+(?=\\s*:.*,bs.*' + pin.key + ')', 

                                                                  ^

TypeError: Cannot read property 'key' of undefined

    at Object.exports.load_dt (/usr/local/lib/node_modules/bonescript/my.js:55:67)

    at Object.newFunction [as serialOpen] (/usr/local/lib/node_modules/bonescript/my.js:228:25)

    at Object.<anonymous> (/home/debian/nodews/shedrd.js:8:3)

    at Module._compile (module.js:456:26)

    at Object.Module._extensions..js (module.js:474:10)

    at Module.load (module.js:356:32)

    at Function.Module._load (module.js:312:12)

    at Function.Module.runMain (module.js:497:10)

    at startup (node.js:119:16)

    at node.js:902:3

debian@beaglebone:~/nodews$


Thanks for the help

Jonathan


On Monday, April 7, 2014 1:13:13 PM UTC-4, Jason Kridner wrote:

ld...@ldeo.columbia.edu

unread,
Sep 28, 2018, 3:59:36 PM9/28/18
to BeagleBoard
Hi All,
I am a new bee in BBB too, but have some knowledge about the serial device and data-acquisition.  I have read all the emails below, and understood the code. The problem is the command to link the UART4 to /dev/tty failed.

debian@beaglebone:/var/lib/cloud9$ echo BB-UART4 > /sys/devices/bone_capemgr9/slots
bash: /sys/devices/bone_capemgr9/slots: No such file or directory


Cannot find the bone_capemgr in the /sys/devices in this board.  Please advice.

ls -l /sys/devices/
total 0
drwxr-xr-x  5 root root 0 Sep 28 17:22 armv7_cortex_a8
drwxr-xr-x  3 root root 0 Sep 28 17:22 breakpoint
drwxr-xr-x 22 root root 0 Sep 28 17:22 platform
drwxr-xr-x  3 root root 0 Sep 28 17:22 soc0
drwxr-xr-x  3 root root 0 Sep 28 17:22 software
drwxr-xr-x  6 root root 0 Sep 28 17:22 system
drwxr-xr-x  3 root root 0 Sep 28 17:22 tracepoint
drwxr-xr-x 12 root root 0 Sep 28 17:22 virtual


Lingling

Jason Kridner

unread,
Oct 1, 2018, 12:54:17 PM10/1/18
to beagl...@googlegroups.com
On Fri, Sep 28, 2018 at 1:40 PM <ld...@ldeo.columbia.edu> wrote:
Hi All,
I am a new bee in BBB too, but have some knowledge about the serial device and data-acquisition.  I have read all the emails below, and understood the code. The problem is the command to link the UART4 to /dev/tty failed.

debian@beaglebone:/var/lib/cloud9$ echo BB-UART4 > /sys/devices/bone_capemgr9/slots
bash: /sys/devices/bone_capemgr9/slots: No such file or directory
 


Cannot find the bone_capemgr in the /sys/devices in this board.  Please advice.

For newer images, capemgr is no longer supported as a kernel service. Instead, the 'config-pin' utility is used to enable pins to provide access to a UART.

Try:

config-pin p9.13 uart
config-pin p9.11 uart


It is still possible to use the bootloader (u-boot) based capemgr via /boot/uEnv.txt, but I don't recommend it as it is error prone and far less flexible.

It is also possible to utilize the mainline device tree overlays, but that process is also error prone and less than intuitive.

Please report back on your success with config-pin.
 
--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/c5d7fc58-966c-44c0-ac67-2d9c3d5a6590%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Ling Ling Dong

unread,
Oct 1, 2018, 2:18:21 PM10/1/18
to beagl...@googlegroups.com, jkri...@beagleboard.org, Nicholas Frearson
Hi Jason,
Thanks for the message. I configured the pins, and confirmed it works using the screen command as below. Does that mean that the two pins are ready as the serial pins where I can transfer data with different baud-rates?
----------------------------------------
config-pin p9.13 uart
config-pin p9.11 uart
screen /dev/ttyO4   -- confirm it works
---------------------------------------
Thank you,

LLD



You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/r1jMChzokaM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/CA%2BT6QPk4j3Ks%2BwSFsMtVCvfRftMdwqjoTSRZxk40Q2O%3DA8Ms3A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages