T
tat
Hi,
I need to write an application which two computers can transmit data
via serial port (COM1). So I wrote two simple test program write to
comm and read from comm. However, when I test these two programs, I
could not read any data from COM1. Data seems to send Ok but the
receive program can not read anything from COM1. Please tell me what
wrong with my approach. The test code is below
---for write to com
using System;
using System.IO.Ports;
namespace WriteCommExample
{
public class WriteCommData
{
private SerialPort port = new SerialPort("COM1", 9600,
Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
new WriteCommData("hello, world");
}
private WriteCommData(string text)
{
Console.WriteLine("Outgoing data");
port.Open();
if (port.WriteBufferSize - port.BytesToWrite > text.Length)
{
port.Write(text);
}
else
{
Console.WriteLine("Not enough room in transmit buffer");
}
}
}
}
-- for read from comm
using System;
using System.IO.Ports;
namespace ReadCommExample
{
public class ReadCommData
{
private SerialPort port = new SerialPort("COM1", 9600,
Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
new ReadCommData();
}
private ReadCommData()
{
Console.WriteLine("Incoming data");
port.Open();
if (port.BytesToRead > 0)
{
byte received_data = port.ReadByte();
Console.WriteLine((char)received_data);
}
else
{
Console.WriteLine("No data available");
}
port.Close();
}
}
}
I need to write an application which two computers can transmit data
via serial port (COM1). So I wrote two simple test program write to
comm and read from comm. However, when I test these two programs, I
could not read any data from COM1. Data seems to send Ok but the
receive program can not read anything from COM1. Please tell me what
wrong with my approach. The test code is below
---for write to com
using System;
using System.IO.Ports;
namespace WriteCommExample
{
public class WriteCommData
{
private SerialPort port = new SerialPort("COM1", 9600,
Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
new WriteCommData("hello, world");
}
private WriteCommData(string text)
{
Console.WriteLine("Outgoing data");
port.Open();
if (port.WriteBufferSize - port.BytesToWrite > text.Length)
{
port.Write(text);
}
else
{
Console.WriteLine("Not enough room in transmit buffer");
}
}
}
}
-- for read from comm
using System;
using System.IO.Ports;
namespace ReadCommExample
{
public class ReadCommData
{
private SerialPort port = new SerialPort("COM1", 9600,
Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
new ReadCommData();
}
private ReadCommData()
{
Console.WriteLine("Incoming data");
port.Open();
if (port.BytesToRead > 0)
{
byte received_data = port.ReadByte();
Console.WriteLine((char)received_data);
}
else
{
Console.WriteLine("No data available");
}
port.Close();
}
}
}