13 messages in net.java.dev.jna.usersRe: [jna-users] handling serial port ...
FromSent OnAttachments
zamekMar 11, 2008 11:58 am 
Marc RavensbergenMar 11, 2008 12:24 pm 
zamekMar 11, 2008 1:47 pm 
Timothy WallMar 12, 2008 5:38 am 
zamekMar 12, 2008 2:34 pm 
Timothy WallMar 12, 2008 4:36 pm 
zamekMar 13, 2008 1:12 pm 
Timothy WallMar 13, 2008 5:04 pm.java
zamekMar 14, 2008 7:26 am 
Timothy WallMar 14, 2008 8:20 am 
zamekMar 14, 2008 8:51 am 
Timothy WallMar 17, 2008 6:58 am 
zamekMar 17, 2008 11:22 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] handling serial port with jna on linuxActions...
From:zamek (zam@vili.pmmf.hu)
Date:Mar 14, 2008 7:26:30 am
List:net.java.dev.jna.users

Hi Timothy,

Thanks a lot for the code, but unfortunately it isn´t works:( I can read from port, but signal cannot arrived.

I simly copied from your code like this:

======================== private final static int SIGIO = 29, TCSANOW = 0, TIOCMGET = 0x5415, TIOCMSET = 0x5418, TIOCM_RTS = 4, O_RDWR=2, O_NOCTTY = 400, O_NONBLOCK = 4000, F_SETOWN = 8, F_SETFL = 4, FASYNC = 20000, B115200 = 0010002, CS8 = 0000060, PARENB = 0000400, PARODD = 0001000, IGNPAR = 0000004, VTIME = 5, VMIN = 6, TCIFLUSH = 0, TCOFLUSH = 1, TCIOFLUSH = 2, CLOCAL = 4000;

interface CLibrary extends Library {

CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class);

int open (String device, int flags); int close(int filedes); int sigemptyset (Pointer set); int sigaction(int sig, sigaction action, sigaction oldAction); int fcntl (int filedes, int command, int others); int getpid (); int tcgetattr (int filedes, Termios tio); int tcsetattr (int filedes, int when, Termios tio); int tcflush (int filedes, int queue); int ioctl (int filedes, int command, Pointer q); int read (int filedes, byte[] buffer, int counts); }

int dev;

SignalHandler signal = new SignalHandler (){ @Override public void callback(int signal) { IntByReference st = new IntByReference(); CLibrary.INSTANCE.ioctl(dev, TIOCMGET, st.getPointer()); st.setValue(st.getValue() & ~TIOCM_RTS); CLibrary.INSTANCE.ioctl(dev, TIOCMSET, st.getPointer()); st.setValue(st.getValue() | TIOCM_RTS); CLibrary.INSTANCE.ioctl(dev, TIOCMSET, st.getPointer()); }};

public Main() { int pid = CLibrary.INSTANCE.getpid();

dev = CLibrary.INSTANCE.open("/dev/ttyUSB2", O_RDWR | O_NOCTTY | O_NONBLOCK); if (dev <= 0) { System.out.println("Open error:"+dev); System.exit(dev); }

sigaction saio = new sigaction(); sigaction old = new sigaction(); saio.sa_handler = this.signal;

int result = CLibrary.INSTANCE.sigaction(SIGIO, saio, old); if (result != 0) { System.err.println("Could not install handler: " + result); System.exit(1); }

CLibrary.INSTANCE.fcntl(dev, F_SETOWN, pid); CLibrary.INSTANCE.fcntl(dev, F_SETFL, FASYNC); Termios oldSet = new Termios(); result = CLibrary.INSTANCE.tcgetattr(dev, oldSet); Termios newSet = new Termios(); newSet.c_cflag = B115200 | CS8 | CLOCAL | CREAD; newSet.c_iflag = IGNPAR; newSet.c_oflag = 0; newSet.c_lflag = 0; newSet.c_cc[VMIN] = 1; newSet.c_cc[VTIME] =0; result = CLibrary.INSTANCE.tcflush(dev, TCIFLUSH); result = CLibrary.INSTANCE.tcsetattr(dev, TCSANOW, newSet);

byte[] buffer = new byte[255];

signal.callback(333);

while (true) { int readed = CLibrary.INSTANCE.read(dev, buffer, 255); if (readed > 0) { if (buffer[0] == 0x1b) break; for (int i=0; i<readed;i++) System.out.write(buffer[i]); System.out.println(); } }

CLibrary.INSTANCE.tcsetattr(dev, TCSANOW, oldSet); CLibrary.INSTANCE.close(dev); }

public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Main(); } }); } =======================