All pastes #187204 Raw Edit

Anonymous

public text v1 · immutable
#187204 ·published 2006-09-30 21:40 UTC
rendered paste body
using System;
using System.IO.Ports;

namespace testGPS
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort sp = new SerialPort("COM1", 4800, Parity.None, 8, StopBits.One);
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

            try
            {
                sp.Open();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error opening port:\r\n" + ex.ToString());
            }
            finally
            {
                sp.Close();
                sp.Dispose();
            }
        }

        static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            try
            {
                Console.WriteLine(sp.ReadLine());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error reading from port:\r\n" + ex.ToString());
            }
        }
    }
}