By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

SerialPort

P: n/a
I set up two virtual COM ports 3 and 4 using com0com. I tried to test
it with the following program. However, the DataReceived event
handlers never get called. Can anyone with SerialPort class
experience double check my code so I can narrow it down to com0com not
being setup correctly?

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.IO;
using System.Text;
namespace com0comTest
{
class Program
{
static SerialPort _com3;
static SerialPort _com4;

static void InitCom3()
{
_com3 = new SerialPort();
_com3.DataReceived +=new
SerialDataReceivedEventHandler(com3_DataReceived);
_com3.ErrorReceived +=new
SerialErrorReceivedEventHandler(com3_ErrorReceived );

if(_com3.IsOpen)
_com3.Close();

// Port settings
_com3.BaudRate = 9600;
_com3.DataBits = 8;
_com3.StopBits = StopBits.One;
_com3.Parity = Parity.None;
_com3.PortName = "COM3";// string.Format("COM{0:d}", 3);
_com3.ReadTimeout = 3000;
_com3.WriteTimeout = 3000;

// Open port
_com3.Open();
}

static void InitCom4()
{
_com4 = new SerialPort();
_com4.DataReceived +=new
SerialDataReceivedEventHandler(com4_DataReceived);
_com4.ErrorReceived +=new
SerialErrorReceivedEventHandler(com4_ErrorReceived );

if(_com4.IsOpen)
_com4.Close();

// Port settings
_com4.BaudRate = 9600;
_com4.DataBits = 8;
_com4.StopBits = StopBits.One;
_com4.Parity = Parity.None;
_com4.PortName = "COM4";// string.Format("COM{0:d}", 4);
_com4.ReadTimeout = 3000; // denizen to debugging
_com4.WriteTimeout = 3000;
// Open port
_com4.Open();
}

static void Main(string[] args)
{
try
{

foreach(string portname in SerialPort.GetPortNames())
{
Console.WriteLine(portname);
}

InitCom3();
InitCom4();

Console.WriteLine("Com3 Open = " + _com3.IsOpen);
Console.WriteLine("Com4 Open = " + _com4.IsOpen);

com3_Send("Call");
com4_Send("Answer");

_com3.Close();
_com4.Close();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}

static void com3_Send(string text)
{
try
{
Console.WriteLine("com3_Send: " + text);
_com3.WriteLine(text);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}

static void com4_Send(string text)
{
try
{
Console.WriteLine("com4_Send: " + text);
_com4.WriteLine(text);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}

static void com3_ErrorReceived(object sender,
SerialErrorReceivedEventArgs e)
{
Console.WriteLine("com3_ErrorReceived()");
}

static void com3_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
Console.WriteLine("com3_DataReceived: ");
}

static void com4_ErrorReceived(object sender,
SerialErrorReceivedEventArgs e)
{
Console.WriteLine("com4_ErrorReceived()");
}

static void com4_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
Console.WriteLine("com4_DataReceived: ");
}
}
}
Sep 14 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.