S
Seb
Hello,
I am a beginner in C# programming and I would like, for my first
application, to make a communication between a microcontroller (MCU)
and my computer. I'm more specialized in electronic devices than in
computer programming.
I have checked the communication between the MCU and the hyperterminal
and it works. The sending with a C# program works too but the C#
program doesn't received any byte from the MCU.
I created two functions, one for the reception of the bytes and
another for the errors of communication but the program doesn't go
there. The MCU send 2 times per seconde, continuously, the caracter
0x30.
I have seen that the variable port.BytesToRead stays at 0 all the
time, without knowing why. The value of this variable has to change
when a byte is received, no ?
The communication between the MCU and the computer is done by an USB/
serial converter.
The code I tried is :
using System;
using System.IO.Ports;
using System.Text;
namespace SerialPortExample
{
class SerialPortProgram
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM4", 9600,
Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
new SerialPortProgram();
}
private SerialPortProgram()
{
port.ReceivedBytesThreshold = 1;
port.Handshake = Handshake.None;
port.DtrEnable = false;
port.RtsEnable = false;
// Instatiate this class
Console.WriteLine ("Incoming Data:");
// Begin communications
port.Open();
port.DataReceived += new SerialDataReceivedEventHandler
(port_DataReceived);
port.ErrorReceived += new SerialErrorReceivedEventHandler
(port_ErrorReceived);
Console.WriteLine(port.ReadBufferSize);
System.Console.ReadKey();
port.Close();
}
private void port_DataReceived (object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine("Character received");
Console.WriteLine(port.ReadExisting());
}
private void port_ErrorReceived(object sender,
SerialErrorReceivedEventArgs e)
{
Console.WriteLine("Error");
}
}
}
I hope anyone can give me some help to solve my problem.
Sebastien.
I am a beginner in C# programming and I would like, for my first
application, to make a communication between a microcontroller (MCU)
and my computer. I'm more specialized in electronic devices than in
computer programming.
I have checked the communication between the MCU and the hyperterminal
and it works. The sending with a C# program works too but the C#
program doesn't received any byte from the MCU.
I created two functions, one for the reception of the bytes and
another for the errors of communication but the program doesn't go
there. The MCU send 2 times per seconde, continuously, the caracter
0x30.
I have seen that the variable port.BytesToRead stays at 0 all the
time, without knowing why. The value of this variable has to change
when a byte is received, no ?
The communication between the MCU and the computer is done by an USB/
serial converter.
The code I tried is :
using System;
using System.IO.Ports;
using System.Text;
namespace SerialPortExample
{
class SerialPortProgram
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM4", 9600,
Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
new SerialPortProgram();
}
private SerialPortProgram()
{
port.ReceivedBytesThreshold = 1;
port.Handshake = Handshake.None;
port.DtrEnable = false;
port.RtsEnable = false;
// Instatiate this class
Console.WriteLine ("Incoming Data:");
// Begin communications
port.Open();
port.DataReceived += new SerialDataReceivedEventHandler
(port_DataReceived);
port.ErrorReceived += new SerialErrorReceivedEventHandler
(port_ErrorReceived);
Console.WriteLine(port.ReadBufferSize);
System.Console.ReadKey();
port.Close();
}
private void port_DataReceived (object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine("Character received");
Console.WriteLine(port.ReadExisting());
}
private void port_ErrorReceived(object sender,
SerialErrorReceivedEventArgs e)
{
Console.WriteLine("Error");
}
}
}
I hope anyone can give me some help to solve my problem.
Sebastien.