Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#任务完成后从2个串行端口读取数据_C#_Gps_Event Handling_Serial Port_Rfid - Fatal编程技术网

C#任务完成后从2个串行端口读取数据

C#任务完成后从2个串行端口读取数据,c#,gps,event-handling,serial-port,rfid,C#,Gps,Event Handling,Serial Port,Rfid,我有一个GPS跟踪器和一个RFID阅读器。 我想在我把我的RFID卡放在der RFID阅读器上后读取最后一个GPS位置 在我的代码中,我得到了GPS坐标。我有两个串行端口 “gpsPort”和“rfidPort”。我不知道如何以这种方式中断两个事件处理程序。你能解决这个问题吗 这是我的密码: class Program { static void Main(string[] args) { SerialPort gpsPort = new SerialPort(

我有一个GPS跟踪器和一个RFID阅读器。 我想在我把我的RFID卡放在der RFID阅读器上后读取最后一个GPS位置

在我的代码中,我得到了GPS坐标。我有两个串行端口 “gpsPort”和“rfidPort”。我不知道如何以这种方式中断两个事件处理程序。你能解决这个问题吗

这是我的密码:

class Program
{
    static void Main(string[] args)
    {
        SerialPort gpsPort = new SerialPort("COM5");
        gpsPort.BaudRate = 9600;
        gpsPort.Parity = Parity.None;
        gpsPort.StopBits = StopBits.One;
        gpsPort.DataBits = 8;
        gpsPort.Handshake = Handshake.None;
        gpsPort.RtsEnable = true;
        gpsPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        gpsPort.Open();

        SerialPort rfidPort = new SerialPort("COM4");
        rfidPort.BaudRate = 9600;
        rfidPort.Parity = Parity.None;
        rfidPort.StopBits = StopBits.One;
        rfidPort.DataBits = 8;
        rfidPort.Handshake = Handshake.None;
        rfidPort.RtsEnable = true;
        rfidPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler2);
        rfidPort.Open();
        Console.ReadKey();            
    }

     public static void  DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {

        SerialPort sp = (SerialPort)sender;

        string indata = sp.ReadExisting();

        if (indata.Contains("GPRMC"))
        {
            string[] sentence = indata.Split(',');

            string latitude = sentence[3].Substring(0, 2) + "°";
            latitude = latitude + sentence[3].Substring(2);
            latitude = latitude + sentence[4];

            string longitude = sentence[5].Substring(2, 1) + "°";
            longitude = longitude + sentence[5].Substring(3);
            longitude = longitude + sentence[6];

            Console.Write("Latitude:" + latitude + Environment.NewLine + "Longitude:" + longitude + Environment.NewLine + Environment.NewLine);
        }            
    }

     public static void DataReceivedHandler2(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();

        Console.Write(indata + Environment.NewLine);       
    }
}

使
string indata
成为第一个DataReceived事件范围之外的静态变量

class Program
{
    private static string indata_GPS = "";
....
}
现在,您应该从GPS读取此变量:

public static void  DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    indata_GPS = sp.ReadExisting();
一旦第二个
数据接收到
事件从RFID设备触发,您只需从
indata\u GPS
读取值即可。这样,您将从GPS获得最新的值

public static void DataReceivedHandler2(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();

    Console.Write("RFID: " + indata + Environment.NewLine);
    Console.Write("GPS latest Data: " + indata_GPS  + Environment.NewLine);
}
我不知道如何中断两个事件处理程序


不需要打断任何事情;)

使
string indata
成为第一个DataReceived事件范围之外的静态变量

class Program
{
    private static string indata_GPS = "";
....
}
现在,您应该从GPS读取此变量:

public static void  DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    indata_GPS = sp.ReadExisting();
一旦第二个
数据接收到
事件从RFID设备触发,您只需从
indata\u GPS
读取值即可。这样,您将从GPS获得最新的值

public static void DataReceivedHandler2(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();

    Console.Write("RFID: " + indata + Environment.NewLine);
    Console.Write("GPS latest Data: " + indata_GPS  + Environment.NewLine);
}
我不知道如何中断两个事件处理程序


不需要打断任何事情;)

谢谢你,这很有效!但我每次都会从控制台中的最后一个GPS数据中得到2个标签。你知道为什么吗?@hx_9009我真的不知道。我不知道你的代码到底是什么样子。您是否有
控制台。在两个
DataReceived
事件中写入
?没有,只有一个控制台。写入DataReceivedHandler2。当我第一次启动应用程序时,我只得到一个标签。但之后,我总是得到两个标签谢谢你,它的工作!但我每次都会从控制台中的最后一个GPS数据中得到2个标签。你知道为什么吗?@hx_9009我真的不知道。我不知道你的代码到底是什么样子。您是否有
控制台。在两个
DataReceived
事件中写入
?没有,只有一个控制台。写入DataReceivedHandler2。当我第一次启动应用程序时,我只得到一个标签。但之后,我总是得到两个标签