Search code examples
c#gpsevent-handlingserial-portrfid

C# read data from 2 SerialPorts after task


I have a GPS- Tracker and a RFID-Reader. I would like to read the last GPS-Position after I put my RFID-Card on der RFID-Reader.

In my code I getting permanent the GPS-coordinantes. And I have two SerialPorts "gpsPort" and "rfidPort". I dont know how Interrupt two EventHandler in this way. Can you solve the Problem or any idea ?

Here is my code:

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);       
    }
}

Solution

  • Make string indata a static variable outside the scope of the firs DataReceived event.

    class Program
    {
        private static string indata_GPS = "";
    ....
    }
    

    now you should read from GPS into this variable:

    public static void  DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        indata_GPS = sp.ReadExisting();
    

    as soon as the second DataReceived event fires from the RFID device you simply read out the value from indata_GPS. This way you will have the latest value from 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);
    }
    

    I dont know how Interrupt two EventHandler

    No need to interrupt anything ;)