简体   繁体   English

Wpf 串行端口接收到在 Windows 10 上工作但在 Windows 7 上不工作的数据

[英]Wpf Serial port received data working on Windows 10 but not working on Windows 7

I have created a Wpf applicationvisual studio 2019, it runs perfectly on Windows 10 but don't run on Windows 7, the code is simple just open a serial port and also event handler for receiving and after receive data it just show on console.我创建了一个 Wpf applicationvisual studio 2019,它可以在 Windows 10 上完美运行,但不能在 Windows 7 上运行,代码很简单,只需打开一个串行端口和事件处理程序,用于接收和接收数据后,它只显示在控制台上。 I have tested PC serial port by putty it's ok on putty.我已经用腻子测试了PC串口,在腻子上没问题。

namespace _04December2019_2 {命名空间_04December2019_2 {

public partial class UserControl1 : UserControl
{

    //    String Test1;
    public static String Test1="";
    public static string report_id="";
    public static string patient_id="";
    public UserControl1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

    }


    public static void Main()
    {
        string testString;
        Console.Write("Enter COM PORT: ");
        testString = Console.ReadLine();
        Console.WriteLine("You entered '{0}'", testString);

        SerialPort mySerialPort = new SerialPort(testString);

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.RtsEnable = true;

        //mySerialPort.DtrEnable = true;


        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);


        try
        {
            mySerialPort.Open();
            Console.WriteLine("Server Started");
        }
        catch
        {
            MessageBox.Show("Error in communication");
        }



        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();



        Console.ReadKey();

        mySerialPort.Close();
    }


    public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        String s = sp.ReadExisting();
        if(s==" ")
        {

        }
        else if (s == "@")
        {
            Test1 = String.Empty;
        }
        else if (s == "&")
        {

            Console.WriteLine(Test1);
            try
            {
               // string_receive(Test1);
            }
            catch
            {
                MessageBox.Show("Error");
            }
            report_id = string.Empty;
            Test1 = string.Empty;

        }
        else
        {
            Test1 += s;
        }

    }

    }

} }

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

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