휴대폰 번호로 문자 메시지를 보내고 있습니다. o/p 스트림을 사용하여 데이터를 쓰고 Port에서 i/p 스트림을 사용하여 데이터를 읽습니다. o/p 스트림이 제대로 작동하지만 i/p 스트림에서 데이터를 읽을 수 없습니다.컴 포트 입력 스트림
public class SendMsg implements SerialPortEventListener
{
Enumeration portList;
CommPortIdentifier portId;
SerialPort serialPort;
OutputStream outputStream;
InputStream inputStream;
Thread readThread;
String messageString;
String messageString1;
String strResponse="";
SendMsg pWriter;
String msg[]=new String[200];
int ix=0;
boolean msgEnd=true;
String className;
static Enumeration ports;
static CommPortIdentifier pID;
static String messageToSend = "ComPortSendMsg deatails!\n";
public SendMsg(String className) throws NoSuchPortException, IOException
{
this.className=className;
ports = CommPortIdentifier.getPortIdentifiers();
System.out.println("ports name"+ports);
while(ports.hasMoreElements())
{
pID = (CommPortIdentifier)ports.nextElement();
System.out.println("Port Name " + pID.getName());
if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println("Port Name 1 " + pID.getName());
if (pID.getName().equals("COM1"))
{
try {
System.out.println("Port Name 2 " + pID.getName());
System.out.println("COM1 found");
serialPort=(SerialPort)pID.open(className, 9600);
outputStream=serialPort.getOutputStream();
inputStream=serialPort.getInputStream();
break;
} catch (PortInUseException ex) {
Logger.getLogger(SendMsg.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
public void closePort()
{
try
{
inputStream.close();
System.out.println("Finished2");
outputStream.close();
System.out.println("Finished1");
serialPort.close();
System.out.println("Finished");
}
catch(Exception e)
{
System.out.println("Close Error"+e);
}
}
public void send(String phno,String msg)
{
String s = "AT+CMGF="+1;
System.out.println("AT+CMGF command :"+s);
messageString = "AT+CMGS=\""+phno+"\"\r";
messageString1 = msg+"\n" +(char)26;
System.out.println("AT CMGS "+messageString);
System.out.println("AT CMGS "+messageString1);
try
{
outputStream.write(s.getBytes());
System.out.print("this is send try block");
outputStream.write(messageString.getBytes());
outputStream.write(messageString1.getBytes());
Thread.sleep(2000);
byte[] b = new byte[1000];
String r="";
String r1="";
System.out.println(inputStream.available());
while (inputStream.available() > 0) {
int n = inputStream.read(b);
System.out.println("number of bytes"+n);
r= new String(b);
}
System.out.println("this is input stream msg"+r);
}
catch (Exception e)
{
System.out.println(e);
}
}
public static void main(String args[]) throws NoSuchPortException, IOException
{
SendMsg f=new SendMsg("Msg Sending");
f.send("9884345649","Wish U Happy");
System.out.println("---------END--------");
f.closePort();
}
@Override
public void serialEvent(SerialPortEvent spe) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
입력 포트에서 읽으면 어떻게됩니까? –
출력이 0임을 보여줍니다.이 코드는 System.out.println (inputStream.available()); while (inputStream.available()> 0) { int n = inputStream.read (b); System.out.println ("바이트 수"+ n); r = 새 문자열 (b); } – Naresh
출력 스트림을 쓰고 난 후 플러시하면 도움이됩니까? 또한 하나의 스레드에서 모든 것을 실행 중이므로 쓰기가 프로그램을 차단할 수있는 경우 고정됩니다. 제가 다량 사용하고 –