LK204-25-USB + Linux / Java

LK/ELK/VK/PK/OK/MX/GLK/EGLK/GVK/GLT Series

Moderators: Henry, Mods

Post Reply
ballbag
LCD?
Posts: 4
Joined: Wed Apr 30, 2008 10:34 am

LK204-25-USB + Linux / Java

Post by ballbag »

Hey, having spent 7 hours on this today I'm admitting defeat and asking for help :lol:

I have the LK204-25USB device. I have connected it and its powered up no problem.

Initially I found the LCDControl java source code at http://www.lcdforums.com/forums/viewtopic.php?t=2724 - a little fiddling with javax.comm etc and I have it compiled and can call it from my test java app. However, I just get errors like tty0 or tty1 cannot be found. I modified the code to try /dev/ttyS0 and S1 also but still to no avail. I did a little experimentation and added this method to the application to try and list all available ports

Code: Select all

public void test() {
	Enumeration portList = portId.getPortIdentifiers();

	while (portList.hasMoreElements())
	{
		portId = (CommPortIdentifier) portList.nextElement();

		if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
			System.out.println(portId.getName());
		}

	}
}
however as far as I can tell all it does is list entries in the javax.comm.properties file (I also added my /dev/ttyS0 and /dev/ttyS1 devices to this). Manually setting the LCDControl.java to try and use the full paths of /dev/ttyX throw IOExeceptions.


A little more searching produced this page http://www.lcdforums.com/forums/viewtopic.php?t=936 which states that the DisplayCommunicationsExample.zip archive contains code which WILL work with USB devices and not just serial ones, however http://www.lcdforums.com/download/CodeS ... Sample.zip is a dead link..

I gave up on this route, and decided to test it out using LCDProc..

I have LCDProc installed and /etc/LCDd.conf configured to use the MtxOrb driver and it doesnt work right... nothing comes up on the LCD (i tried using /dev/lcd, /dev/tty0, /dev/ttyS0 etc - still no joy). Just to test that I had LCDProc configured okay I changed its output driver to 'curses' and ran the LCDd daemon in the forground with the 'lcdproc' client attached. This worked fine and began to produce statistics on my box.

Scratching my head further I did 'tail -f /var/log/messages' when I plug in the LCD I get this output

Code: Select all

Apr 30 17:37:09 localhost NetworkManager: <debug> [1209573429.347468] nm_hal_device_added(): New device added (hal udi is '/org/freedesktop/Hal/devices/usb_device_1b3d_157_0TmvUnM1_usbraw'). 
Searching through the output of 'lshal' shows no associated input device (i.e. nothing pertaining to /dev).

I'm seriously stuck now.. I'm normally quite good with Google but I just seem to be running round in circles now and as far as I can tell there is no 'official' driver for Linux :o

I'm running Ubuntu Gutsy 7.10, and I suspect the issue to be something to do with HAL not having a clue what the device is but I have no idea how to get around it.

Any replies or hints would be appreciated as this module required for a final year university project!

Thanks for reading.
ballbag
LCD?
Posts: 4
Joined: Wed Apr 30, 2008 10:34 am

Post by ballbag »

Okay, I'm answering my own question :lol:

Enabling the drivers
The Linux driver which must be enabled for this lk204-25-usb LCD to work is called 'ftdi_sio' - it comes with Ubuntu 7.10 by default HOWEVER it does not know about the lk204-25-usb device.

By playing with 'lsusb' while plugging/unplugging the device in I discovered that the USB LCD is referenced as:

Code: Select all

Bus 004 Device 016: ID 1b3d:0157
So as root we can manually force the driver to pick up the device using this:

Code: Select all

bash# modprobe ftdi_sio vendor=0x1b3d product=0x157
After that modprobe line has been done plug the LCD module into the computer again and you should get a line in /var/log/message which says something to the effect of

Code: Select all

localhost kernel: [  236.392000] usb 4-1: FTDI USB Serial Device converter now attached to ttyUSB0
That means that the device is now accessible via /dev/ttyUSB0.

Running LCD proc (with /etc/LCDd.conf set to use the MtxOrb driver and /dev/ttyUSB0) now produced the following error in /var/log/messages

Code: Select all

MtxOrb: open(/dev/lcd) failed (Message too long)
The solution to this problem can be found in this post: http://www.lcdforums.com/forums/viewtop ... ssage+long
- you need to boot into windows to fix it though.

