How does Arduino IDE Serial Monitor Works? (ttyusb not always works Ubuntu Linux +FIX)

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello. I wanted to share this fix i found out, which made me think how arduino IDE works.

 

Short question: Does Arduino IDE's Serial monitor unplugs and plugs the usb port programatically every time you click on it, before starting to listen to the port serial communication?

 

Long Question:

I recently started programming etmega328p on AVR-C on ubuntu. I do really good, and i upload whatever i do here:  (Even tho i started on windows with atmel studio, i slowly moved to linux). 

using linux, in order to debug my programs, i use serial monitor from arduino IDE. More specifically, i setup a serial communication on every program i make, so i can have an output to help me debug. This code in particular. Now, I see the output of this code, using the Serial Monitor of the Arduino IDE, which is not the mist professional way, but it works always.

 

I made an attempt yesterday to use a more professional solution and to see the serial using "sudo cat /dev/ttyUSB0" (or USB1,2,3.. depending the port). The thing is, it doesnt always works. It may work the first times i plug my atmega, upload the code and check the serial, but st some point it stucks. But after the first time it stucks, every time after that, i try to check the serial, i see nothing. Example below. On the third

`sudo cat /dev/ttyUSB0`

, i re-created the error, by trying to upload code to the device, while simultaneously checking the serial. 

And, as you can see the `sudo cat /dev/ttyUSB0` doesnt work after that.

 

I found a solution, from here that worked, where is suggests unplugging and plugging the usb port programatically bedore executing `sudo cat /dev/ttyUSB0`.

Now, whenever i used Arduino IDE's serial, i never ever had this issue. This made me think, how does arduino IDE's serial monitor work? does it unplug and plug programatically the usb every time i click on it? Should i ask about that the arduino forum? 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is not about unplugging and plugging in.  It's about ownership.  TTY ports are not shared between applications, they are claimed by them.  So if application 1 claims a specific TTY, a different app will not be allowed to use it until the original application releases it.  The Arduino IDE will claim ttyUSB0 when it starts up (assuming you will want to use it for uploading your code).  Since it is already claimed, you can also use it for the IDE serial monitor as well.   If, while you are running the IDE, you decide to access the same serial port from the command line... it will be claimed and you can't have it.  Once you close down the IDE, ownership of the TTY will be released and you can use it from a new application or from your shell.