Port Writer : COM Port « Development Class « Java






Port Writer

 

import java.io.*;
import javax.comm.*;
import java.util.*;

public class PortWriter
{
    static Enumeration ports;
    static CommPortIdentifier pID;
    static OutputStream outStream;
    SerialPort serPort;
    static String messageToSend = "message!\n";
    
    public PortWriter() throws Exception{
        serPort = (SerialPort)pID.open("PortWriter",2000);
        outStream = serPort.getOutputStream();
        serPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE);
    }    
    
    public static void main(String[] args) throws Exception{
        ports = CommPortIdentifier.getPortIdentifiers();
        
        while(ports.hasMoreElements())
        {
            pID = (CommPortIdentifier)ports.nextElement();
            System.out.println("Port " + pID.getName());
            
            if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if (pID.getName().equals("COM1"))
                {
                    PortWriter pWriter = new PortWriter();
                    System.out.println("COM1 found");
                }
            }
        }
        outStream.write(messageToSend.getBytes());
    }
}

   
  








Related examples in the same category

1.Querying Available COM Ports
2.Printing a FilePrinting a File
3.List the ports
4.Open a serial port using Java CommunicationsOpen a serial port using Java Communications
5.Subclasses CommPortOpen and adds send/expect handling for dealing with Hayes-type modems
6.Read from a Serial port, notifying when data arrives
7.Choose a port, any port!
8.Read from multiple Serial ports, notifying when data arrives on any
9.Demonstrate the port conflict resolution mechanism
10.Port Finder
11.Port Reader
12.Port Sniffer
13.This program tries to do I/O in each direction using a separate ThreadThis program tries to do I/O in each direction using a separate Thread