Källkod, persondatorn (java)

/**  the computer end of the SpinLed 3500 system **/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import gnu.io.*;

public class SpinLED implements KeyListener,ActionListener
{
    JFrame frame;
    JTextField textField;
    int maxChars=29; //så många bokstäver man kan skriva på ett varv
    JMenuBar menuBar;
    JMenu options,ComChoose,actions,showText,showIm;
    JMenuItem exit,Image1,Image2,Image3,temp;
    JRadioButtonMenuItem COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,COM10;
    int baud=9600,inv=0;
    JScrollBar scrollSpeed;
    // FileReader filläs;
 
    //importerade \/
    static Enumeration          portList;
    static CommPortIdentifier   portId;
    static String               messageString = "Hello, world!";
    static SerialPort           serialPort;
    static PrintStream          outputStream;
    static boolean              outputBufferEmptyFlag = false;
    boolean portFound = false;
    String  defaultPort = "COM1";    

    public SpinLED()
    {
        frame=new JFrame("SpinLED 3500");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(400,400));
        
        menuBar=new JMenuBar();
        options=new JMenu("options");
        ComChoose=new JMenu("change COM-port");
        options.add(ComChoose);
        
        actions=new JMenu("actions");
        showIm=new JMenu("Show image");
        actions.add(showIm);
        
        options.addSeparator();
        ButtonGroup ComGroup = new ButtonGroup();
        
        
        scrollSpeed=new JScrollBar(JScrollBar.VERTICAL,0,1,0,7);
        frame.getContentPane().add(scrollSpeed, BorderLayout.EAST);
        
        COM1 = new JRadioButtonMenuItem("COM 1");
        COM1.setSelected(true);
        ComGroup.add(COM1);
        ComChoose.add(COM1);
        COM1.addActionListener(this);
        COM2 = new JRadioButtonMenuItem("COM 2");
        COM2.setSelected(false);
        ComGroup.add(COM2);
        ComChoose.add(COM2);
        COM2.addActionListener(this);
        COM3 = new JRadioButtonMenuItem("COM 3");
        COM3.setSelected(false);
        ComGroup.add(COM3);
        ComChoose.add(COM3);
        COM3.addActionListener(this);
        COM4 = new JRadioButtonMenuItem("COM 4");
        COM4.setSelected(false);
        ComGroup.add(COM4);
        ComChoose.add(COM4);
        COM4.addActionListener(this);
        COM5 = new JRadioButtonMenuItem("COM 5");
        COM5.setSelected(false);
        ComGroup.add(COM5);
        ComChoose.add(COM5);
        COM5.addActionListener(this);
        COM6 = new JRadioButtonMenuItem("COM 6");
        COM6.setSelected(false);
        ComGroup.add(COM6);
        ComChoose.add(COM6);
        COM6.addActionListener(this);
        COM7 = new JRadioButtonMenuItem("COM 7");
        COM7.setSelected(false);
        ComGroup.add(COM7);
        ComChoose.add(COM7);
        COM7.addActionListener(this);
        COM8 = new JRadioButtonMenuItem("COM 8");
        COM8.setSelected(false);
        ComGroup.add(COM8);
        ComChoose.add(COM8);
        COM8.addActionListener(this);
        COM9 = new JRadioButtonMenuItem("COM 9");
        COM9.setSelected(false);
        ComGroup.add(COM9);
        ComChoose.add(COM9);
        COM9.addActionListener(this);
        COM10 = new JRadioButtonMenuItem("COM 10");
        COM10.setSelected(false);
        ComGroup.add(COM10);
        ComChoose.add(COM10);
        COM10.addActionListener(this);
        
        menuBar.add(options);
        menuBar.add(actions);
        
        exit=new JMenuItem("exit");
        exit.addActionListener(this);
        options.add(exit);
        
        Image1=new JMenuItem("Image 1");
        Image1.addActionListener(this);
        showIm.add(Image1);
        
        Image2=new JMenuItem("Image 2");
        Image2.addActionListener(this);
        showIm.add(Image2);
        
        Image3=new JMenuItem("Image 3");
        Image3.addActionListener(this);
        showIm.add(Image3);
        
        temp=new JMenuItem("Show temperature");
        temp.addActionListener(this);
        actions.add(temp);
        
        
        textField=new JTextField(maxChars);
        textField.addKeyListener(this);
        frame.getContentPane().add(textField, BorderLayout.SOUTH);

