C#シリアルポート受信データ送信


      :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace SendData
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort();

            Console.WriteLine("  ( COM1):");
            port.PortName = Console.ReadLine();

            Console.WriteLine("   :");
            port.BaudRate = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("   :");
            port.DataBits = Convert.ToInt32(Console.ReadLine());

            int stopBits = 0;
            Console.WriteLine("   :");
            stopBits = Convert.ToInt32(Console.ReadLine());
            switch (stopBits)
            {
                case 0:
                    port.StopBits = StopBits.None;
                    break;
                case 1:
                    port.StopBits = StopBits.One;
                    break;
                case 2:
                    port.StopBits = StopBits.Two;
                    break;
                default:
                    port.StopBits = StopBits.None;
                    break;
            }

            try
            {
                port.Open();
                string sendData="";
                bool exitFlag=false;
                while (exitFlag == false)
                {
                    Console.WriteLine("      :");
                    sendData = Console.ReadLine();
                    if (sendData.CompareTo("exit") == 0) 
                        break;
                    else
                        port.WriteLine(sendData);
                }
                port.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}
      :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace RecvData
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort();

            Console.WriteLine("  ( COM1):");
            port.PortName = Console.ReadLine();

            Console.WriteLine("   :");
            port.BaudRate = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("   :");
            port.DataBits = Convert.ToInt32(Console.ReadLine());

            int stopBits = 0;
            Console.WriteLine("   :");
            stopBits = Convert.ToInt32(Console.ReadLine());
            switch (stopBits)
            {
                case 0:
                    port.StopBits = StopBits.None;
                    break;
                case 1:
                    port.StopBits = StopBits.One;
                    break;
                case 2:
                    port.StopBits = StopBits.Two;
                    break;
                default:
                    port.StopBits = StopBits.None;
                    break;
            }

            try
            {
                port.Open();
                bool exitFlag = false;
                int n = 0;
                while (exitFlag == false)
                {
                    Console.WriteLine(port.ReadLine());
                    n++;
                    if (n == 999999)
                    {
                        Console.WriteLine("    ?(y/s)");
                        string ans = Console.ReadLine();
                        if (ans.CompareTo("y") == 0)
                            exitFlag = true;
                        else
                            n = 0;
                    }
                }
                port.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}