小弟程序如下:各位大哥帮忙看看啊
  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); ,这里的两个参数什么意思?小弟初来乍到,请多多指教!

解决方案 »

  1.   

    拉把椅子上帮"zhaohaijian()"顶,
    呵呵
      

  2.   

    你应该先看看你的可用串口是什么,然后再决定如何对串口进行操作。
      commportidentifier.open("smsapp", 3000);
      第一个参数是你的应用程序的名字,第二个参数是端口开启的等待时间。
      

  3.   

    wincomm.dll等文件要放在你用的jdk下面,如果program files下面有java的话,就放在它下面,如果没有则放在你的java_home下面
      

  4.   

    COM0,还是第一次听说。COMx里的这个x不是你随便写的,是驱动安装以后就固定了。另外,PC里缺省1开始,一般是COM1和Com2,也不排除很多PC只有一个串口,是COM1
      

  5.   

    右鍵我的電腦
    屬性。設備管理器查看你的port
    然后代替程序里面的port
    過3秒再調用smsapp
      

  6.   

    下面有介绍,还有需要的动态库及类库下载
    http://www.programmerstudy.com/programme/java/20084/97.html