Congratulations, LCDProc will now work.

Example Java code
Regarding the Java code found on the forum. It's not quite right for USB support (under Linux anyway)

The javax.comm.properties file needs to change as follows

Code: Select all

# Paths to server-side serial port devices
serpath0 = /dev/ttyS0
serpath1 = /dev/ttyS1
Change to:

Code: Select all

# Paths to server-side serial port devices
serpath0 = /dev/ttyS0
serpath1 = /dev/ttyS1
serpath2 = /dev/ttyUSB0
Regarding the actual example Java code given out by on this forum http://www.lcdforums.com/forums/viewtopic.php?t=2724 - I think it's for Windows because you need to change the line that says

Code: Select all

String portname = new String("COM");
to

Code: Select all

String portname = new String("/dev/ttyUSB");
Note: you seem to have to re-compile your java code against the modified javax.comm file (so make the changes to that before you re-compile basically).

The example code does some weird stuff but it can write to the Display anyway. If I use it I might post it back on here so there is a definitive reference of how to get one of these things working - I really don't think it should have taken so long, or so many broken links on a manufacturers support forum to find the answer but oh well tis the way of the web :D
Ray
Matrix Orbital
Matrix Orbital
Posts: 745
Joined: Thu Dec 13, 2001 4:00 pm
Location: Earth.... I think..
Contact:

Post by Ray »

You just had some bad timing, due on demand from other customers I was working on an appnote on using our displays with linux for the last few days. Its available here.
ballbag
LCD?
Posts: 4
Joined: Wed Apr 30, 2008 10:34 am

Post by ballbag »

Thats always the way isn't it :lol:

Thanks for the link anyway Ray.

I'm interest as to whether or not there is any better example Java source? Using this current one when I call the clear() method, I just get odd characters appearing on the screen, it looks like a weird P followed by a Square Root sign then the X..

Using the example C code in that appnote the screen clears perfectly.
Ray
Matrix Orbital
Matrix Orbital
Posts: 745
Joined: Thu Dec 13, 2001 4:00 pm
Location: Earth.... I think..
Contact:

Post by Ray »

I can imagine the whole string.getbytes might return unicode instead of ascii. could you dump the contents of the array it generates to the console or inspect it with your debugger to verify its actually sending 254,88 to the display? (or just remove the whole whacky string tricks and send a array with those contents)
Ray
Matrix Orbital
Matrix Orbital
Posts: 745
Joined: Thu Dec 13, 2001 4:00 pm
Location: Earth.... I think..
Contact:

Post by Ray »

Wow never used java before, just discovered it has no unsigned datatypes :jawdrop: oh well, was able to reproduce your test case and patch it make it work, no more need to mess with java for me :)

Code: Select all

	/**
	 * Clears the LCD screen.
	 */
	public void clear() {
		byte[] ClearArray = { -2, 88 };
		try {
			outputStream.write(ClearArray);
		} catch (Exception ex) {
			System.out.println("Could not clear lcd");
		}
	}
ballbag
LCD?
Posts: 4
Joined: Wed Apr 30, 2008 10:34 am

Post by ballbag »

Ray,
Thanks for taking the time to play with that code - I saw your post regarding doing away with the silly string.getBytes() method however I didn't look back on the forums until today.

It's not complete, and it doesn't implement all the possible functions on the lk204-25-usb datasheet PDF but it does everything I need for my mp3 player project and should be enough to get anyone started with the USB module under Linux/Java.

Code: Select all

/********************************************************
* lcdlib 						*
* 							*
* Allows control of a Matrix Orbital lk204-25-USB LCD	*
* NOTE: does not implement ALL functions in datasheet	*
*********************************************************/

import java.io.*;
import java.util.*;
import javax.comm.*;


