⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialportio.java

📁 无线传感器网络操作系统源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * "Copyright (c) 2001 and The Regents of the University * of California.  All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * $\Id$ *//** * File: SerialPortIO.java * * Description: * The SerialPortIO handles the collection of packets * from a mote connected to the serial port.  The Constructor * takes in an already open input stream from which to read * data. * * @author <a href="mailto:bwhull@sourceforge.net">Bret Hull</a> */package net.tinyos.SerialForwarder;import java.util.*;import java.io.*;import javax.comm.*;import java.net.*;public class SerialPortIO extends Thread{    private static final String   CLASS_NAME = "SerialPortIO";    private static SerialPortIO   runningSerialPortIO = null;    private FileInputStream       m_fis           = null;    private InputStream           m_is            = null;    private OutputStream          m_os            = null;    private Vector                m_vctPSForwarders = new Vector ();    private boolean               m_bDummyData    = false;    private boolean               m_bShutdown     = false;    private boolean               m_bSourceSim    = false;    private boolean               m_bSourceDB     = false;    private boolean               m_bTerminated   = false;    private boolean		  m_bSock	  = false;    int                           m_nBytes        = 0;    int                           dmmyDtPrd       = 1000;    private SerialPort            serialPort      = null;    private DBReader              m_dbReader      = null;    /* the following are used if the "serial port" is really a socket */    private Socket		  m_socket	  = null;    public synchronized static void InitSerialPortIO ( )    {        if ( runningSerialPortIO == null )        {            runningSerialPortIO = new SerialPortIO ( );            runningSerialPortIO.start();        }    }    public static void RegisterPacketForwarder ( ClientServicer cs)    {        InitSerialPortIO ( );        if ( runningSerialPortIO != null )        {            runningSerialPortIO.RegisterPSForwarder ( cs );        }    }    public static void UnregisterPacketForwarder ( ClientServicer cs )    {        if ( runningSerialPortIO != null )        {            runningSerialPortIO.UnregisterPSForwarder ( cs );        }    }    public static void WriteBytes ( byte[] data )    {        InitSerialPortIO ( );        if ( runningSerialPortIO != null )        {            runningSerialPortIO.WriteToPort ( data );        }    }    public SerialPortIO ( )    {        m_bDummyData = SerialForward.useDummyData;        m_nBytes = SerialForward.nBytesRead;        m_bSourceSim  = SerialForward.bSourceSim;	m_bSourceDB = SerialForward.bSourceDB;	m_bSock = SerialForward.commPort_is_socket;    }    public void run ( )    {        SerialForward.VERBOSE ( "SerialPortIO: initializing" );        SetIOStreams ( );        ReadData ( );        Terminate ( );        SerialForward.VERBOSE ( "SerialPortIO: closing data source" );    }    private void SetIOStreams ( )    {        if ( m_bSourceSim )        {            SerialForward.VERBOSE ( "Reading data from TOS Simulator" );            ListenForSim ( );        }        else if ( m_bDummyData )        {            SerialForward.VERBOSE ( "Reading Dummy data" );            SerialForward.PACKET_DUMMY_DATA = new byte[SerialForward.PACKET_SIZE];            SerialForward.PACKET_DUMMY_DATA[0] = (byte) 0x7E;            for (int ii = 1; ii < SerialForward.PACKET_DUMMY_DATA.length; ii++)            {                SerialForward.PACKET_DUMMY_DATA[ii] = (byte)ii;            }        }	else if ( m_bSourceDB ) {	    m_dbReader = new DBReader (SerialForward.strDBUser, SerialForward.strDBPassword, SerialForward.bPostgresql );	}        else if ( m_bSock ) {            // read from socket            try            {                OpenSocket( );                SerialForward.VERBOSE("Successfully opened " + SerialForward.commHost);                m_is = m_socket.getInputStream();                m_os = m_socket.getOutputStream();            }            catch ( Exception e )            {                SerialForward.VERBOSE ( "Unable to open socket to host [" +			SerialForward.commHost + ", port " +			SerialForward.commTCPPort + "] as serial port" );                m_is = null;                m_os = null;            }	}	else        {            // read from a true serial port            try            {                OpenCommPort ( );                SerialForward.VERBOSE( "Successfully opened " + SerialForward.commPort );                m_is = serialPort.getInputStream();                m_os = serialPort.getOutputStream();            }            catch ( Exception e )            {                SerialForward.VERBOSE ( "Unable to open serial port" );                PrintAllPorts ( );                m_is = null;                m_os = null;            }        }        return;    }    private void ListenForSim ( )    {        ServerSocket socketSimListen = null;        Socket socketSimRead = null;        try {            SerialForward.VERBOSE( "Listening for TOS Simulator on port " + SerialForward.TOSSIM_LISTENPORT);            socketSimListen = new ServerSocket ( SerialForward.TOSSIM_LISTENPORT );            socketSimRead  = socketSimListen.accept();            m_is = socketSimRead.getInputStream();            SerialForward.VERBOSE( "Read Connection opened to TOS Simulator" );        }        catch ( IOException e )        {            SerialForward.VERBOSE( "Cannot listen for TOS Simulator on port " + SerialForward.TOSSIM_LISTENPORT);            m_bShutdown = true;            return;        }        try {            Socket socketSimWrite = new Socket ( SerialForward.TOSSIM_ADDRESS, SerialForward.TOSSIM_WRITEPORT );            m_os = socketSimWrite.getOutputStream();        }        catch ( IOException e )        {            SerialForward.VERBOSE( "Cannot open write connection to TOS Simulator" );        }    }    private void OpenCommPort() throws        NoSuchPortException, PortInUseException, IOException,	UnsupportedCommOperationException    {          CommPortIdentifier portId =	    CommPortIdentifier.getPortIdentifier( SerialForward.commPort );          serialPort =	    (SerialPort)portId.open(CLASS_NAME, CommPortIdentifier.PORT_SERIAL);          serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);          serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8,	    SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);    }    private void OpenSocket() throws        UnknownHostException, IOException, NumberFormatException    {	    // use a socket as the serial port	    m_socket = new Socket(		SerialForward.commHost,		Integer.parseInt(SerialForward.commTCPPort)	    );    }    private void ReadData ( )    {        if ( m_bSourceSim )        {            //ReadSimData ( );        }        else if ( m_bDummyData )        {            ReadDummyData ( );        }	else if ( m_bSourceDB ) {	    ReadDBData ( );	}        else        {            ReadSerialData ( );        }    }    private void WriteToPort ( byte[] data )    {        if ( m_bSourceSim )        {            WriteSerialData ( data );        }        else if ( m_bDummyData )        {            // do nothing        }        else        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -