public int OpenPort(String portName) {
		if(portOpened && portName.equals(GetLastPort())) return 0;
		if(PortExists(portName) == false) return 0;
		
		ClosePort();
		
		Log("Connecting to "+portName+"..."+NL);

		// open the port
		serialPort = new SerialPort(portName);
		try {
            serialPort.openPort();// Open serial port
            serialPort.setParams(BAUD_RATE,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
            serialPort.addEventListener(this);
        } catch (SerialPortException e) {
			Log("<span style='color:red'>Port could not be configured:"+e.getMessage()+"</span>\n");
			return 3;
		}

		Log("<span style='color:green'>Opened.</span>\n");
		portOpened=true;
		SetLastPort(portName);

		return 0;
	}