简体   繁体   中英

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. I have tested PC serial port by putty it's ok on putty.

namespace _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;
        }

    }

    }

}

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