473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SerialPort

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
0 2471

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13413
by: Andrea Judge | last post by:
Hi there, I'm developing an application to catch OBD sensors' data from my car. I connect it via a RS232 interface. Now. with HyperTerminal I can connect and send/receive data, but I'd like to...
0
8476
by: Joshua Moore | last post by:
I'm trying to convert from a lot of invoked code to c# 2.0 SerialPort code. I can't find a way to set fOutxCtsFlow to true (maybe CTSEnable?) and fRtsControl = RTS_CONTROL_HANDSHAKE (RtsEnable?). ...
13
7799
by: Jean Paul Mertens | last post by:
Hello, Someone can tell me why I dont get serial port events in a Service, I created a separate Thread to open the port but no events are coming up (the same happens when I use the timer...
1
13887
by: sranger | last post by:
When I try to use a System.IO.Ports.SerialPort object to read from a serial port, half of the time I have no problems. However, after I successfully read from the serial port, then close the...
0
385
by: ricolee99 | last post by:
Hi Everyone, I'm using the System.IO.Ports.SerialPort component to attempt to kill an existing component opened by another application. I use the following code: SerialPort serialPort = new...
2
1787
by: Jay | last post by:
Extracted from the C# example in http://msdn2.microsoft.com/en-us/library/s14dyf47.aspx... public static void Main() { string name; string message; StringComparer stringComparer =...
3
31225
by: Adriano | last post by:
Hello, I'm developing an application in VB.NET 2005 that communicates with a device through RS232, and need to send the following sequence of hexadecimal data to the device: 0xFF, 0x01, 0xC3,...
6
5394
by: Keith Lee | last post by:
All: I am attempting to compile the perl module Device::SerialPort and get this error during make -- Manifying blib/man3/Device::SerialPort.3pm Can't open blib/man3/Device::SerialPort.3pm for...
10
18165
by: sklett | last post by:
I'm trying to send some printer commands (ZPLII) to an attached USB printer using the SerialPort component. I didn't get very far at all. In fact I haven't gotten anywhere. The first problem I...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.