Pulling Sensor Data From Teensy/Arduino Environment into C Program

Don Kelly

Well-known member
What's the easiest way to pull in sensor data from the Arduino/Teensy environment into a separate C program?

My application is that I'm reading real-time data from a Nicla Sense ME, and I want to pass that data over to a real-time C program I'm running (an SDR application).

I've been experimenting with trying this via the serial port (TTYUSB0), and it seems like a real pain, and I'm not having success.

Is there a smarter or easier way that I might approach this?

For my app, C++ or C# is not an option, I need to stick to C.

Thanks for any suggestions!!
Don
 
What Teensy are you trying to use? What is the C program running on ? I am assuming some form of Linux from your TTYUSB0

But for example lets assume Teensy 4.1 (and most all others). It typically does not show up on host as /dev/ttyUSB0
Devices with this device address /dev/ttyUSBx are typically ones that use the FTDI chips and drivers.
On most linux systems, the Teensy typically shows up as /dev/ttyACMx like /dev/ttyACM0

And you usually need to make sure the Teensy udev rules is installed on your host. More details at: https://www.pjrc.com/teensy/td_download.html


To do simple Serial stuff on linux (Been awhile), I have used termios code to setup to send data back and forth.


You can also maybe try using RAWHID as usb type... More details up at: https://www.pjrc.com/teensy/rawhid.html
 
KurtE,

Yes, I'm on Ubuntu 20.04, running on x86 PC. The C program is the interface for an SDR comms link (TX side). So I'm wanting to get sensor data from the Nicla via Teensy to the C code in real time.

I've been trying to run some supposedly-simple C examples that read/write to TTYUSB0, but no success yet. I didn't post code 'cuz I was really curious if there was a smarter means. Guess I'll try some read/writes using terminos and see if I have better luck, thx for the suggestion.

Will look at your other suggestions as well, thx!
 
Hey Don,

I wrote some software many years ago that wraps around a Linux UART to give a HardwareSerial interface so that I could run the same code on both a Teensy and a BeagleBone Black. It's attached, below. It's been years, so I hope everything still works. At the very least, it might give you a good starting point.
View attachment HardwareSerial.zip

EDIT: I just reviewed the code and you'll definitely need to modify it since I'm using classes and C++ style strings. But at least it will give a starting point with the termios options.

I have a simple library for encoding and decoding bytestream (i.e. UART) messages:
https://github.com/bolderflight/framing

On the sender side, it takes care of framing the message and generating a checksum. On the receiver side, the library is able to find messages in a stream of bytes, check the checksum, and return the message payload. You just have to define the message structure - I would probably just go with a C-struct and be careful to order the struct members such that padding will not be used.

EDIT: Ditto with the library, I'm using classes, so you'd need to modify it for C

Brian
 
Hey Don,

I wrote some software many years ago that wraps around a Linux UART to give a HardwareSerial interface so that I could run the same code on both a Teensy and a BeagleBone Black. It's attached, below. It's been years, so I hope everything still works. At the very least, it might give you a good starting point.
View attachment 28868

EDIT: I just reviewed the code and you'll definitely need to modify it since I'm using classes and C++ style strings. But at least it will give a starting point with the termios options.
...
EDIT: Ditto with the library, I'm using classes, so you'd need to modify it for C

Brian

Sounds familiar: I did something similar about 9-10 years ago, to talk to RPI, then BBBK, ODroid, UP...

The code in the library was never cleaned up, but simply different things I played with:

But some of the sort of Arduino classes were up in:
https://github.com/KurtE/Raspberry_Pi/tree/master/library

But again as you mentioned this is C++...
 
All,

I figured it out. If you'd like details (including all the code), please see the memo I just posted on my LinkedIn account, https://www.linkedin.com/in/kellydak/

Basically I stream real-time sensor data from a Nicla Sense ME, to the microcontroller, via USB serial, to a C program on an Ubuntu Linux PC.

Thx for the various ideas! Don
 
Back
Top