0

I'm working on a TCP communication program for the chemestry analyzer "Mindray BS240". The problem is that the analyzer keep disconnecting and reconnecting (every 30s). Here is my code, what did I miss ?

private void startTcpListner()
    {
        try
        {
            var port = settings.LisPort;
            IPAddress localAddr = IPAddress.Parse(settings.LisIpAddress);

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            var bytes = new byte[256];
            String data = null;

            LogManager.GetCurrentClassLogger().Info("Waiting for a connection... ");

            // Perform a blocking call to accept requests.
            // You could also use server.AcceptSocket() here.
            var client = server.AcceptTcpClient();
            LogManager.GetCurrentClassLogger().Info("Connected !");

            data = null;

            // Get a stream object for reading and writing
            stream = client.GetStream();

            // Enter the listening loop.
            while (true)
            {
                while (!stream.DataAvailable) ;

                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    var line = Encoding.UTF8.GetString(bytes, 0, i);
                    LogManager.GetCurrentClassLogger().Info("Received: {0}", line);

                    data += line;

                    if (line.Length > 3 &&
                        line[line.Length - 2] == Hl7Helper.FileSeparator &&
                        line[line.Length - 1] == Hl7Helper.CarriageReturn)
                    {
                        handleMessage(data);
                        data = null;
                    }
                }
            }
        }
        catch (SocketException e)
        {
            LogManager.GetCurrentClassLogger().Error("SocketException: {0}", e);
        }
        catch (Exception e)
        {
            LogManager.GetCurrentClassLogger().Error(e);
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }
    }

In the log file, I have :

Waiting for a connection...
Connected !
SidAhmed
  • 2,332
  • 2
  • 25
  • 46
  • You should put a Sleep in `while (!stream.DataAvailable) ;` otherwise your PC will use all the CPU time. – Neil Sep 01 '22 at 12:49
  • Your code is just reading, does the analyser need prompting to start sending, or does it just send as soon as a connection is made? – Neil Sep 01 '22 at 12:52
  • @Neil : The analyzer send the message directly, as soon as there is a result or sample query. It does not need a prompting. – SidAhmed Sep 01 '22 at 13:35

0 Answers0