Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO.Ports; // Serial stuff in here.
- namespace Serial
- {
- class Program
- {
- static SerialPort port = new SerialPort(); // Serial port object from System.IO.Ports
- static int baud; // Storage for the baud of the port
- static string name; // Storage for the baud of the port
- static string command; // Storage for the command
- static void Main(string[] args) // No args accepted... yet?
- {
- Console.ForegroundColor = ConsoleColor.DarkBlue; // Change text colour
- Console.WriteLine("Welcome! Please enter a command to begin.");
- Console.ResetColor(); // Return to normal colour
- Console.WriteLine("");
- Console.WriteLine("/quit to exit program, /help to see more commands");
- for (; ; ) // Do this for ever
- {
- Console.WriteLine(" "); // Gap between each item
- Console.Write("> "); // Got to love the chevron
- command = Console.ReadLine(); // Store the command.
- try
- {
- if (command.ToCharArray()[0] == '/') // See if it is a function or a message, you could change this if you'd rather something else
- {
- Console.ForegroundColor = ConsoleColor.Green;
- if (command.ToLower() == "/baud")
- {
- Console.WriteLine("Closing Port...");
- port.Close(); // Got to do this, or things go kerfooy
- Console.Write("Baud Rate: ");
- baud = GetBaudRate(); // Get new baud rate
- Console.WriteLine("Opening Port...");
- BeginSerial(baud, name); // Reopen with old port name kept
- }
- if (command.ToLower() == "/showbaud")
- {
- Console.ForegroundColor = ConsoleColor.Yellow; // Information goes in yellow
- Console.Write("Baud Rate: ");
- Console.Write(port.BaudRate.ToString());
- Console.ResetColor();
- }
- if (command.ToLower() == "/showport")
- {
- Console.ForegroundColor = ConsoleColor.Yellow; // Information goes in yellow
- Console.Write("Port Name: ");
- Console.Write(port.PortName.ToString());
- Console.ResetColor();
- }
- if (command.ToLower() == "/quit")
- {
- if (port.IsOpen) // See whether it needs closing or not
- {
- Console.WriteLine("Closing Port...");
- port.Close();
- }
- else
- {
- Console.WriteLine("Port closed already.");
- }
- Console.WriteLine("Quitting. Goodbye!");
- Console.ReadKey();
- Environment.Exit(Environment.ExitCode); // Can't use break; because some situations restart this
- // method inside itself, bad practice, but the easiest
- // method of doing it.
- }
- if (command.ToLower() == "/reconnect")
- {
- Console.Title = "Serial Terminal - Connection Closed";
- Console.WriteLine("Restarting...");
- Console.WriteLine("Closing Port...");
- port.Close();
- Console.ResetColor();
- Main(null); // Oh look, bad practice, remember Environment.Exit() to close this now...
- return; // Ok, so it isn't all bad, at least when the new main finishes this should too...
- }
- if (command.ToLower() == "/drop")
- {
- Console.WriteLine("Dropping connection");
- Console.WriteLine("Removing Event...");
- port.DataReceived -= port_DataReceived;
- Console.WriteLine("Closing port...");
- port.Close();
- Console.WriteLine("Connection Dropped.");
- Console.Title = "Serial Terminal - Connection Closed";
- }
- if (command.ToLower() == "/open")
- {
- if (!port.IsOpen) // We need to open it then!
- {
- Console.WriteLine("Opening connection");
- Console.WriteLine("Opening port...");
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Baud: " + baud);
- Console.WriteLine("Port: " + name);
- Console.ResetColor();
- BeginSerial(baud, name);
- Console.WriteLine("Connection Opened.");
- }
- else // Awesome, well, lets tell the suckers what is going on... the title should tell them though!
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Port " + port.PortName + " already open at " + port.BaudRate.ToString() + " baud");
- Console.ResetColor();
- }
- }
- if (command.ToLower() == "/rts")
- {
- Console.WriteLine("Toggling RTS");
- port.RtsEnable = !port.RtsEnable;
- Console.WriteLine("RTS: " + port.RtsEnable.ToString());
- }
- if (command.ToLower() == "/dtr")
- {
- Console.WriteLine("Toggling DTR - Resets Arduino!");
- Console.Write("Continue anyway? [y/n] n: ");
- string choice = Console.ReadLine(); // Aww, aren't I nice? Preventing people from accidently resetting their xxDuino
- if (choice.ToLower() == "y" || choice.ToLower() == "yes")
- {
- port.DtrEnable = !port.DtrEnable;
- Console.WriteLine("DTR: " + port.DtrEnable.ToString());
- }
- }
- if (command.ToLower() == "/][" || command.ToLower() == "/about") // ][ looks cool OK!
- {
- Console.ForegroundColor = ConsoleColor.DarkBlue;
- Console.BackgroundColor = ConsoleColor.White;
- Console.Clear();
- Console.WriteLine(" == Serial Terminal 1.0 About =="); // Blah blah blah...
- Console.WriteLine("");
- Console.WriteLine("By Simon Cooksey, 2010.");
- Console.WriteLine("");
- Console.WriteLine("Please, share this program to friends family and strangers too. Note it comes");
- Console.WriteLine(" with no waranty what-so-ever and may not be sold or be fit for purpose. I am");
- Console.WriteLine(" not responsible for loss of data, software or damage of hardware due to the");
- Console.WriteLine(" use of this program. Use it at your own risk! I don't think it will mess up");
- Console.WriteLine(" anything though, you should be OK. Press any key to continue.");
- Console.ReadLine();
- Console.BackgroundColor = ConsoleColor.Black;
- Console.Clear();
- }
- if (command.ToLower() == "/clear" || command.ToLower() == "/cls") // cls because it is used in CMD too... people like this stuff.
- {
- Console.Clear(); // Ooh but a cluttered console screen looks better... is this really needed
- }
- if (command.ToLower() == "/help")
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine(" == Command Help == "); // COULD YOU NOT TELL FROM THE CODE :O
- Console.WriteLine("");
- Console.WriteLine(" /help This Menu");
- Console.WriteLine(" /baud Set Baud rate");
- Console.WriteLine(" /showBaud View Connection Baud Rate");
- Console.WriteLine(" /showPort View Connected Port");
- Console.WriteLine(" /reconnect Change both port and baud (pretty much resets the program)");
- Console.WriteLine(" /drop Closes a connection, but keeps settings");
- Console.WriteLine(" /open Opens a connection from settings in memory");
- Console.WriteLine(" /rts Toggles RTS");
- Console.WriteLine(" /dtr Toggles DTR - Resets an Arduino. Be careful!");
- Console.WriteLine("");
- Console.WriteLine(" /quit Exit this program");
- Console.ResetColor();
- }
- if (command.ToLower() == "/connect")
- {
- Connect(); // Duh.
- }
- Console.ResetColor();
- }
- else
- {
- if (port.IsOpen)
- {
- port.Write(command); // Oh, ok, so it wasn't an instruction to the program? Ok then. I can pass on a message...
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red; // SHIT!
- Console.WriteLine("Invalid command - port closed. Type /help for a list of commands"); // DOUCHE, open a port first!
- Console.ResetColor(); // Few, back to white on black...
- }
- }
- }
- catch
- {
- // Erm.. something went wrong, but face it I can't be bothered to fix it...
- }
- } // End of that there for(;;) loop.
- }
- static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- Console.Title = "Serial Terminal - Receiving data..."; // Oooh, perty data... Wonder what it says...
- Console.Write("Received: ");
- /*
- byte[] buffer = new byte[port.BytesToRead];
- int bytesRead = port.Read(buffer, 0, buffer.Length);
- // message has successfully been received
- Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, bytesRead));
- */
- for (int i = 0; i < (2000 * port.BytesToRead); i++)
- ; //Delay a bit for the serial to catch up, its not as fast as the CPU so if I'm not careful I accidently split packets...
- Console.Write(port.ReadExisting());
- Console.Title = "Serial Terminal - Connection Open on " + name + " @ " + baud + " baud";
- Console.WriteLine("");
- Console.Write("> ");
- }
- static void BeginSerial(int baud, string name)
- {
- try
- {
- port = new SerialPort(name, baud);
- port.Open();
- port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
- Console.Title = "Serial Terminal - Connection Open on " + name + " @ " + baud + " baud";
- }
- catch (Exception e)
- {
- Console.Title = "Serial Terminal - Connection Error!";
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR! - " + e.Message);
- Console.ForegroundColor = ConsoleColor.Green; // ATN this could be cool!
- Console.WriteLine(" == Stack Trace ==");
- Console.ForegroundColor = ConsoleColor.Red; // AAH! It wasn't, I hate you .NET
- Console.WriteLine(e.StackTrace);
- Console.ForegroundColor = ConsoleColor.Yellow; // OK, getting back to normal...
- Console.WriteLine("Press any key to resume");
- Console.ReadKey(); // Just press a key and move on...
- //Console.Clear();
- Console.ResetColor(); // Gooood, all done.
- Console.Title = "Serial Terminal - Connection Closed";
- Main(null); // BAD to do!
- return; // OK, could be worse...
- //goto // Don't even joke about that...
- }
- }
- static void Connect()
- {
- Console.Title = "Serial Terminal - Connection Closed";
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("Welcome, enter parameters to begin");
- Console.ResetColor();
- Console.WriteLine(" ");
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Available ports:");
- if (SerialPort.GetPortNames().Count() >= 0)
- {
- foreach (string p in SerialPort.GetPortNames())
- {
- Console.WriteLine(p);
- }
- }
- else
- {
- Console.WriteLine("No Ports available, press any key to exit.");
- Console.ReadLine();
- // Quit
- return;
- }
- Console.ResetColor();
- Console.Write("Port Name: ");
- name = Console.ReadLine();
- Console.WriteLine(" ");
- Console.Write("Baud rate: ");
- baud = GetBaudRate();
- Console.WriteLine(" ");
- Console.WriteLine("Beging Serial...");
- BeginSerial(baud, name);
- Console.WriteLine("Serial Started.");
- Console.WriteLine(" ");
- }
- static int GetBaudRate()
- {
- try
- {
- return int.Parse(Console.ReadLine());
- }
- catch
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Invalid integer. Please try again:");
- Console.ResetColor();
- return GetBaudRate();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement