简体   繁体   中英

Sending data to a TCP Server

I'm trying to realize a program that sends data to a TCP-server and then receives the same message by COM-port. I have the following code but it sends the data but doesn't receive any data.

When I monitor the serial port I can't see that the data is received.

How do I fix this? Do I have to switch to a multi threading system or is there a other way to sync this? Because I can see that the COM-port is opened but not read out.

And when I test this with Putty I can get it to work.

public class Variables
{
    private static int v_VarI = 0;

    public static int VarI
    {
        get { return v_VarI; }
        set { v_VarI = value; }
    }

    private static int v_VarJ = 0;

    public static int VarJ
    {
        get { return v_VarJ; }
        set { v_VarJ = value; }
    }
}

class Program
{
    public class TcpTimeClient
    {
        private const int portNum = 6666;
        private const string hostName = "192.168.1.51";
        private const string data = "test";

        public static int Main(String[] args)
        {

            new SerialPortProgram();

            while (Variables.VarI < 10)
            {
                Byte[] bytesSent = Encoding.ASCII.GetBytes(data);
                try
                {
                    TcpClient client = new TcpClient(hostName, portNum);

                    NetworkStream ns = client.GetStream();

                    byte[] bytes = new byte[1024];
                    ns.Write(bytesSent, 0, bytesSent.Length);
                    int bytesRead = ns.Read(bytes, 0, bytes.Length);

                    client.Close();

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                Variables.VarI++;
                while (Variables.VarJ < Variables.VarI)
                {

                }
            }
            Console.ReadLine();
            return 0;
        }
    }

    public class SerialPortProgram
    {
        public SerialPort receiveport = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);

        public SerialPortProgram()
        {
            receiveport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

            receiveport.Open();
        }

        public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Show all the incoming data in the port's buffer
            string rec_data = receiveport.ReadExisting();
            Console.WriteLine(rec_data);
            Variables.VarJ++;
        }
    }
}

Update
I've found that the program I wrote and putty and an other program named Serial Port Monitor differ in the IOCTL_SERIAL_SET_WAIT_MASK.

Serial Port Monitor: 0x00 00 01 19
Putty: Unkown
Own program: 0x00 00 01 FB

Does anyone know how to change this mask? Because it's not in the System.IO.Ports.SerialPort class.

you can use this :

public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
         int length =  receiveport.BytesToRead;
        byte[] buf = new byte[length];
        SerialPort2.Read(buf, 0, length);

    }

buf array is your answer but in decimal mode . and you can changed it to want.

The wait mask is not your problem. Your wait mask enables the serial port for RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING and has everything except ERR turned on.

This is correct for the way .Net wraps serial ports, and anyway it is not exposed in the .NET serial port class. Don't waste time pursuing that difference; your problem lies elsewhere.

Your serial port code ran perfectly on my system and printed all the data received from an external serial source, so I suspect your problem is with the TCP server whose code you did not post, or with your serial ports. Perhaps your configuration requires that you transmit something before the port begins sending? Try something like:

            receiveport.Open();
            receiveport.WriteLine("?");

Then if you still have problems, try connecting to another serial device to see if you can prove your receive code is working for you as it is for me. If you still can't figure it out, post more information about what's happening on the serial source end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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