Arduino + Juce Serial Communication


@Jules, @jordanh & the rest

Hi I am trying to set up a basic Serial communication with an Arduino for my application. The Idea is to receive one byte at a time.
I have been looking into may aspect on how to get Serial communication and JUCE working. I even tried to use openFrameworks with JUCE but with no successes.
Then I came along this thread viewtopic.php?f=2&t=5500&hilit=serial+arduino by jordanh, which pointed to a Serial code by Graffiti. I tried to implement it into my code.
But the code seems to give me errors with the explicit Thread initialization and other errors. I am working on OS X with xCode and using the latest JUCE library. The project was created using Introjucer, and I added the juce_SerialPort to the Juce Modules and licked them in my code.

Here is the Thread Error: [color=#FF0000]…/…/JuceLibraryCode/modules/juce_SerialPort/SerialPort.h:112:2: error: constructor for ‘SerialPortInputStream’ must explicitly initialize the base class ‘juce::Thread’ which does not have a default constructor [3][/color]
And here is a casting Error in the canReadString() method [color=#FF0000]…/…/JuceLibraryCode/modules/juce_SerialPort/SerialPort.h:133:8:{133:8-133:21}{133:15-133:21}: error: cannot cast from type ‘juce::MemoryBlock’ to pointer type ‘char *’ [3]

Here is the code for the SerialPortInputStream

class SerialPortInputStream : public InputStream, public ChangeBroadcaster, private Thread { public: SerialPortInputStream(SerialPort * port) :port(port),bufferedbytes(0), notify(NOTIFY_OFF), notifyChar(0) { startThread(); } ~SerialPortInputStream() { signalThreadShouldExit(); waitForThreadToExit(500); } enum notifyflag{NOTIFY_OFF=0, NOTIFY_ON_CHAR, NOTIFY_ALWAYS}; void setNotify(notifyflag notify=NOTIFY_ON_CHAR, char c=0) { notifyChar = c; this->notify = notify; } bool canReadString() { const ScopedLock l(bufferCriticalSection); int iNdx=0; while(iNdx<bufferedbytes) if(((char*)buffer)[iNdx++]==0)return true; return false; } bool canReadLine() { const ScopedLock l(bufferCriticalSection); int iNdx=0; while(iNdx<bufferedbytes) if( (((char*)buffer)[iNdx++]=='\n') /*|| (((char*)buffer)[iNdx++]=='\r')*/) return true; return false; } virtual void run(); virtual int read(void *destBuffer, int maxBytesToRead); virtual const String readNextLine() //have to override this, because InputStream::readNextLine isn't compatible with SerialPorts (uses setPos) { String s; char c; s.preallocateStorage(32); while(read(&c, 1) && (c!='\n')) s.append(String::charToString(c), 1); s = s.trim(); return s; } virtual int64 getTotalLength() { const ScopedLock l(bufferCriticalSection); return bufferedbytes; }; virtual bool isExhausted() { const ScopedLock l(bufferCriticalSection); return bufferedbytes?false:true; }; virtual int64 getPosition(){return -1;}; virtual bool setPosition(int64 newPosition){return false;}; juce_UseDebuggingNewOperator private: SerialPort * port; int bufferedbytes; MemoryBlock buffer; CriticalSection bufferCriticalSection; notifyflag notify; char notifyChar; };
For the full code please go here:

Dose anyone knows how to solve this please?
Am I doing something wrong or is the Serial code not setup properly?

Thank you in advance!


That code contains a lot of mistakes - maybe it’s old and a few library classes have changed since it was written. Haven’t time to help, but you should be able to get it going with a bit of updating.


While not a JUCE-centric solution, I did have some luck communicating with Arduino using Boost::ASIO. I found the code in the following link to be a good starting point.


Thank you for the help. I managed to communicate :slight_smile: