Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

C# to communicate with a COM port.

Hi,

I am looking for get a simple sample in C# that can allow me to open up a serial port
say COM1 and do the following:

Send a ATi0 <CR>
Read the response that comes back from the serial port / modem.


Thanks,

Ward.

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See example code below:
using System;
using System.IO.Ports;

namespace ConsoleApplication1
{
    class Program
    {
        static SerialPort port;

        static void Main(string[] args)
        {
            port = new SerialPort("COM1", 9600, Parity.None, 8);
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
            port.Write("ATi0\r\n");
            Console.ReadLine();
        }

        static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Console.Write( port.ReadExisting() );
        }
    }
}

Open in new window