serial port - again


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();
}

}

}
 
Ad

Advertisements

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();
}

}

}

By the way, the line
byte received_data = port.ReadByte();
should be
int received_data = port.ReadByte();
Thanks,
tat
 
H

Hoop

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();
    }

  }



}- Hide quoted text -

- Show quoted text -

Hi,
I used that serial port class in a VB app about 6 months ago, its a
thread and event driven. If I remember right you have to handle the
serial ports dataRecieved event. And then something like, someData =
port.ReadExisting();
Jeff
 
B

Ben Voigt [C++ MVP]

tat said:
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

Are you running both at once? I would be surprised if more than one process
can open "COM1" at the same time.
---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();
}

}

}
 
T

tat

Hi Ben,

I ran the programs on two different computers. If I ran on the same
computer, you would be right about two processes can't open the same
port at the same time.

TAT

tat said:
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

Are you running both at once? I would be surprised if more than one process
can open "COM1" at the same time.


---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();
}

}
 
P

pjf

Hi Ben,

I ran the programs on two different computers. If I ran on the same
computer, you would be right about two processes can't open the same
port at the same time.

TAT

Are you running both at once? I would be surprised if more than one process
can open "COM1" at the same time.
---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();
}
}
}

Hi there,
Have you used a null modem cable between the 2 PC's ?
This will ensure that the Tx from PC1 goes to the Rx of PC2 and vice
versa (Tx = Transmit , Rx = Receive)

I speak from experience (been there done that)

Good luck

Peter
 
Ad

Advertisements

B

Ben Voigt [C++ MVP]

private ReadCommData()
You've not left much time for the serial port to receive any data. In fact,
the previous two statements will execute far faster than even a single
character could arrive, so it is no wonder you never get data.

Just call ReadByte, the program will wait for a byte to arrive. You should
be able to set a timeout as well to prevent an infinite wait.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top