(linux) writing to /dev/ttyUSB*

1,113 views
Skip to first unread message

Jesús Almansa

unread,
May 4, 2011, 5:42:14 AM5/4/11
to JSDB
Hi,

I have some questions about how to write to port /dev/ttyUSB0 on my
linux machine (Ubuntu 10.04 LTS). The questions are about the follow
ing script fragment, which executes as required, though I do not under
stand why:

f = new Stream('/dev/ttyUSB0', 'wt');
f.write('+');
f.get();

Q1.
Mode w for a file stream in the JSDB reference is documented as:
w Create or replace a file with read and write access
Shouldn't it say this instead?
w Open an existing file with write access

Q2.
It is not sufficient for me to call the write method of the stream, in
order to send the data through the port. Why?

Q3.
I need to call method get in order to be able to read data at the
other
end of the port. But I can not make sense of why this method.


Here is the context from where my questions originate.
I use two radio buttons of a webpage to switch on and off an LED that
is wired to an Arduino board.
The Arduino board is connected, with a standard USB cable, to the same
computer where the webpage is served. My computer runs on Ubuntu 10.
04 LTS.

The solution that I have coded follows the approach suggested by Jason
S. at stackoverflow: to use an ad hoc web server at the localhost to
parse
an XMLHttpRequest from the webpage and write an appropriate message
through the USB port.

Jesús Almansa

unread,
May 4, 2011, 3:55:17 PM5/4/11
to JSDB
I should make an additional question.

After thorough testing, I found out that my script could switch on/off
the
LED, only because I had previously used a python script to probe the
serial port to which the Arduino is connected:

#!/usr/bin/env python

import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 9600)
time.sleep(1.5) #Gives Arduino time to reset after opening its serial
port
ser.close()

However, this is not desirable. I would like to have a self-contained
solution to my problem, using only JavaScript scripting.

So my question number zero would be:
Q0.
Why can't I make my JSDB script work without the use of python?

I'd appreciate any insights about this.
--j

Shanti Rao

unread,
May 4, 2011, 4:07:53 PM5/4/11
to js...@googlegroups.com
JSDB file ports are buffered (by JSDB, not the OS) by default. Try "wt+" mode and see if that helps.

Python probably calls ioctl() to set the serial port parameters, like data rate. Perhaps we need a separate Stream constructor for unix, like

new Stream('com://ttyUSB0:9600')

It's also possible to expose ioctl() to JS, but I would rather not. You can run ioctl from the command line on linux, either before starting JSDB or from a system call.

The relevant code for the serial port stream is in rs/io_comm.cpp (which is Windows-only now) and rs/io_file.cpp if you want to muck around with the source.

Shanti

Jesus Almansa

unread,
May 6, 2011, 3:52:40 PM5/6/11
to js...@googlegroups.com
Shanti, thank you for the hints.

JSDB file ports are buffered (by JSDB, not the OS) by default. Try "wt+" mode and see if that helps.

I tried that mode, and it does not help.
 
Python probably calls ioctl() to set the serial port parameters, like data rate. Perhaps we need a separate Stream constructor for unix, like

new Stream('com://ttyUSB0:9600')

Following your suggestion, I straced what both Python and JSDB do.
First of all, both call open() but with the same access mode O_RDWR,
but different creation flags
   [ Python ]  O_NOCTTY
   [ JSDB ] O_CREAT | O_TRUNC
and different file status flags
   [ Python ]  O_NONBLOCK | O_LARGEFILE
As for ioctl(), I could see that Python calls it five times, whereas JSDB
calls it one time, and their structures differ.

Unfortunately, this is as much as I can say, since I do not understand
what is going on. Reading man and googling did not helped me.


It's also possible to expose ioctl() to JS, but I would rather not. You can run ioctl from the command line on linux, either before starting JSDB or from a system call.

I prefer not to do this at the moment. What I am doing, instead, is to
make a system call to run the Python script that makes it possible to
write to /dev/ttyUSB0.

 
The relevant code for the serial port stream is in rs/io_comm.cpp (which is Windows-only now) and rs/io_file.cpp if you want to muck around with the source.

I did check this files, but not knowing C++, nor what the requirements
are,  I can do nothing here.

All in all, it is unfortunate that I can not be of help in this case.
Best,
--j

Reply all
Reply to author
Forward
0 new messages