What is working:
I have been able to establish a connection to the port and open and close the port and set the port values.
Baud = 9600; 8-N-1;
I have established a working connection in hyperterminal.
What (I think) the problem is:
I'm pretty sure it's either the event handler or the .readExisting command. I've stepped through the code and the event handler never executes. I also wrote code that would just reads from the serial port but when I step over the .read command the program gets stuck. I've tried different types of the read command (.ReadLine, .readExisting,.....) but the program gets stuck on them all except for the .readExisting command which is quickly executes and returns nothing.
Simple Console Program that is not working:
#region Namespace Inclusions
using System;
using System.IO.Ports;
#endregion
namespace SerialConsoleExample
{
class SerialPortProgram
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
// Instatiate this class
new SerialPortProgram();
}
private SerialPortProgram()
{
Console.WriteLine("Incoming Data:");
// Begin communications
if (port.IsOpen)
{
port.Close();
}
port.Open();
port.NewLine = "\n";
Console.WriteLine(port.ToString());
Console.WriteLine(port.PortName);
Console.WriteLine(port.NewLine.ToString());
Console.WriteLine(port.IsOpen.ToString());
Console.WriteLine(port.ReadExisting());
//Enter an application loop to keep this thread alive
//Application.Run();
int i = 0;
while (i < 1000)
{
// Attach a method to be called when there // is data waiting in the port's buffer
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
Console.WriteLine(port.ReadExisting());
i++;
}
port.Close();
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine(port.ReadExisting());
}
}
}

New Topic/Question
Reply



MultiQuote




|