package com.vengo.test.ui;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
@SuppressWarnings("serial")
public class VendingPanel extends JPanel
{
private int[] numItemsSelected;
//private JPanel headerLabelPanel;
private static SerialPort serialPort;
private static boolean portFound = false;
public static final byte START_PREAMBLE = 0x1B;
public static final byte END_PREAMBLE = 0x5B;
public VendingPanel(int[] numItemsSelected)
{
//this("images/background.png");
setLayout(new BorderLayout());
add(UserInterface.jfxpanel5,BorderLayout.CENTER);
this.numItemsSelected = numItemsSelected;
//createHeaderPanel();
//add(headerLabelPanel, BorderLayout.PAGE_START);
String portName = "";
int i;
for (i=1; i<20; i++)
{
portName = "COM" + i;
if (openSerialPort(portName))
{
portFound = true;
break;
}
}
if (portFound)
{
spin();
}
}
/*public VendingPanel(String img)
{
super(img);
}
private void createHeaderPanel()
{
// GraphicsEnvironment ge =
// GraphicsEnvironment.getLocalGraphicsEnvironment();
// ge.getAllFonts();
ImagePanel logoPanel = new ImagePanel("images/logo.png");
Font font = new Font("Lucida Sans", Font.PLAIN, 50);
JLabel headerLabel = new JLabel("Vending your items");
headerLabel.setFont(font);
// headerLabel.setPreferredSize(new
// Dimension(1280-(logoPanel.getWidth()),logoPanel.getHeight()));
// headerLabel.setHorizontalTextPosition(JLabel.CENTER);
headerLabelPanel = new JPanel();
headerLabelPanel.setLayout(new BoxLayout(headerLabelPanel, BoxLayout.LINE_AXIS));
headerLabelPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
headerLabelPanel.setOpaque(false);
headerLabelPanel.add(logoPanel);
headerLabelPanel.add(Box.createRigidArea(new Dimension(160, 0)));
headerLabelPanel.add(headerLabel);
//headerLabelPanel.addMouseListener(this);
}*/
private boolean openSerialPort(String portName)
{
serialPort = new SerialPort(portName);
try
{
System.out.println("Port opened: " + serialPort.openPort());
System.out.println("Params set: " + serialPort.setParams(SerialPort.BAUDRATE_115200, 8, 1, 0));
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;// Prepare mask
serialPort.setEventsMask(mask);// Set mask
serialPort.addEventListener(new SerialPortReader());// Add SerialPortEventListener
Thread.sleep(1000L);
//serialPort.writeByte((byte) 'R');
return true;
}
catch (SerialPortException ex)
{
System.out.println(portName + " not found");
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
private void spin()
{
try
{
byte[] buffer = new byte[13];
buffer[0] = (byte) START_PREAMBLE;
buffer[1] = (byte) END_PREAMBLE;
buffer[2] = (byte) 'S';
for (int i=0; i<6; i++)
{
buffer[i+3] = (byte) numItemsSelected[i];
}
//buffer[3] = (byte) 1;
//buffer[4] = (byte) 0;
//buffer[5] = (byte) 0;
//buffer[6] = (byte) 0;
//buffer[7] = (byte) 0;
//buffer[8] = (byte) 0;
buffer[9] = (byte) 0;
buffer[10] = (byte) 0;
int checksum = (buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4] + buffer[5] + buffer[6] + buffer[7] + buffer[8] + buffer[9] + buffer[10]);
buffer[11] = (byte) (checksum >> 8);
buffer[12] = (byte) checksum;
serialPort.writeBytes(buffer);
// System.out.println(serialPort.getInputBufferBytesCount());
/*
* boolean dataRead = false; while (!dataRead) { if
* (serialPort.getInputBufferBytesCount() > 0) {
* System.out.println(serialPort.getInputBufferBytesCount()); byte[]
* buffer2 = serialPort.readBytes();
* System.out.println(serialPort.getInputBufferBytesCount());
* System.out.println(getHexString(buffer2, buffer2.length));
* dataRead = true;
* System.out.println(serialPort.purgePort(SerialPort
* .PURGE_RXCLEAR|SerialPort.PURGE_TXCLEAR)); } }
*/
}
catch (SerialPortException ex)
{
System.out.println(ex);
}
}
private static String getHexString(byte[] b, int length)
{
String result = "";
for (int i = 0; i < length; i++)
{
result = result + Integer.toString((b[i] & 0xFF) + 256, 16).substring(1);
}
return result;
}
/*private void returnToStart()
{
CardLayout cl = (CardLayout) parent.getLayout();
//System.out.println(parent.getComponentCount());
for (int i = 1; i < parent.getComponentCount(); i++)
{
cl.removeLayoutComponent(parent.getComponent(i));
// parent.remove(parent.getComponent(i));
}
}*/
/*
* In this class must implement the method serialEvent, through it we learn
* about events that happened to our port. But we will not report on all
* events but only those that we put in the mask. In this case the arrival
* of the data and change the status lines CTS and DSR
*/
class SerialPortReader implements SerialPortEventListener
{
public void serialEvent(SerialPortEvent event)
{
if (event.isRXCHAR())
{// If data is available
if (event.getEventValue() > 0)
{
try
{
String response = getHexString(serialPort.readBytes(), event.getEventValue());
System.out.println("Response: " + response);
System.out.println("Purge: " + serialPort.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR));
try
{
if (serialPort.isOpened())
{
System.out.println("Port closed: " + serialPort.closePort());
}
}
catch (SerialPortException e1)
{
e1.printStackTrace();
}
//returnToStart();
//serialPort.closePort();
//System.exit(0);
}
catch (SerialPortException e)
{
e.printStackTrace();
}
}
/*
* if (event.getEventValue() == 10) {// Check bytes count in the
* input buffer // Read data, if 10 bytes available try { byte
* buffer[] = serialPort.readBytes(10); } catch
* (SerialPortException ex) { System.out.println(ex); } }
*/
}
else if (event.isCTS())
{// If CTS line has changed state
if (event.getEventValue() == 1)
{// If line is ON
System.out.println("CTS - ON");
}
else
{
System.out.println("CTS - OFF");
}
}
else if (event.isDSR())
{// /If DSR line has changed state
if (event.getEventValue() == 1)
{// If line is ON
System.out.println("DSR - ON");
}
else
{
System.out.println("DSR - OFF");
}
}
}
}
}