简体   繁体   English

向 TCP 服务器发送数据

[英]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.我正在尝试实现一个将数据发送到 TCP 服务器然后通过 COM 端口接收相同消息的程序。 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.因为我可以看到 COM 端口已打开但未读出。

And when I test this with Putty I can get it to work.当我用 Putty 测试它时,我可以让它工作。

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.我发现我编写的程序和 putty 以及另一个名为 Serial Port Monitor 的程序在 IOCTL_SERIAL_SET_WAIT_MASK 上有所不同。

Serial Port Monitor: 0x00 00 01 19串口监视器:0x00 00 01 19
Putty: Unkown腻子:未知
Own program: 0x00 00 01 FB自有程序:0x00 00 01 FB

Does anyone know how to change this mask?有谁知道这个面具怎么换? Because it's not in the System.IO.Ports.SerialPort class.因为它不在 System.IO.Ports.SerialPort 类中。

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 . buf 数组是您的答案,但在十进制模式下。 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.您的等待掩码为RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING启用串行端口,并且除了 ERR 之外的所有内容都已打开。

This is correct for the way .Net wraps serial ports, and anyway it is not exposed in the .NET serial port class.这对于 .Net 包装串行端口的方式是正确的,并且无论如何它不会在 .NET 串行端口类中公开。 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.您的串行端口代码在我的系统上完美运行并打印了从外部串行源接收到的所有数据,所以我怀疑您的问题出在您没有发布代码的 TCP 服务器上,或者您的串行端口上。 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.如果您仍然无法弄清楚,请发布有关串行源端发生的事情的更多信息。

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

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