0

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. I have a java program that listens to the COM ports and performs an action, this works great with the device.

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 socket = new Socket("192.168.1.160", 8888);

I also tried it using the UDP method and no message whats so ever is recorded.

Any help would be greatly appreciated. Also if there is no possible solution for Java then any other language would do fine.

EDIT:

Here is the Java code where I am attempting to send the data

  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. This would allow more in depth control at every level.

So, now I am writing a java program that can communicate using these protocols. 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.

1 Answers1

0

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. Sockets are for network traffic. A quick google search resulted this for me. How to send data to COM PORT using JAVA?

Maybe that will help?

Community
  • 1
  • 1
Joshua
  • 222
  • 1
  • 5
  • Thanks for the responce, Yea I could most definitly do that but it is not what I'm looking for. I need to make the app send the signal via the network so my app that reads the com ports wont know the difference between the signals. The goal of this is to send a constant signal so it can get recorded, this will help determine if the ip:port connections ever get disconnected.(because the com port will still be available) – Martin Salinas Apr 17 '13 at 17:12
  • Oh that makes much more sense. Is this signal sender also the receiver? Or they two different boxes? Based on your response and the question it seems like they are the same box. – Joshua Apr 17 '13 at 17:21
  • It is most definitely the receiver but it doesn't necessarily have to be the sender. I think if the signal gets sent correcetly and interpreted correctly it shouldn't matter where it is, but then again I'm not 100% sure. – Martin Salinas Apr 17 '13 at 18:51
  • well you may run into trouble if you are the sender and the reciever because i have no idea if that CPR manager handles loopback addresses. Can you post your code for sending and I'll be able to tell you if you are sending the correct traffic. – Joshua Apr 17 '13 at 19:09
  • I edited the original post with the code. Thanks for your help. – Martin Salinas Apr 17 '13 at 19:24
  • so in your code you never flush your output buffer...that may be the case. When you are doing anything with buffers, data is cached in memory and needs to be flushed in order to send the data across the line. – Joshua Apr 18 '13 at 11:53
  • I tried adding the flush to the code, but the result is the same. It doesn't show any activity and on the monitor when it hits the socket constructor I get the disconnect message. – Martin Salinas Apr 18 '13 at 19:50