Plug in and plug out arduino

I don't know if here is the right place to post this, but I want to know how to get the COMM Port that arduino is connecting in C language. And how to know when the arduino was plug out in C language too.

On Linux, you could do something like this:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char device[128];

    FILE *fp = popen("readlink -f /dev/serial/by-id/*Arduino*", "r");
    if (fp == NULL) {
        printf("Failed to run readlink command\n");

    unsigned i = 0;
    while (fgets(device, sizeof(device), fp) != NULL) {
        printf("Arduino %u: %s", i++, device);


This looks for Arduino serial ports, and prints their device path.

You can check whether the /dev/tty* file exists to detect when it’s disconnected. You can use the “access” function from the unistd.h header to do this.

But the real question here is: why are you using C for this program? Serial communication usually involves string manipulations, which is a nightmare in C. If this isn’t an assignment and you’re free to choose a language, I’d recommend Python or another high-level language. If performance is a concern, C++ would be a much better choice than C, because its standard library has excellent support for strings and regexes.


Thanks for answer!
That's because I'm just need a byte to do what I want.
And how about windows?

I have no experience with Windows, but from a quick google search, trying to do it in C or even Visual C++ looks like a complete waste of time to me. Just use Python, the PySerial module has backends for all major operating systems.

from serial import Serial
from import comports

arduino_ports = sorted(
    filter(lambda port: "Arduino" in port.manufacturer, comports()))

arduino_port = arduino_ports[0]

print("Selecting port:")
print(f"  {} " +
      f"({arduino_port.usb_description()} - {arduino_port.manufacturer})")

print("Other Arduino ports:")
for port in arduino_ports[1:]:
    print(f"  {} " +
          f"({port.usb_description()} - {port.manufacturer})")

with Serial(port=arduino_port.device, baudrate=115200) as arduino:
    byte = # read one byte