简体   繁体   English

通过网络发送数据

[英]Sending data via the network

I would like to create a program that will emulate a device connected to the network and send signals through a specific port. 我想创建一个程序来模拟连接到网络的设备并通过特定端口发送信号。

The device is connected to the network and sends data through a port. 设备已连接到网络,并通过端口发送数据。 On the server(or computer) I have running the CPR Manager v.4.3.0.1 from Lantronix that will associate the IP:PORT to a virtual COM port on the computer. 在服务器(或计算机)上,我运行了Lantronix的CPR Manager v.4.3.0.1,它将IP:PORT关联到计算机上的虚拟COM端口。 I have a java program that listens to the COM ports and performs an action, this works great with the device. 我有一个Java程序,它侦听COM端口并执行操作,这在设备上很好用。

I tried writing a java app using the Socket class to perform the connection but it was un successful, on the CPR side it only registers a Disconnect when the very first line is executed: 我尝试使用Socket类编写Java应用程序来执行连接,但未成功,在CPR端,它仅在执行第一行时才注册Disconnect:

Socket socket = new Socket("192.168.1.160", 8888);

I also tried it using the UDP method and no message whats so ever is recorded. 我还使用UDP方法尝试了此操作,没有消息记录下来。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Also if there is no possible solution for Java then any other language would do fine. 同样,如果没有针对Java的可能解决方案,那么任何其他语言都可以。

EDIT: 编辑:

Here is the Java code where I am attempting to send the data 这是我尝试发送数据的Java代码

  public static void main(String[] args){
    try{
      Socket socket = new Socket("192.168.1.160", 8888);

      if(socket.isConnected()){
          System.out.println("It is connected.");
          socket.setKeepAlive(true);
          System.out.println(socket.isBound());
      }else{
          System.out.println("It is not connected.");
      }
      PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
      BufferedReader in =
              new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));
      String msg = "32";
      for(int i = 0; i < 50; i++){
          out.println(msg);
      }

      //Receive a reversed message
      msg = in.readLine();
      System.out.println("Server : " + msg);

    }catch(Exception ioe){
      ioe.printStackTrace();
    }
  }

Thanks. 谢谢。

Update 更新资料

I got in contact with some people of the devices and they showed me that there is a way to communicate straight via a TCP/IP connection sending there ASCII Command Protocols. 我与设备的一些人联系,他们告诉我,有一种方法可以通过发送ASCII命令协议的TCP / IP连接直接进行通信。 This would allow more in depth control at every level. 这将允许在每个级别上进行更多的深度控制。

So, now I am writing a java program that can communicate using these protocols. 因此,现在我正在编写一个可以使用这些协议进行通信的Java程序。 Because, I am not using a comm port anymore I am tying to emulate the baud rate, data bits, stop bit stuff. 因为,我不再使用通讯端口,而是要模拟波特率,数据位和停止位。 I will post when I have some that works. 当我有一些有效的方法时,我会发布。

Thanks for all the help. 感谢您的所有帮助。

if the product you are using is forwarding the traffic to a COM port should you be listening on the COM port not on a network connection. 如果您使用的产品将流量转发到COM端口,那么您应该在COM端口而不是网络连接上进行监听。 Sockets are for network traffic. 套接字用于网络流量。 A quick google search resulted this for me. 一个快速的谷歌搜索结果为我。 How to send data to COM PORT using JAVA? 如何使用JAVA将数据发送到COM PORT?

Maybe that will help? 也许会有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM