Search code examples
javaarduinoserial-portarduino-unorxtx

Java RXTX - Detecting the right device through serial communication


Let me go straight to the problem.

Let say, I have a Arduino Uno (COM4) and Arduino Mega (COM5), both is communicating with my OS through serial port. How can I detect that COM4 is really an Arduino Uno so that I don't mistakenly connecting my program to COM5 port? The process of connecting to the serial port is done in runtime by my program.

The code that I saw online here in another SO question and from my experiment with that code earlier, it only listed out all serial ports while it don't reveal out what is that device is (so the program don't know what is really that device, either can be Arduino Uno or Arduino Mega or something else).

So my question is, how can I achieve this purpose? Cross-platform is preferred but if such things can't be achieve, then please answer on how to do it inside Windows (as I currently developing this program for computer that's using Windows OS)


Solution

  • You can ask a device to identify itself right after connecting. For that you want to support device identification in the device's firmware.

    Add something like following logic into loop method defenition of the arduino sketch:

    int inputData = Serial->read();
    if (inputData == IDENTIFICATION_REQUEST) {
      // respond with information that identify the device
    }
    

    Then you can check which device the programm is connected to sending IDENTIFICATION_REQUEST.