Results 1 to 6 of 6
- 12-14-2010, 01:51 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Serial port communication via jSSC
jSSC (Java Simple Serial Connector) - is a Free java library for working with serial ports. Current version of library (0.7.1) support Win32 and Win64. Linux version is under construction. jSSC give a simple tool to developer for communicate with different devices via serial port. With jSSC you can get the port names, read and write data, control lines RTS and DTR, receive Events etc. jSSC designed to operate 24/7 multi-threaded systems and is currently successfully used in automation, data collection and recording. Now let me give a few examples of actual work with the component.
Example 1. Getting the names of serial ports:
Example 2. Read and write data:Java Code:import jssc.SerialPortList; public class Main { public static void main(String[] args) { //Method getPortNames() returns an array of strings. Elements of the array is already sorted. String[] portNames = SerialPortList.getPortNames(); for(int i = 0; i < portNames.length; i++){ System.out.println(portNames[i]); } } }
Example 3. Read and write data using the interface SerialPortEventListener:Java Code:import jssc.SerialPort; import jssc.SerialPortException; public class Main { public static void main(String[] args) { //In the constructor pass the name of the port with which we work SerialPort serialPort = new SerialPort("COM1"); try { //Open port serialPort.openPort(); //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0); serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //Writes data to port serialPort.writeBytes("Test"); //Read the data of 10 bytes. Be careful with the method readBytes(), if the number of bytes in the input buffer //is less than you need, then the method will wait for the right amount. Better to use it in conjunction with the //interface SerialPortEventListener. byte[] buffer = serialPort.readBytes(10); //Closing the port serialPort.closePort(); } catch (SerialPortException ex) { System.out.println(ex); } } }
Java Code:import jssc.SerialPort; import jssc.SerialPortEvent; import jssc.SerialPortEventListener; import jssc.SerialPortException; public class Main { static SerialPort serialPort; public static void main(String[] args) { serialPort = new SerialPort("COM1"); try { serialPort.openPort(); serialPort.setParams(9600, 8, 1, 0); //Preparing a mask. In a mask, we need to specify the types of events that we want to track. //Well, for example, we need to know what came some data, thus in the mask must have the //following value: MASK_RXCHAR. If we, for example, still need to know about changes in states //of lines CTS and DSR, the mask has to look like this: SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR int mask = SerialPort.MASK_RXCHAR; //Set the prepared mask serialPort.setEventsMask(mask); //Add an interface through which we will receive information about events serialPort.addEventListener(new SerialPortReader()); } catch (SerialPortException ex) { System.out.println(ex); } } static class SerialPortReader implements SerialPortEventListener { public void serialEvent(SerialPortEvent event) { //Object type SerialPortEvent carries information about which event occurred and a value. //For example, if the data came a method event.getEventValue() returns us the number of bytes in the input buffer. if(event.isRXCHAR()){ if(event.getEventValue() == 10){ try { byte buffer[] = serialPort.readBytes(10); } catch (SerialPortException ex) { System.out.println(ex); } } } //If the CTS line status has changed, then the method event.getEventValue() returns 1 if the line is ON and 0 if it is OFF. else if(event.isCTS()){ if(event.getEventValue() == 1){ System.out.println("CTS - ON"); } else { System.out.println("CTS - OFF"); } } else if(event.isDSR()){ if(event.getEventValue() == 1){ System.out.println("DSR - ON"); } else { System.out.println("DSR - OFF"); } } } } }
jSSC page on Google Code: java-simple-serial-connector - Project Hosting on Google Code
- 12-30-2010, 10:43 AM #2
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Problem with Reading and writing
hi,
i tried your last example for reading and writing with event listener nut it's not working with my device.
i have a vaisala weather station and its paremters are:
9600,8,1,0
when i try your code, it's happen nothing.
but in the same time i can communicate with HyperTerminal.
but at this moment if i try your example, there is the following message:
"jssc.SerialPortException: Port name - EMPTY; Method name - setParams(); Exception type - Port not opened."
why?
Thank a lot
SPX
- 12-30-2010, 12:50 PM #3
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
Dear spalax,
You can use the javax.comm from Sun. and can use the online codes that are widely available. A example is available with the same settings us you have mentioned.
Reading and Writing Data from comm port in JAVA
- 12-30-2010, 04:59 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Spalax, you can recieve this exception only if you not opened the port. Please check you source.
1. Create SerialPort class object: SerialPort serialPort = new SerialPort("COM1");
2. Open port: serialPort.openPort();
3. Set serial port settings: serialPort.setParams(9600, 8, 1, 0);
Check result of openPort() method. This method must return true if you seccesfully opened the port.
Good luck in your projects.Last edited by scream3r; 12-30-2010 at 09:23 PM.
- 12-30-2010, 08:06 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Thanks a lot every one.
now the code is working
i will get back to you once i finished the next step
spalax
- 02-15-2011, 08:12 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
jSSC-CE released. Library for WinCE.
jSSC page on Google Code: java-simple-serial-connector - Project Hosting on Google Code
Similar Threads
-
serial communication is very slow while reading from serial port
By elsanthosh in forum AWT / SwingReplies: 1Last Post: 07-30-2010, 08:29 AM -
serial port communication in java
By elsanthosh in forum New To JavaReplies: 2Last Post: 04-06-2010, 06:33 PM -
Usage of Serial Com Port communication
By calvinlyp in forum New To JavaReplies: 0Last Post: 02-19-2010, 06:25 AM -
Serial Port
By radhika in forum New To JavaReplies: 5Last Post: 11-06-2009, 10:40 AM -
serial port
By musiigedeo in forum Advanced JavaReplies: 0Last Post: 07-23-2007, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks