Announcement

Collapse
No announcement yet.

XPort connection only works in debugger

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • KevinB
    started a topic XPort connection only works in debugger

    XPort connection only works in debugger

    I have a device that has a Lantronix xport for IP based communications. I'm trying to write a simple .net c# app to send some commands to the device.

    My code (see below) works fine when I step through it in the debugger mode but fails when I run it in normal execution.

    I've switch off the firewall to ensure it not that. I don't get any exceptions or errors, the device just doesn't get the same result.

    Looking for some pointers on what a could check next.

    Many thanks in advance.


    Code:
            public static void Function1(string hostname, int port)
            {
                IPAddress ipAddress = IPAddress.Parse(hostname);
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
    
                TcpClient comipClient = new TcpClient(AddressFamily.InterNetwork);
                comipClient.Connect(remoteEP);
                NetworkStream stream = comipClient.GetStream();
                
                System.Text.ASCIIEncoding str = new System.Text.ASCIIEncoding();
                byte[] data = new byte[] { 0x5C, 0x57, 0x31, 0x32, 0x33, 0x34, 0x2F };
                stream.Write(data, 0, data.Length);
                stream.Flush();
                
                data = new byte[] { 0x5C, 0x59, 0x01, 0x2F };
                stream.Write(data, 0, data.Length);
                stream.Flush();
    
                comipClient.Close();
    
            }

  • joshjack
    replied
    Try commenting out the stream.Flush().

    Also consider adding a 100ms thread sleep after .Connect() to allow the TCP framework to initialize and connect.

    J

    Leave a comment:

Working...
X