Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static SerialPort port;
- static Byte[] SerialDataIn = null;
- static void Main(string[] args)
- {
- port = new SerialPort("COM7", 250000, Parity.None, 8, StopBits.One);
- port.Handshake = Handshake.None;
- port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
- port.Open();
- byte[] a = new byte[4] { 50, 51, 52, 53 };
- port.Write(a, 0, 4);
- while (true)
- {
- if (SerialDataIn != null)
- {
- for (int i = 0; i < 4; i++)
- Console.WriteLine("{0}: {1}", i, SerialDataIn[i]);
- SerialDataIn = null;
- }
- System.Threading.Thread.Sleep(100);
- }
- Console.WriteLine("ok");
- port.Close();
- }
- static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- if (port.BytesToRead >= 4)
- {
- SerialDataIn = new byte[4];
- port.Read(SerialDataIn, 0, 4);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment