Serial port does not exist!

This is the test code I am using

#include “MainComponent.h”

//==============================================================================
MainComponent::MainComponent() : serialPort((juce::String prefix, juce::String msg) { juce::Logger::outputDebugString(prefix + ": " + msg); })
{
// Make sure you set the size of the component after
// you add any child components.
setSize (800, 600);

// Some platforms require permissions to open input channels so request that here
if (juce::RuntimePermissions::isRequired (juce::RuntimePermissions::recordAudio)
    && ! juce::RuntimePermissions::isGranted (juce::RuntimePermissions::recordAudio))
{
    juce::RuntimePermissions::request (juce::RuntimePermissions::recordAudio,
                                       [&] (bool granted) { setAudioChannels (granted ? 2 : 0, 2); });
}
else
{
    // Specify the number of input and output channels that we want to open
    setAudioChannels (2, 2);
}

// Check if the specified port exists
// Get a list of available serial port paths
juce::StringPairArray serialPortPaths = SerialPort::getSerialPortPaths();

// Check if the specified port path exists
// Get a list of available serial port paths

// Check if the specified port path exists
bool portExists = false;
for (int i = 0; i < serialPortPaths.size(); ++i)
{
    if (serialPortPaths.getAllValues()[i] == "/dev/tty.usbmodem148917201")
    {
        portExists = true;
        break;
    }
}

if (!portExists)
{
    juce::Logger::outputDebugString("Specified serial port does not exist!");
    return;
}



// Open the serial port connection
if (!serialPort.exists())
{
    if (serialPort.open ("/dev/tty.usbmodem148917201")) // Replace with your Teensy's port path
    {
        // Configure serial port settings if needed
        SerialPortConfig config (115200, 8, SerialPortConfig::SERIALPORT_PARITY_NONE, SerialPortConfig::STOPBITS_1, SerialPortConfig::FLOWCONTROL_NONE);
        serialPort.setConfig (config);

        // Serial port opened successfully
        juce::Logger::outputDebugString ("Serial port opened successfully!");
    }
    else
    {
        // Failed to open serial port
        juce::Logger::outputDebugString ("Failed to open serial port!");
    }
}

}

MainComponent::~MainComponent()
{
// This shuts down the audio device and clears the audio source.
shutdownAudio();

// Close the serial port connection
serialPort.close();

}

//==============================================================================
void MainComponent::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
// This function will be called when the audio device is started, or when
// its settings (i.e. sample rate, block size, etc) are changed.

// You can use this function to initialise any resources you might need,
// but be careful - it will be called on the audio thread, not the GUI thread.

// For more details, see the help for AudioProcessor::prepareToPlay()

}

void MainComponent::getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill)
{
// Your audio-processing code goes here!

// For more details, see the help for AudioProcessor::getNextAudioBlock()

// Right now we are not producing any data, in which case we need to clear the buffer
// (to prevent the output of random noise)
bufferToFill.clearActiveBufferRegion();

}

void MainComponent::releaseResources()
{
// This will be called when the audio device stops, or when it is being
// restarted due to a setting change.

// For more details, see the help for AudioProcessor::releaseResources()

}

//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
// (Our component is opaque, so we must completely fill the background with a solid colour)
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));

// You can add your drawing code here!

}

void MainComponent::resized()
{
// This is called when the MainContentComponent is resized.
// If you add any child components, this is where you should
// update their positions.
}

It is giving me error Serial post does not exist. Please give me a possible reason and solution to address that?

Welcome to the forum.

