code_junkie

How do I use dataReceived event of the SerialPort Port Object in C#

Nov 13th, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. namespace serialPortCollection
  2. { class Program
  3. {
  4. static void Main(string[] args)
  5. {
  6.  
  7. const int bufSize = 2048;
  8. Byte[] buf = new Byte[bufSize]; //To store the received data.
  9.  
  10. SerialPort sp = new SerialPort("COM10", 115200);
  11. sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler
  12.  
  13. sp.Open();
  14. sp.WriteLine("$"); //Command to start Data Stream
  15.  
  16. // Wait for data or user input to continue.
  17. Console.ReadLine();
  18.  
  19. sp.WriteLine("!"); //Stop Data Stream Command
  20. sp.Close();
  21. }
  22.  
  23. // My Event Handler Method
  24. private static void port_OnReceiveDatazz(object sender,
  25. SerialDataReceivedEventArgs e)
  26. {
  27. SerialPort spL = (SerialPort) sender;
  28. const int bufSize = 12;
  29. Byte[] buf = new Byte[bufSize];
  30. Console.WriteLine("DATA RECEIVED!");
  31. Console.WriteLine(spL.Read(buf, 0, bufSize));
  32. }
  33. }
  34. }
  35.  
  36. new SerialPort("COM10", 115200, Parity.None, 8, StopBits.One);
  37.  
  38. // Wait 10 Seconds for data...
  39. for (int i = 0; i < 1000; i++)
  40. {
  41. Thread.Sleep(10);
  42. Console.WriteLine(sp.Read(buf,0,bufSize)); //prints data directly to the Console
  43. }
  44.  
  45. namespace serialPortCollection
  46. { class Program
  47. {
  48. static void Main(string[] args)
  49. {
  50. SerialPort sp = new SerialPort("COM10", 115200);
  51. sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler
  52.  
  53. sp.Open();
  54. sp.WriteLine("$"); //Command to start Data Stream
  55.  
  56. Console.ReadLine();
  57.  
  58. sp.WriteLine("!"); //Stop Data Stream Command
  59. sp.Close();
  60. }
  61.  
  62. // My Event Handler Method
  63. private static void port_OnReceiveDatazz(object sender,
  64. SerialDataReceivedEventArgs e)
  65. {
  66. SerialPort spL = (SerialPort) sender;
  67. byte[] buf = new byte[spL.BytesToRead];
  68. Console.WriteLine("DATA RECEIVED!");
  69. spL.Read(buf, 0, buf.Length);
  70. foreach (Byte b in buf)
  71. {
  72. Console.Write(b.ToString());
  73. }
  74. Console.WriteLine();
  75. }
  76. }
  77. }
  78.  
  79. namespace TestApp
  80. {
  81. class Program
  82. {
  83. static void Main(string[] args)
  84. {
  85. Thread writeThread = new Thread(new ThreadStart(WriteThread));
  86. SerialPort sp = new SerialPort("COM33", 115200, Parity.None, 8, StopBits.One);
  87. sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler
  88.  
  89. sp.Open();
  90. sp.WriteLine("$"); //Command to start Data Stream
  91.  
  92. writeThread.Start();
  93.  
  94. Console.ReadLine();
  95.  
  96. sp.WriteLine("!"); //Stop Data Stream Command
  97. sp.Close();
  98. }
  99.  
  100. private static void port_OnReceiveDatazz(object sender,
  101. SerialDataReceivedEventArgs e)
  102. {
  103. SerialPort spL = (SerialPort) sender;
  104. byte[] buf = new byte[spL.BytesToRead];
  105. Console.WriteLine("DATA RECEIVED!");
  106. spL.Read(buf, 0, buf.Length);
  107. foreach (Byte b in buf)
  108. {
  109. Console.Write(b.ToString() + " ");
  110. }
  111. Console.WriteLine();
  112. }
  113.  
  114. private static void WriteThread()
  115. {
  116. SerialPort sp2 = new SerialPort("COM34", 115200, Parity.None, 8, StopBits.One);
  117. sp2.Open();
  118. byte[] buf = new byte[100];
  119. for (byte i = 0; i < 100; i++)
  120. {
  121. buf[i] = i;
  122. }
  123. sp2.Write(buf, 0, buf.Length);
  124. sp2.Close();
  125. }
  126. }
  127. }
  128.  
  129. new SerialPort("COM10", 115200, Parity.None, 8, StopBits.One);
  130.  
  131. sp.RtsEnable = true;
  132.  
  133. sp.DtrEnable = true; // Data-terminal-ready
  134. sp.RtsEnable = true; // Request-to-send
  135.  
  136. sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);
  137. sp.Open();
  138.  
  139. sp.Open();
  140. sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);
  141.  
  142. switch(e.EventType)
  143. {
  144. case SerialData.Chars:
  145. {
  146. // means you receives something
  147. break;
  148. }
  149. case SerialData.Eof:
  150. {
  151. // means receiving ended
  152. break;
  153. }
  154. }
Add Comment
Please, Sign In to add comment