小弟程序如下:各位大哥帮忙看看啊
import java.io.*;
import java.util.*;
import javax.comm.*;
public class main1
{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static char c34;
static char c26 = '\032';
static String s1 = "AT+CMGS=13889530147\n";
static String s2="AT+CMGF=1\n";
static String s3="AT+CMGS=13889530147\n";
static String s4="hello"+'\032'+"\n";
public main1() {}
public static void main(String args[])
{
try
{
CommPortIdentifier commportidentifier = CommPortIdentifier.getPortIdentifier("COM0");
serialPort = (SerialPort)commportidentifier.open("smsapp", 3000);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream.write(s2.getBytes());
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++){}
}
outputStream.write(s3.getBytes());
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++){}
}
outputStream.write(s4.getBytes());
outputStream.flush();
outputStream.close();
}
catch(Exception e)
{
System.out.println(e+"");
}
}
}
结果出现异常: javax.comm.NoSuchPortException
小弟不是很明白,这个程序也是在网上找的,有几个问题想请教下:PC机的串口是如何分配的,COM0换成COM1或者COM2可以吗?另外就是commportidentifier.open("smsapp", 3000); ,这里的两个参数什么意思?小弟初来乍到,请多多指教!
import java.io.*;
import java.util.*;
import javax.comm.*;
public class main1
{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static char c34;
static char c26 = '\032';
static String s1 = "AT+CMGS=13889530147\n";
static String s2="AT+CMGF=1\n";
static String s3="AT+CMGS=13889530147\n";
static String s4="hello"+'\032'+"\n";
public main1() {}
public static void main(String args[])
{
try
{
CommPortIdentifier commportidentifier = CommPortIdentifier.getPortIdentifier("COM0");
serialPort = (SerialPort)commportidentifier.open("smsapp", 3000);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream.write(s2.getBytes());
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++){}
}
outputStream.write(s3.getBytes());
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++){}
}
outputStream.write(s4.getBytes());
outputStream.flush();
outputStream.close();
}
catch(Exception e)
{
System.out.println(e+"");
}
}
}
结果出现异常: javax.comm.NoSuchPortException
小弟不是很明白,这个程序也是在网上找的,有几个问题想请教下:PC机的串口是如何分配的,COM0换成COM1或者COM2可以吗?另外就是commportidentifier.open("smsapp", 3000); ,这里的两个参数什么意思?小弟初来乍到,请多多指教!
呵呵
commportidentifier.open("smsapp", 3000);
第一个参数是你的应用程序的名字,第二个参数是端口开启的等待时间。
屬性。設備管理器查看你的port
然后代替程序里面的port
過3秒再調用smsapp
http://www.programmerstudy.com/programme/java/20084/97.html