Is it possible to receive serial data into the USB host port on the BeagleBone? For example 'serial' data send from an Arduino over it's USB client output? I guess it would be a matter of having the right USB->TTY driver in the BeagleBone Linux install? And is it possible to receive this data in a Bela program?

I guess something analogous must be happening with the Midi over USB functionality of Bela.

Yes you should be able to use it and receive it in the Bela program.

Any generic tutorial for reading a serial port on Linux in C or C++ should do.
Make sure you read / write your serial port from a separate thread from the audio thread.
A thread on Bela is called AuxiliaryTask, which is a wrapper for Xenomai thread. A number of examples show how to use AuxiliaryTask, for instance:

06-Sensors/capacitive-touch/
11-Extras/bucket-brigade-chorus/
04-Audio/FFT-phase-vocoder/

The first also show how traditional Linux programming techniques (in this case communication over I2C) can be used seamlessly on Bela.

Out of curiosity, what do you need the Arduino for? Is there something specific that runs on it that you would not be able to run on Bela ?

Thanks Giulio 🙂

I'm trying to get an Arduino talking to Bela just because the Arduino is currently being used for an audio-installation in a gallery, with an Inertial Motion Unit attached to an Arduino via I2C, talking to a Mac laptop via USB serial. I need to replace the laptop with something else (as laptop is needed elsewhere) and Bela seems like a good option.

Sometime in the next few weeks, I hope to cut the Arduino out entirely. Actually I originally hoped to make this installation with Bela but time did not permit. The problem is that the installation is in England, I am in Australia, my artistic collaborator in England is not confident with electronics, and the IMU is currently attached to the Arduino and bundled up in a box.

I should probably just try talking my colleague through pulling the box apart and attaching the IMU to Bela, but I thought it might be easier to leave the box intact, plug it into the Bela USB for a couple of weeks until I can post a new box over.

16 days later

Hey dr-offig,

did you manage to get this going?
I'm also trying to set up a serial connection between an Arduino nano (inside an expression pedal 😉 ) and a pd-patch (with libpd).

I hacked together a test .c - file to see if it works at all on the beaglebone, and it does when I run it via ssh.
Sourced together from these tutorials:
https://github.com/xanthium-enterprises/Serial-Port-Programming-on-Linux/blob/master/USB2SERIAL_Read/Reciever%20(PC%20Side)/SerialPort_read.c
and
https://forum.arduino.cc/index.php?topic=288234.0

I posted the resulting SerialPort_read.c here: https://gist.github.com/anonymous/e356e39f783226e0656a2c6cee0c1e75
Compile it with gcc -o SerialPort_read SerialPort_read.c , run it with SerialPort_read.c , and it should post whatever comes from an Arduino Serial at 9600bd (if that is connected to your device as /dev/ttyUSB0 ).
But when I try to squeeze these into the render.cpp - file alongside the pd-patch, it won't get the Serial connection going (although it compiles):
https://gist.github.com/anonymous/61262807963dbbced9e02ce6c1aa8813

And then there's the question of how to get the resulting message back into PD (if i manage to make it connect..).
Where should the parsing part go??

Anyway, curious if you found a solution 🙂
Best,
Jan

    janklug But when I try to squeeze these into the render.cpp - file alongside the pd-patch, it won't get the Serial connection going (although it compiles):
    https://gist.github.com/anonymous/61262807963dbbced9e02ce6c1aa8813

    Not sure how that code can compile: you have lots of function calls at the top that are not within a function, so it surely does not compile.

    Maybe for some bug (e.g.: this ) the IDE does not try to recompile your project? can you clean the project (project settings tab -> manage projects -> clean) and try hitting the run button again?

    I have an Arduino uno here, which shows up as /dev/ttyACM0, the program runs and can successfully open the serial port.
    Try the following:

    • your code is trying to open /dev/ttyUSB1, but the error dialog references ttyUSB0, so maybe you are addressing the wrong device?

    • check you are addressing the device with the correct name. From the console in the IDE, run
      ls /dev/ttyUSB* and see what is there. If nothing shows up, try
      lsusb to check that the USB device is actually recognized

    • I would not be surprised if the Arduino needed to be connected before you power up the BeagleBone. So make sure the Arduino is connected when you start the BeagleBone.

    indeed, it was the wrong device.. it opens with ttyUSB0 🙂
    thanks for checking!

    So the next step is to squeeze in the checking-for-incoming-serial-data, parsing that and sending it to the pd-patch.

    would that go into the render function?
    Anyway, time to take a break; better to look at that tomorrow 😉

    You would need a separate thread to monitor the serial port. You could then set a global variable from it. Then from the audio thread (e.g.: inside render) you would read the content of the global variable and send a message to a receiver in Pd, using a syntax like this:

    	libpd_float("yourReceiver", state);

    this would send to a [r yourReceiver] object in libpd.
    In order to do this you'd need a Pd project with a custom render.cpp file. See the example 08-PureData/customRender.

    On the other hand, the [serial] object is supported by libpd(though I have not tested it), so you may skip all you've done so far and simply try running your Pd patch with the [serial] object in it.

    On the other hand again, I would ask you: why do you need Arduino for this application? Is there no way you could connect your device straight into Bela?

    thanks!

    the libpd-approach seems to make more sense then.
    can't find the [serial] object though. Could it be [comport]?
    I managed to install that on my mac, but can't figure out how to do that with bela...

    but i can try to be patient and wait for the step-by-step guide 😉

    The reason why I attempt it via the Arduino is composed of these parts:
    - I'd like to use the same pedal (and other components) in multiple setups, depending on context; also with Max on my laptop, which works nicely plug-and-play via USB connection.
    - I'd like to learn how to do that
    - experimenting with other sensors that I know how to handle in arduino, incl. pre-cooking the data
    - making it easier to experiment for students who have Arduino experience

    The [serial] object exists in Pd 0.46. To be honest I am not sure what that does, I never used it and there is no help file for it. The little I understand by looking at its source code is that it may be a Win32 only object and may require the GUI and so I have to take back what I said earlier: I don't believe it will work on Bela.

    Also had a look at the source of [comport] and it seems that it is polling the port in the audio thread which is especially evil on Bela.

    You better keep going the custom-render.cpp route outlined above, also because it is the one you will learn the most from.

    ok,that sounds like the best approach then!

    is there anything else necessary to get the connection to pd working than

    1) #include <libpd/z_libpd.h> , #include <libpd/s_stuff.h>
    2) libpd_float("arduino", 440); // in render()
    3) a receive object in pd called 'arduino'
    ?
    Can't get it to work... Do I need to register anything?

    your receiver object should be

     [r arduino] 

    Check out this section of the libpd wiki

    And the render.cpp file should live in the same folder as your project ( to make sure it is getting compiled and linked in, try to add a printf statement in setup() and make sure it shows up on the console).

    alright so I misunderstood your previous question.
    You DO need more than the 3 things you outlined above. Actually you need quite a lot more than that.
    Your project currently does not use libpd at all, does not open a patch, does not feed data through it, does not process Pd messages.

    You'll need to start from core/default_libpd_render.cpp or examples/08-PureData/customRender/render.cpp as a starting point and add your code to it.

    This should be made clearer in the example and in the wiki.

    a year later

    dr-offig looks nice, by the way, what wold be the next step? I mean, would you have a sample render.cpp file which makes actual use of the Serial port, sending and receiving formatted data?