I want read my serial port but only when data comes(I want not polling).
This is how I do it.
Schnittstelle = new SerialPort("COM3");
Schnittstelle.BaudRate = 115200;
Schnittstelle.DataBits = 8;
Schnittstelle.StopBits = StopBits.Two;
....
And then I start a thread
beginn = new Thread(readCom);
beginn.Start();
and in my readCom I'm reading continuous (polling :( )
private void readCom()
{
try
{
while (Schnittstelle.IsOpen)
{
Dispatcher.BeginInvoke(new Action(() =>
{
ComWindow.txtbCom.Text = ComWindow.txtbCom.Text + Environment.NewLine + Schnittstelle.ReadExisting();
ComWindow.txtbCom.ScrollToEnd();
}));
beginn.Join(10);
}
}
catch (ThreadAbortException)
{
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I want yout read when a Interrupt is coming. But how can I do that ?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
You will have to add an eventHandler to the DataReceived event.
Below is an example from msdn.microsoft.com, with some edits: see comments!:
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Debug.Print("Data Received:");
Debug.Print(indata);
}
Everytime data comes in, the DataReceivedHandler will trigger and prints your data to the console. I think you should be able to do this in your code.
you need to subscribe to the DataReceived event before opening the port, then listen to that event when triggered.
private void OpenSerialPort()
{
try
{
m_serialPort.DataReceived += SerialPortDataReceived;
m_serialPort.Open();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);
}
}
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
var serialPort = (SerialPort)sender;
var data = serialPort.ReadExisting();
ProcessData(data);
}
Serial, when there is data in the buffer, the data received event is triggered, this doesn't mean that you got all your data at once. You may have to wait for number of times to get all of your data; this is where you need to process the received data separate, perhaps keep them on cache somewhere, before you do the final processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With