class lcdlib {

private CommPortIdentifier portId;
private SerialPort serialPort;
private OutputStream outputStream;


/********************************************************
* CONSTRUCTOR						*
* Usage: lcdlib lcd = new lcdib("/dev/ttyUSB0");	*
*********************************************************/
public lcdlib(String portname) {
	try {
		portId = CommPortIdentifier.getPortIdentifier(portname);
	} catch (NoSuchPortException ex) {
		System.out.println(portname + " does not exist.");
		return;
	}

	try {
		serialPort = (SerialPort)portId.open("LCDControl",2000);
	} catch (PortInUseException ex) {
		System.out.println("Could not open " + portname);
	return;
	}
	// for some reason, java throws some errors when setting up the serial port 
	// if you call this class from as a 3rd class e.g. main.java <-- anotherclass.java <-- lcdlib.java
	// however, if you set it up twice it works..
	// 1st run
	try {
		serialPort.setSerialPortParams(19200,
		SerialPort.DATABITS_8,
		SerialPort.STOPBITS_1,
		SerialPort.PARITY_NONE);
	} catch(Exception e) {
		// error.
	}
	// 2nd run
	try {
		serialPort.setSerialPortParams(19200,
		SerialPort.DATABITS_8,
		SerialPort.STOPBITS_1,
		SerialPort.PARITY_NONE);
	} catch (UnsupportedCommOperationException ex) {
		System.out.println("Could not set serial stuff");
	}

	try {
		outputStream = serialPort.getOutputStream();
	} catch (IOException e) {
		System.out.println("Error getting output stream: "+e);
	}

	reset();
}



/**
* Resets the lcd
*/
public void reset() {

try {
	outputStream.write(0xFE); // send command string
	outputStream.write(0x42); // send B

	outputStream.write(0xFE); // send command string
	outputStream.write(0x7E); // send tilde (~)

	outputStream.write(0xFE); // send command string
	outputStream.write(0x54); // send T

	outputStream.write(0xFE); // send command string
	outputStream.write(0x59); // send Y
	
	outputStream.write(0xFE); // send command string
	outputStream.write(0x58); // send B

} catch (Exception ex) {
System.out.println("Could not reset lcd");
}
}

/**
* Clears the LCD screen.
*/
public void clear() {
	try {
		outputStream.write(0xFE); // send command string
		outputStream.write(0x58); // send clear screen command
	} catch (Exception ex) {
		System.out.println("Could not clear lcd");
	}

}



/**
* Writes a string to the screen at the cursor's current
* position.
*
* @param str String to write to the screen.
*/
public void display_write(String str) {

	try {
		outputStream.write(str.getBytes());
	} catch (Exception ex) {
		System.out.println("Could not write to lcd");
	}

}

/**
* Changes position of the lcd's cursor.
*
* @param x Horizontal index to move the cursor to
* @param y Line number to move the cursor to
*/
public void display_position(int x, int y) {

	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x47); // set cursor command
		outputStream.write(y); // y pos
		outputStream.write(x); // x pos
	} catch (Exception ex) {
	System.out.println("Could not change cursor position on lcd");
	}
}

public void autoScrollOn() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x51); // autoscroll on
	} catch (Exception ex) {
	System.out.println("Could not turn auto scroll on");
	}
}

public void autoScrollOff() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x52); // auto scroll off
	} catch (Exception ex) {
	System.out.println("Could not turn auto scroll off");
	}
}

public void autoLineWrapOn() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x43); // auto line wrap on 
	} catch (Exception ex) {
	System.out.println("Could not turn line wrap on");
	}
}

public void autoLineWrapOff() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x44); // auto line wrap off
	} catch (Exception ex) {
	System.out.println("Could not turn line wrap off");
	}
}

public void underLineCursorOn() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x4A); // underline cursor on
	} catch (Exception ex) {
	System.out.println("Could not turn underline on");
	}
}

public void underLineCursorOff() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x4B); // underline cursor off
	} catch (Exception ex) {
	System.out.println("Could not turn underline off");
	}
}

public void blinkingBlockCursorOn() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x53); // blinking block cursor on
	} catch (Exception ex) {
	System.out.println("Could not turn blinking cursor on");
	}
}

public void blinkingBlockCursorOff() {
	try {
		outputStream.write(0xFE); // command string
		outputStream.write(0x54); // blinking block cursor off
	} catch (Exception ex) {
	System.out.println("Could not turn blinking cursor off");
	}
}



}
It's then called like this.

Code: Select all

class main {

	public static void main(String args[]) {


		//invoke control class
		lcdlib lcd = new lcdlib("/dev/ttyUSB0");

		// clear screen
		System.out.println("Clearing screen");
		lcd.clear();

		// write to the screen at "HOME" position (1,1)
		System.out.println("Writing to LCD");
		lcd.display_write("testing blap");
	
		// write to screen at Line 3 Row 2
		System.out.println("Writing to LCD at line 3 row 2");
		lcd.display_position(3,2);
		lcd.display_write("i win");

	}

}
Took me 15 hours and lots of RedBull :lol:
Post Reply