Java PHP-WAMP服务器(Windows)中的串行端口(com端口)访问被拒绝

Java PHP-WAMP服务器(Windows)中的串行端口(com端口)访问被拒绝,java,php,tomcat,serial-port,wamp,Java,Php,Tomcat,Serial Port,Wamp,我一直在尝试编写一个PHP脚本,通过连接到服务器机器的GSM调制解调器发送SMS消息 下面是前面提到的PHP脚本 <?php $device = "COM7"; exec("mode $device BAUD=9600 PARITY=n DATA=8"); $comport = fopen($device, "w"); fputs($comport,"AT+CMGF=1\n\r"); fputs($comport,"AT+cmgs=\"xxxxxxxxxx\"\n\r"); fputs(

我一直在尝试编写一个PHP脚本,通过连接到服务器机器的GSM调制解调器发送SMS消息

下面是前面提到的PHP脚本

<?php 
$device = "COM7";
exec("mode $device BAUD=9600 PARITY=n DATA=8");
$comport = fopen($device, "w");
fputs($comport,"AT+CMGF=1\n\r");
fputs($comport,"AT+cmgs=\"xxxxxxxxxx\"\n\r");
fputs($comport,"sms text\n\r");
fputs($comport,chr(26));
fclose($comport);
echo "done";  
?>
上述代码在作为独立应用程序在服务器外部运行时起作用

下面是调用上述类以发送SMS的servlet代码

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class sendSMS extends HttpServlet {
       @Override
       public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // Set the response MIME type of the response message
        response.setContentType("text/html");
        // Allocate a output writer to write the response message into the network socket
        PrintWriter out = response.getWriter();

        SMSsender obj = new SMSsender();
        try {
          obj.sendMessage(null);  
        }
        catch(Exception e)
        {
          out.println(e.getMessage());
        }
        finally {
        out.close();  // Always close the output writer
        }
    }
 }
当这个servlet通过ApacheTomcat运行时,服务器会自动关闭,日志中出现以下错误

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000001cf32174465, pid=5828, tid=2276
#
# JRE version: Java(TM) SE Runtime Environment (10.0.2+13) (build 10.0.2+13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (10.0.2+13, mixed mode, tiered, compressed oops, g1 gc, windows-amd64)
# Problematic frame:
# C  [rxtxSerial.dll+0x4465]
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.

我能够解决这个问题

显然,windows不再允许通过虚拟文件名与串行端口通信。在PHP的例子中,我试图通过fopencom7与com端口取得联系。因此,这种方法似乎不再可行。这是大卫·吉布森说的。如果我能找到原始资料,我很想读更多关于这方面的文章

根据DavidGibson的帖子,像DIO这样的PHP扩展在Windows环境中不是很稳定。所以我停止了寻找PHP代码的解决方案

现在来看看Java解决方案

早些时候,我尝试使用RXTX java库。尽管该代码作为一个独立的应用程序运行,但在通过tomcat运行时出现访问冲突错误。 我想知道RXTX是否还在积极开发中

无论如何,我已经厌倦了平台无关的解决方案。非常惊人,它不需要任何DLL或任何其他本机库

我终于能够通过servlet发送短信了,这要感谢这个很棒的、易于使用的库。我已经在下面发布了我的工作Java代码供您参考

我希望这能帮助那些面临同样问题的人!干杯

感谢Mehdi、Erwin和Sanguinary为使用这个令人惊叹的Stackoverflow平台提供的指导,当然也感谢开发人员

注意:下面的代码杂乱无章,未经优化。此代码仅供参考

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.io.InputStream;
import java.util.Scanner;
import com.fazecast.jSerialComm.*;

public class sendSMS extends HttpServlet {
    SerialPort ubxPort;

    static String messageString1 = "AT";
    static String messageString2 = "AT+CMGF=1"; 
    static String messageString3 = "AT+CMGS=\"+xxxxxxxxxx\"";
    static String messageString4 = "TESTY2";
    static char enter = 13;
    static char CTRLZ = 26;


   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {

      // Set the response MIME type of the response message
      response.setContentType("text/html");
      // Allocate a output writer to write the response message into the network socket
      PrintWriter out = response.getWriter();


      try {

          ubxPort = SerialPort.getCommPort("com7");
          boolean openedSuccessfully = ubxPort.openPort();
          out.println("h1<br>");
          if(openedSuccessfully)
          {
            out.println("Port Opened<br>");
            byte[] buffer = (messageString1 + enter).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("1 sent<br>");

            InputStream in = ubxPort.getInputStream();
            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

            buffer = (messageString2 + enter).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("2 sent<br>");

            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

            buffer = (messageString3 + enter).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("3 sent<br>");

            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

            buffer = (messageString4 + CTRLZ).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("4 sent<br>");

            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

          }

      }
      catch(Exception e)
      {
          out.println("here3");
          out.println(e.getMessage());
      }
      finally {
          ubxPort.closePort();
          out.println("here4");
         out.close();  // Always close the output writer
      }
   }
}

不要将错误发布为截图。嗨。我是新来的。我想说明确切的错误是什么。欢迎来到Stackoverflow。您应该始终将代码和错误作为问题本身的文本而不是图像发布。另外,请检查您是否使用java标记它,并讨论java程序的问题,您应该在java中发布一个,并包含确切的错误消息。非常感谢。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.io.InputStream;
import java.util.Scanner;
import com.fazecast.jSerialComm.*;

public class sendSMS extends HttpServlet {
    SerialPort ubxPort;

    static String messageString1 = "AT";
    static String messageString2 = "AT+CMGF=1"; 
    static String messageString3 = "AT+CMGS=\"+xxxxxxxxxx\"";
    static String messageString4 = "TESTY2";
    static char enter = 13;
    static char CTRLZ = 26;


   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {

      // Set the response MIME type of the response message
      response.setContentType("text/html");
      // Allocate a output writer to write the response message into the network socket
      PrintWriter out = response.getWriter();


      try {

          ubxPort = SerialPort.getCommPort("com7");
          boolean openedSuccessfully = ubxPort.openPort();
          out.println("h1<br>");
          if(openedSuccessfully)
          {
            out.println("Port Opened<br>");
            byte[] buffer = (messageString1 + enter).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("1 sent<br>");

            InputStream in = ubxPort.getInputStream();
            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

            buffer = (messageString2 + enter).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("2 sent<br>");

            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

            buffer = (messageString3 + enter).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("3 sent<br>");

            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

            buffer = (messageString4 + CTRLZ).getBytes();
            ubxPort.writeBytes(buffer, buffer.length);
            out.println("4 sent<br>");

            try
            {
               for (int j = 0; j < 1000; ++j)
                  System.out.print((char)in.read());
               in.close();
            } catch (Exception e) { e.printStackTrace(); }

          }

      }
      catch(Exception e)
      {
          out.println("here3");
          out.println(e.getMessage());
      }
      finally {
          ubxPort.closePort();
          out.println("here4");
         out.close();  // Always close the output writer
      }
   }
}