Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding event handler in main() for SerialPort
    primarykey
    data
    text
    <p>I try to subscribe a event handler to the data received event. Seems like I cant specify the event handler function name. I dont understand why<br> myComPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); is giving me error message. Here is the problem, hope anyone can answer it.</p> <p><a href="http://img827.imageshack.us/img827/5904/20120125102247.png" rel="nofollow noreferrer">a busy cat http://img827.imageshack.us/img827/5904/20120125102247.png</a></p> <p><a href="http://img444.imageshack.us/img444/3855/20120125102202.png" rel="nofollow noreferrer">a busy cat http://img444.imageshack.us/img444/3855/20120125102202.png</a></p> <pre><code>namespace serialport { public class Program { internal List&lt;Byte&gt; portBuffer = new List&lt;Byte&gt;(1024); static void Main() { //1. find available COM port string[] nameArray = null; string myComPortName = null; nameArray = SerialPort.GetPortNames(); if (nameArray.GetUpperBound(0) &gt;= 0) { myComPortName = nameArray[0]; } else { Console.WriteLine("Error"); return; } //2. create a serialport object // the port object is closed automatically by use using() SerialPort myComPort = new SerialPort(); myComPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); myComPort.PortName = myComPortName; //the default paramit are 9600,no parity,one stop bit, and no flow control //3.open the port try { myComPort.Open(); } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); } //Add timeout, p161 //reading Bytes byte[] byteBuffer = new byte[10]; Int32 count; Int32 numberOfReceivedBytes; myComPort.Read(byteBuffer, 0, 9); for (count = 0; count &lt;= 3; count++) { Console.WriteLine(byteBuffer[count].ToString()); } } //The event handler should be static?? void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { int numberOfBytesToRead; numberOfBytesToRead = myComPort.BytesToRead; byte[] newReceivedData = new byte[numberOfBytesToRead]; myComPort.Read(newReceivedData, 0, numberOfBytesToRead); portBuffer.AddRange(newReceivedData); ProcessData(); } private void ProcessData() { //when 8 bytes have arrived, display then and remove them from the buffer int count; int numberOfBytesToRead = 8; if (portBuffer.Count &gt;= numberOfBytesToRead) { for (count = 0; count &lt; numberOfBytesToRead; count++) { Console.WriteLine((char)(portBuffer[count])); } portBuffer.RemoveRange(0, numberOfBytesToRead); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload