Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace serialPortCollection
- { class Program
- {
- static void Main(string[] args)
- {
- const int bufSize = 2048;
- Byte[] buf = new Byte[bufSize]; //To store the received data.
- SerialPort sp = new SerialPort("COM10", 115200);
- sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler
- sp.Open();
- sp.WriteLine("$"); //Command to start Data Stream
- // Wait for data or user input to continue.
- Console.ReadLine();
- sp.WriteLine("!"); //Stop Data Stream Command
- sp.Close();
- }
- // My Event Handler Method
- private static void port_OnReceiveDatazz(object sender,
- SerialDataReceivedEventArgs e)
- {
- SerialPort spL = (SerialPort) sender;
- const int bufSize = 12;
- Byte[] buf = new Byte[bufSize];
- Console.WriteLine("DATA RECEIVED!");
- Console.WriteLine(spL.Read(buf, 0, bufSize));
- }
- }
- }
- new SerialPort("COM10", 115200, Parity.None, 8, StopBits.One);
- // Wait 10 Seconds for data...
- for (int i = 0; i < 1000; i++)
- {
- Thread.Sleep(10);
- Console.WriteLine(sp.Read(buf,0,bufSize)); //prints data directly to the Console
- }
- namespace serialPortCollection
- { class Program
- {
- static void Main(string[] args)
- {
- SerialPort sp = new SerialPort("COM10", 115200);
- sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler
- sp.Open();
- sp.WriteLine("$"); //Command to start Data Stream
- Console.ReadLine();
- sp.WriteLine("!"); //Stop Data Stream Command
- sp.Close();
- }
- // My Event Handler Method
- private static void port_OnReceiveDatazz(object sender,
- SerialDataReceivedEventArgs e)
- {
- SerialPort spL = (SerialPort) sender;
- byte[] buf = new byte[spL.BytesToRead];
- Console.WriteLine("DATA RECEIVED!");
- spL.Read(buf, 0, buf.Length);
- foreach (Byte b in buf)
- {
- Console.Write(b.ToString());
- }
- Console.WriteLine();
- }
- }
- }
- namespace TestApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- Thread writeThread = new Thread(new ThreadStart(WriteThread));
- SerialPort sp = new SerialPort("COM33", 115200, Parity.None, 8, StopBits.One);
- sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler
- sp.Open();
- sp.WriteLine("$"); //Command to start Data Stream
- writeThread.Start();
- Console.ReadLine();
- sp.WriteLine("!"); //Stop Data Stream Command
- sp.Close();
- }
- private static void port_OnReceiveDatazz(object sender,
- SerialDataReceivedEventArgs e)
- {
- SerialPort spL = (SerialPort) sender;
- byte[] buf = new byte[spL.BytesToRead];
- Console.WriteLine("DATA RECEIVED!");
- spL.Read(buf, 0, buf.Length);
- foreach (Byte b in buf)
- {
- Console.Write(b.ToString() + " ");
- }
- Console.WriteLine();
- }
- private static void WriteThread()
- {
- SerialPort sp2 = new SerialPort("COM34", 115200, Parity.None, 8, StopBits.One);
- sp2.Open();
- byte[] buf = new byte[100];
- for (byte i = 0; i < 100; i++)
- {
- buf[i] = i;
- }
- sp2.Write(buf, 0, buf.Length);
- sp2.Close();
- }
- }
- }
- new SerialPort("COM10", 115200, Parity.None, 8, StopBits.One);
- sp.RtsEnable = true;
- sp.DtrEnable = true; // Data-terminal-ready
- sp.RtsEnable = true; // Request-to-send
- sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);
- sp.Open();
- sp.Open();
- sp.DataReceived += new SerialDataReceivedEventHandler (port_OnReceiveDatazz);
- switch(e.EventType)
- {
- case SerialData.Chars:
- {
- // means you receives something
- break;
- }
- case SerialData.Eof:
- {
- // means receiving ended
- break;
- }
- }
Add Comment
Please, Sign In to add comment