        frame.setJMenuBar(menuBar);
        frame.pack();
        frame.setVisible(true);
        
        //importerat \/
            
        setComPort(defaultPort);
    } 
    
    public void keyTyped(KeyEvent e) 
    {
        char f=e.getKeyChar();
        if(f==e.VK_ENTER)
        {
            send(textField.getText());
            textField.setText("");
        }
    }
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("exit"))
        {
            if(portFound)
            serialPort.close();
            System.exit(1);
        }
        
        if(e.getActionCommand().equals("COM 1")) { setComPort("COM1"); }
        if(e.getActionCommand().equals("COM 2")) { setComPort("COM2"); }
        if(e.getActionCommand().equals("COM 3")) { setComPort("COM3"); }
        if(e.getActionCommand().equals("COM 4")) { setComPort("COM4"); }
        if(e.getActionCommand().equals("COM 5")) { setComPort("COM5"); }
        if(e.getActionCommand().equals("COM 6")) { setComPort("COM6"); }
        if(e.getActionCommand().equals("COM 7")) { setComPort("COM7"); }
        if(e.getActionCommand().equals("COM 8")) { setComPort("COM8"); }
        if(e.getActionCommand().equals("COM 9")) { setComPort("COM9"); }
        if(e.getActionCommand().equals("COM 10")) { setComPort("COM10"); }

        if(e.getActionCommand().equals("Image 1")) { setImage(1); }
        if(e.getActionCommand().equals("Image 2")) { setImage(2); }
        if(e.getActionCommand().equals("Image 3")) { setImage(3); }
        if(e.getActionCommand().equals("Image 4")) { setImage(4); }
        
        if(e.getActionCommand().equals("Show temperature"))
        {
            setTemp();
            System.out.println("temp");
        }
    }
    
    public void setComPort(String Cport)
    {
        System.out.println(Cport);
        defaultPort=Cport;
        if(portFound)
        serialPort.close();
        portFound=false;
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) 
        {
            portId = (CommPortIdentifier) portList.nextElement();

            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
            {
                    if (portId.getName().equals(Cport)) 
                    {
                          System.out.println("Found port " + Cport);

                          portFound = true;

                          try { serialPort = (SerialPort) portId.open("SimpleWrite", 2000); }
                          catch (PortInUseException e) 
                          {
                               System.out.println("Port in use.");
                               continue;
                          } 

                          try{ outputStream = new PrintStream(serialPort.getOutputStream(), true); } 
                          catch (IOException e) {}

                          try 
                          {
                               serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,  
                               SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                               System.out.println(baud+" Baud, 8 databits, 1 stopbit, no parity 
                               set");
                          } 
                          catch (UnsupportedCommOperationException e) {}
    

                          try { serialPort.notifyOnOutputEmpty(true); }
                          catch (Exception e) 
                          {
                            System.out.println("Error setting event notification");
                            System.out.println(e.toString());
                            System.exit(-1);
                          }
                     }
                        
                }
                if(!portFound)
                    System.out.println(Cport+" not found");
          }
    }
    
    public void send(String text)
    {
        byte[] control=new byte[2];
        control[0]=' ';
        control[1]=(byte)(0x80+scrollSpeed.getValue()+inv);
        if (!portFound) {
        System.out.println("port " + defaultPort + " not found.");
        }
        else{
            outputStream.write(control,1,1);
            if(text.length()<maxChars)
            {
                outputStream.write(text.getBytes(),0,text.length());
                for(int i=text.length();i<=maxChars;i++){ outputStream.write(control,0,1); }
            }
            else { outputStream.write(text.getBytes(),0,maxChars); }
        }
    }
    
    public void setImage(int nr)
    {
        byte imByte[]= new byte[1];
        imByte[0]=(byte)((nr-1)<<5+inv+scrollSpeed.getValue());
        outputStream.write(imByte,0,1);
    }
    
    public void setTemp()
    {
        byte tempByte[]= new byte[1];
        tempByte[0]=(byte)(0x01<<6+inv+scrollSpeed.getValue());
        outputStream.write(tempByte,0,1);
    }
    
    public static void main(String [] args)
    {
        new SpinLED();
    }
}
toppen på sidan tillbaka till SpinLED-sidan tillbaka till förstasidan [Teknik och data]

Valid HTML 4.01!