While entering the serial port name into the code in arduino IDE, enter the whole port address i.e:
/dev/cu.usbmodem*
or
/dev/cu.UG-*
where the *
is the port number.
And for the port number in case of mac just open terminal and type
ls /dev/*
and then search for the port that u have set in arduino IDE.