Serial port communication support

Have there ever been any plans for JUCE to support serial port communications? (Obviously including via USB like what Arduino does.)

Until that time, you could use the JUCE Serial Port Library hosted here: http://omohmproductions.com/?page_id=145. I use it specifically to talk to Arduino’s.

5 Likes

Thanks, I’ll take a look at that!

I successfully used the asio (asynchronous I/O) library for that. Asio is part of boost but also available as lightweight header-only standalone version

1 Like

Hi,

Do you have an example as how you use this? I’m new to Juce and trying to understand how to implement this.

Any direction would be great.

Thanks

I just updated an older project of mine to work with the latest JUCE, and put it on github. I’ve also moved the serial port library to github as well… It’s an ‘example’ of how to use the library, but I can’t comment on the code quality… lol…

1 Like

Thank you.

In your cSerialPortListMonitor.cpp, line 39 you are referencing SerialPort::getSerialPortPaths()… however I can not see anywhere in the cSerialPortListMonitor.h file, importing “juce_serialport.h”?

How do you have access to the serialPort?

I have added the juce_serialport folder to the JUCE modules folder, thinking it would be included with “…/JuceLibraryCode/JuceHeader.h”

I have added the header and cpp file to Projucer, then launched it and included “juce_serialport.h” inside my file - states could not be found.

Or have I got it all wrong when adding / including external libraries?

If it’s a JUCE module the header should be included in JuceHeader.h and the source files will automatically be getting compiled for you, no need to add anything other than the the juce module to the Projucer.

Perhaps you didn’t add the module to your .jucer project?

What @Anthony_Nicholls and @richie said! :slight_smile:

Ok. Strange. I added the juce_serialport folder from your website, and placed it inside the Juce modules folder. Then added it inside of Projucer via ‘Add a module from a specified folder…’ and this is the resulting error:

However, when I added the juce_serialport folder to my desktop and re-add the module, it works.

Is this a known issue? Or should I never add any modules to the Juce module folder?

That’s it. I noticed that it does not work by adding any module to the JUCE module folder.
Instead you can simply create a folder for “user modules” which is linked into “Global Paths”.

I can’t comment on the underlying juce module mechanism, and whether or not 3rd party modules can be added to the JUCE modules folder, but it isn’t an issue specific to the serial port module. I put all of my 3rd party modules in a separate folder.

Did this solve your linking issue?

No it does not. I keep having the same linking error whether I add the module from the user module folder (global path) or from a separate folder. I just tried to write another simple project adding only the module and few lines to integrate serial port communications and it happens the same errors for all the functions I’m using.

I declare the related members in the header file as

MainComponent.h

class MainComponent : public Component
{
public:

// Serial Ports 
StringPairArray portList;
SerialPortConfig SPConfig {9600, 8, SerialPortConfig::SERIALPORT_PARITY_NONE, SerialPortConfig::STOPBITS_1, SerialPortConfig::FLOWCONTROL_NONE};
SerialPort* pSP;
SerialPortInputStream * pInputStream;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)

};

and as follows in the constructor :

MainComponent.cpp

MainComponent::MainComponent()
{
portList = SerialPort::getSerialPortPaths();
if (portList.size())
{
int numberOfPorts = portList.size();

    for (int i=0; i<numberOfPorts; ++i) {       // find the expected port to use
        String name = portList.getAllValues()[i];

        if (name == "/dev/tty.usbmodem143401")
        {
            pSP = new SerialPort(portList.getAllValues()[1],SerialPortConfig(9600, 8, SerialPortConfig::SERIALPORT_PARITY_NONE, SerialPortConfig::STOPBITS_1, SerialPortConfig::FLOWCONTROL_NONE)); // create the serial port 
            
           pInputStream = new SerialPortInputStream(pSP);          // create stream for reading
            pInputStream->setNotify(SerialPortInputStream::NOTIFY_ON_CHAR,'1');
        }


}

The whole code compiles well. But still linker error