First of all, please surround your code with three back ticks ``` before and after on a separate line. That way the code will be properly formatted and some code is not turned into emojis, like in your constructor.
You can edit your post, no need to post it again.

I don’t think the serialPort is a juce thing, so can you post what it is and where you got it from? Maybe somebody recognises it and can help.
The declaration of serialPort and the necessary includes are probably in the MainComponent.h, so if you could answer that it’s easier to help.

Good luck.

I am sorry for that. I am basically trying to connect my Arduino chip with the app. I wanted to test it. I can see that the port is connected to my system, but Juce (running on Xcode) is not detecting it.

#include "MainComponent.h"

//==============================================================================
MainComponent::MainComponent() : serialPort([](juce::String prefix, juce::String msg) { juce::Logger::outputDebugString(prefix + ": " + msg); })
{
    // Make sure you set the size of the component after
    // you add any child components.
    setSize (800, 600);

    // Some platforms require permissions to open input channels so request that here
    if (juce::RuntimePermissions::isRequired (juce::RuntimePermissions::recordAudio)
        && ! juce::RuntimePermissions::isGranted (juce::RuntimePermissions::recordAudio))
    {
        juce::RuntimePermissions::request (juce::RuntimePermissions::recordAudio,
                                           [&] (bool granted) { setAudioChannels (granted ? 2 : 0, 2); });
    }
    else
    {
        // Specify the number of input and output channels that we want to open
        setAudioChannels (2, 2);
    }
    
    // Check if the specified port exists
    // Get a list of available serial port paths
    juce::StringPairArray serialPortPaths = SerialPort::getSerialPortPaths();

    // Check if the specified port path exists
    // Get a list of available serial port paths

    // Check if the specified port path exists
    bool portExists = false;
    for (int i = 0; i < serialPortPaths.size(); ++i)
    {
        if (serialPortPaths.getAllValues()[i] == "/dev/tty.usbmodem148917201")
        {
            portExists = true;
            break;
        }
    }

    if (!portExists)
    {
        juce::Logger::outputDebugString("Specified serial port does not exist!");
    }



    // Open the serial port connection
    if (!serialPort.exists())
    {
        if (serialPort.open ("/dev/tty.usbmodem148917201")) // Replace with your Teensy's port path
        {
            // Configure serial port settings if needed
            SerialPortConfig config (115200, 8, SerialPortConfig::SERIALPORT_PARITY_NONE, SerialPortConfig::STOPBITS_1, SerialPortConfig::FLOWCONTROL_NONE);
            serialPort.setConfig (config);

            // Serial port opened successfully
            juce::Logger::outputDebugString ("Serial port opened successfully!");
        }
        else
        {
            // Failed to open serial port
            juce::Logger::outputDebugString ("Failed to open serial port!");
        }
    }
}

MainComponent::~MainComponent()
{
    // This shuts down the audio device and clears the audio source.
    shutdownAudio();

    // Close the serial port connection
    serialPort.close();
}

//==============================================================================
void MainComponent::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
    // This function will be called when the audio device is started, or when
    // its settings (i.e. sample rate, block size, etc) are changed.

    // You can use this function to initialise any resources you might need,
    // but be careful - it will be called on the audio thread, not the GUI thread.

    // For more details, see the help for AudioProcessor::prepareToPlay()
}

void MainComponent::getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill)
{
    // Your audio-processing code goes here!

    // For more details, see the help for AudioProcessor::getNextAudioBlock()

    // Right now we are not producing any data, in which case we need to clear the buffer
    // (to prevent the output of random noise)
    bufferToFill.clearActiveBufferRegion();
}

void MainComponent::releaseResources()
{
    // This will be called when the audio device stops, or when it is being
    // restarted due to a setting change.

    // For more details, see the help for AudioProcessor::releaseResources()
}

//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
    // (Our component is opaque, so we must completely fill the background with a solid colour)
    g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));

    // You can add your drawing code here!
}

void MainComponent::resized()
{
    // This is called when the MainContentComponent is resized.
    // If you add any child components, this is where you should
    // update their positions.
}
#pragma once

#include <JuceHeader.h>
#include "SourceCode.h"


//==============================================================================
/*
    This component lives inside our window, and this is where you should put all
    your controls and content.
*/
class MainComponent  : public juce::AudioAppComponent
{
public:
    //==============================================================================
    MainComponent();
    ~MainComponent() override;

    //==============================================================================
    void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
    void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override;
    void releaseResources() override;

    //==============================================================================
    void paint (juce::Graphics& g) override;
    void resized() override;

private:
    //==============================================================================
    // Your private member variables go here...
    SerialPort serialPort; // Declare an instance of the SerialPort class


    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};