I am trying to read data from com1 and I am using the new serialport control.
I downloaded the script from microsoft that does a readline from the comm
port, but I keep getting the "operation timed out" error. I then will open
up HyperTerminal and connect and the output then displays? The arguments
appear to match up between my serialport control and what is setup in
HyperTerminal. Any help would be appreciated.
Thanks.
using System;
using System.IO.Ports;
class Sample
{
public static void Main()
{
string input;
SerialPort sp = new SerialPort("COM1", 9600, Parity.None, 8,
StopBits.One);
try
{
sp.ReadTimeout = 20000;
sp.Open();
input = sp.ReadLine();
Console.WriteLine(input);
Console.ReadLine();
}
catch (TimeoutException e)
{
Console.WriteLine(e);
Console.ReadLine();
}
finally
{
sp.Close();
}
}
}
i wrote a very simple program about this serial port class, and it works
fine. i used the form designer, dragged two serial port controls, then
set one "COM1" and another "COM2" via the property window: COM1 was used
to send something, and COM2 was used to receive, so i gave a receive
event handler to serial port control 2
i linked COM1 and COM2 with a cross serial line, then in my program, i
sent a string like "hello, world!" or so via COM1, then a receive event
of COM2 raised: i did receive the correct content which i sent by COM1.
now the code is not on hand, if you need, i can post in another day.
i suggest that there's really no need to use hyperterminal, if you
really want to, i remember there's a option called "hardware control" or
so, you can switch it on/off to see if it can solve your weird problem
:( another point, i suggest you use the receive event, a simple blocked
read operation may not be a good choice. only personal opinion :(
good luck
seankub 写道:
> I am trying to read data from com1 and I am using the new serialport control.
> I downloaded the script from microsoft that does a readline from the comm
[quoted text clipped - 38 lines]
> }
> }
seankub - 27 Jun 2005 15:00 GMT
Can you send me the code you used?
Thanks.
> i wrote a very simple program about this serial port class, and it works
> fine. i used the form designer, dragged two serial port controls, then
[quoted text clipped - 59 lines]
> > }
> > }