Arduino - Alix1.d - Serial Communication


I hava an arduino on which I have connected a door sensor. When I connect to the arduino via serial2usb port with windows 7 I can read the values by the sensor right. I mean when the door is closed I get always "0" and when it's open I ALWAYS get something else BUT NOT "0".

The problem is when I try to connect arduino to my Alix1.d pc engine which runs Debian. I have written a programm in Java which reads data from arduino. But the real problem is that it does not read them right... When the door is closed I get "0" but when it's open I sometimes get "0" excpet from other values...In other words it says that the door is closed (for some milliseconds) and then is opens...And this happens as long as the door is open.

I changed the baud rate...I used serial.flush in some tests and nothing happens. While in the windows works ok in any baud rate on linux does not work well to none baud rate...

To mention tha I use "parity none", "stopbits_1", "databits 8 "

Plesae help!!! And sorry for my english...I am from Greece!

You haven't told us what your "sensor" is, how it is wired, or what your sketch looks like.

Once these things are sorted out, maybe we can help.

It is just a simple door sensor with two wires....One in ground and the other to analog pin 0. (

This is one of my many already tried codes on arduino...

 int analogValue=0;

 void setup() {
    Serial.begin(9600); }

 void loop() {
     analogValue = analogRead(0);
     if (analogValue!=0)
     while (analogValue!=0)
      {analogValue = analogRead(0);
   if (analogValue==0)
    while (analogValue==0)
      { analogValue = analogRead(0);

And this is the java Code...

import java.util.*;
import*; // for rxtxSerial library

public class SimpleRead implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration            portList;
    InputStream                  inputStream;
    SerialPort                  serialPort;
    Thread                  readThread;

    public static void main1(String[] args) {
    boolean                  portFound = false;
    String                  defaultPort = "/dev/ttyUSB0";

       if (args.length > 0) {
          defaultPort = args[0];
      portList = CommPortIdentifier.getPortIdentifiers();

      while (portList.hasMoreElements()) {
          portId = (CommPortIdentifier) portList.nextElement();
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals(defaultPort)) {
                System.out.println("Found port: "+defaultPort);
                portFound = true;
                SimpleRead reader = new SimpleRead();
      if (!portFound) {
          System.out.println("port " + defaultPort + " not found.");

    public SimpleRead() {
      try {
          serialPort = (SerialPort)"SimpleReadApp", 2000);
      } catch (PortInUseException e) {}

      try {
          inputStream = serialPort.getInputStream();
      } catch (IOException e) {}

      try {
      } catch (TooManyListenersException e) {}


      try {
          serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
      } catch (UnsupportedCommOperationException e) {}

      readThread = new Thread(this);

public void run() {
      try {
        while (true) {
     catch (InterruptedException e) {}

    public void serialEvent(SerialPortEvent event) {
      switch (event.getEventType()) {

      case SerialPortEvent.BI:

      case SerialPortEvent.OE:

      case SerialPortEvent.FE:

      case SerialPortEvent.PE:

      case SerialPortEvent.CD:

      case SerialPortEvent.CTS:

      case SerialPortEvent.DSR:

      case SerialPortEvent.RI:

      case SerialPortEvent.OUTPUT_BUFFER_EMPTY:

      case SerialPortEvent.DATA_AVAILABLE:
          byte[] readBuffer = new byte[20];

          try {
            while (inputStream.available() > 0) {
                int numBytes =;
            String x=new String(readBuffer);
            System.out.print(new String(readBuffer));
          } catch (IOException e) {}



I'm sorry my Greek isn't much use beyond ordering food, but I'm guessing that's a reed switch. Is your problem caused by switch bounce, or a floating input?

Yes it’s a reed switch.
If understand well, you are asking me if the problem is caused by the door sensor right?

If you mean this, I am 100% that the problem is not caused by this, because when the arduino is connected to my Windows 7 laptop sends the right values…

Nope, still sounds like a floating input. Is the switch (sensor) labelled "NC" or "NO"?

I dont know...there was no label on the switch sensor:-(!

Sorry to insist but how can this be a floating problem as long as the same arduino....the same wire...the same distance and from the same place it works ok on the windows laptop?