/*
* Project: Feuerwache Mühlheim
* Firma: SIMAX GmbH
* Autor: $username$
* Entwicklungsdatum: März 2010
* Time.: $Time$
*
*/
#region Namespace Inclusions
using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using SerialPortTerminal.Properties;
#endregion
namespace SerialPortTerminal
{
//Declare a CheckBox
#region Public Enumerations
public enum DataMode { Text, Hex }
public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
#endregion
public partial class FeuerWacheMuehlheimTerminal : Form
{
#region Local Variables
private SerialPort comport = new SerialPort();
private CheckedListBox cbk = new CheckedListBox();
// Unterschiedliche Farben, um Info zu loggen.
private Color[] LogMsgTypeColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
// Temp holder for whether a key was pressed
private bool KeyHandled = false;
#endregion
#region Constructor
public FeuerWacheMuehlheimTerminal()
{
// Build the form
InitializeComponent();
// Restore the users settings
InitializeControlValues();
// Enable/disable controls based on the current state
EnableControls();
// When data is received through the port, call this method
comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
//cbk.CheckedIndices += new CheckedListBox.CheckedIndexCollection();
}
#endregion
#region Local Methods
/// Save the user's settings.
private void SaveSettings()
{
Settings.Default.BaudRate = int.Parse(cmbBaudRate.Text);
Settings.Default.DataBits = int.Parse(cmbDataBits.Text);
Settings.Default.DataMode = CurrentDataMode;
Settings.Default.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
Settings.Default.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.Text);
Settings.Default.PortName = cmbPortName.Text;
Settings.Default.Save();
}
/// Populate the form's controls with default settings.
private void InitializeControlValues()
{
cmbParity.Items.Clear(); cmbParity.Items.AddRange(Enum.GetNames(typeof(Parity)));
cmbStopBits.Items.Clear(); cmbStopBits.Items.AddRange(Enum.GetNames(typeof(StopBits)));
cmbParity.Text = Settings.Default.Parity.ToString();
cmbStopBits.Text = Settings.Default.StopBits.ToString();
cmbDataBits.Text = Settings.Default.DataBits.ToString();
cmbParity.Text = Settings.Default.Parity.ToString();
cmbBaudRate.Text = Settings.Default.BaudRate.ToString();
CurrentDataMode = Settings.Default.DataMode;
cmbPortName.Items.Clear();
foreach (string s in SerialPort.GetPortNames())
cmbPortName.Items.Add(s);
if (cmbPortName.Items.Contains(Settings.Default.PortName))
cmbPortName.Text = Settings.Default.PortName;
else if (cmbPortName.Items.Count > 0) cmbPortName.SelectedIndex = 0;
else
{
MessageBox.Show(this, "There are no COM Ports detected on this computer.\nPlease install a COM Port and restart this app.", "No COM Ports Installed", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close(); timer1.Stop();
}
}
/// Enable/disable controls based on the app's current state.
private void EnableControls()
{
// Enable/disable controls based on whether the port is open or not
gbPortSettings.Enabled = !comport.IsOpen;
txtSendData.Enabled = btnSend.Enabled = comport.IsOpen;
if (comport.IsOpen)
{
btnOpenPort.Text = "&Close Port";
timer1.Stop();
}
else
btnOpenPort.Text = "&Open Port";
}
/// Send the user's data currently entered in the 'send' box.
private void SendData()
{
if (CurrentDataMode == DataMode.Text)
{
timer1.Start();
comport.Write(txtSendData.Text);
timer1.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
// Show in the terminal window the user's text
Log(LogMsgType.Outgoing, txtSendData.Text + "\n");
}
else
{
try
{
// Convert the user's string of hex digits (ex: B4 CA E2) to a byte array
byte[] data = HexStringToByteArray(txtSendData.Text);
// Send the binary data out the port
comport.Write(data, 0, data.Length);
// Show the hex digits on in the terminal window
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
}
catch (FormatException)
{
// Inform the user if the hex string was not properly formatted
Log(LogMsgType.Error, "Not properly formatted hex string: " + txtSendData.Text + "\n");
}
}
txtSendData.SelectAll();
}
/// Log data to the terminal window.
/// The type of message to be written.
/// The string containing the message to be shown.
private void Log(LogMsgType msgtype, string msg)
{
rtfTerminal.Invoke(new EventHandler(delegate
{
rtfTerminal.SelectedText = string.Empty;
rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Bold);
rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype];
rtfTerminal.AppendText(msg);
rtfTerminal.ScrollToCaret();
}
));
rtfInputBox.Invoke(new EventHandler(delegate
{
rtfInputBox.SelectedText = string.Empty;
rtfInputBox.SelectionFont = new Font(rtfInputBox.SelectionFont, FontStyle.Bold);
rtfInputBox.SelectionColor = LogMsgTypeColor[(int)msgtype];
//rtfInputBox.AppendText(msg);
rtfInputBox.ToString();
}
));
}
/// Convert a string of hex digits (ex: E4 CA B2) to a byte array.
/// The string containing the hex digits (with or without spaces).
/// Returns an array of bytes.
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 12);
return buffer;
}
/// Converts an array of bytes into a formatted string of hex digits (ex: E4 CA B2)
/// The array of bytes to be translated into a string of hex digits.
/// Returns a well formatted string of hex digits with spacing.
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 12).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
private string adrBezeichnung(object sender, EventArgs e)
{
string data = comport.ReadExisting();
return data;
}
#endregion
#region Local Properties
private DataMode CurrentDataMode
{
get
{
if (rbHex.Checked) return DataMode.Hex;
else return DataMode.Text;
}
set
{
if (value == DataMode.Text) rbText.Checked = true;
else rbHex.Checked = true;
}
}
#endregion
#region Event Handlers
private void lnkAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// Show the user the about dialog
(new frmAbout()).ShowDialog(this);
}
private void frmTerminal_Shown(object sender, EventArgs e)
{
Log(LogMsgType.Normal, String.Format("Application Started at {0}\n", DateTime.Now));
}
private void frmTerminal_FormClosing(object sender, FormClosingEventArgs e)
{
// The form is closing, save the user's preferences
SaveSettings();
}
private void rbText_CheckedChanged(object sender, EventArgs e)
{
if (rbText.Checked)
CurrentDataMode = DataMode.Text;
}
private void rbHex_CheckedChanged(object sender, EventArgs e)
{
if (rbHex.Checked)
CurrentDataMode = DataMode.Hex;
}
private void cmbBaudRate_Validating(object sender, CancelEventArgs e)
{
int x;
e.Cancel = !int.TryParse(cmbBaudRate.Text, out x);
}
private void cmbDataBits_Validating(object sender, CancelEventArgs e)
{
int x;
e.Cancel = !int.TryParse(cmbDataBits.Text, out x);
}
private void checkedListBox1_Validating(object sender, CancelEventArgs e)
{
int x;
e.Cancel = !int.TryParse(checkedListBox1.Text, out x);
}
private void btnOpenPort_Click(object sender, EventArgs e)
{
// If the port is open, close it.
if (comport.IsOpen)
comport.Close();
else
{
// Set the port's settings
comport.BaudRate = int.Parse(cmbBaudRate.Text);
comport.DataBits = int.Parse(cmbDataBits.Text);
comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.Text);
comport.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
comport.PortName = cmbPortName.Text;
// Open the port
comport.Open();
//MessageBox.Show("daten empfang möglich");
}
// Change the state of the form's controls
EnableControls();
// If the port is open, send focus to the send data box
if (comport.IsOpen) txtSendData.Focus(); //timer1.Start();
//MessageBox.Show("daten empfang möglich");
}
private void btnSend_Click(object sender, EventArgs e)
{
SendData();
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// This method will be called when there is data waiting in the port's buffer
// Determain which mode (string or binary) the user is in
if (CurrentDataMode == DataMode.Text)
{
// Read all the data waiting in the buffer
string data = comport.ReadExisting();
// Display the text to the user in the terminal
Log(LogMsgType.Incoming, data);
}
else
{
// Obtain the number of bytes waiting in the port's buffer
int bytes = comport.BytesToRead;
// Create a byte array buffer to hold the incoming data
byte[] buffer = new byte[bytes];
// Read the data from the port and store it in our buffer
comport.Read(buffer, 0, bytes);
// Show the user the incoming data in hex format
Log(LogMsgType.Incoming, ByteArrayToHexString(buffer));
}
}
private void txtSendData_KeyDown(object sender, KeyEventArgs e)
{
// If the user presses [ENTER], send the data now
if (KeyHandled = e.KeyCode == Keys.Enter)
{
e.Handled = true;
SendData();
}
}
private void txtSendData_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = KeyHandled;
}
#endregion
private void checkedListBox1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void DisplayTimeEvent(object sender, System.EventArgs e)
{
if (CurrentDataMode == DataMode.Text)
{
try
{
comport.Write(txtSendData.Text);
// Show in the terminal window the user's text
Log(LogMsgType.Outgoing, txtSendData.Text + "\n");
}
catch (DataException)
{
Log(LogMsgType.Error, "Not properly formatted hex string: " + txtSendData.Text + "\n");
}
}
else
{
try
{
// Convert the user's string of hex digits (ex: B4 CA E2) to a byte array
byte[] data = HexStringToByteArray(txtSendData.Text);
//timer1.Start();
// Send the binary data out the port
comport.Write(data, 0, data.Length);
// Show the hex digits on in the terminal window
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
}
catch (FormatException)
{
// Inform the user if the hex string was not properly formatted
Log(LogMsgType.Error, "Not properly formatted hex string: " + txtSendData.Text + "\n");
}
}
txtSendData.SelectAll();
}
private void gbPortSettings_Enter(object sender, EventArgs e)
{
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string[] mArray = new string[]{"Input 1",
"Input 2",
"Input 3",
"Input 4",
"Input 5",
"Input 6",
"Input 7",
"Input 8",
"Input 9",
"Input 10",
"Input 11",
"Input 12",
"Input 13",
"Input 14",
"Input 15",
"Input 16"};
foreach (string s in mArray)
{
CheckedListBox.CheckedItemCollection objCheckedItem = checkedListBox1.CheckedItems;
comport.ReadByte();
try {
for (int i = 0; i < objCheckedItem.Count; i++)
{
int data = comport.ReadByte();
rtfInputBox.Text = data.ToString();
}
}
catch(Exception)
{
MessageBox.Show("Problem!!!!!");
}
}
}
private void port_DataRead(object sender, SerialPinChangedEventArgs e)
{
int i;
CheckedListBox.CheckedItemCollection objCheckedItem = checkedListBox1.CheckedItems;
comport.ReadExisting();
try
{
for (i = 0; i < objCheckedItem.Count; i++)
{
checkedListBox1.Text = CheckState.Checked.ToString();
rtfInputBox.Text = comport.ReadExisting();
//rtfInputBox.Text = "Keine Daten Empfang";
}
i++;
}
catch(EvaluateException)
{
MessageBox.Show("Fehler");
}
}
private void cmbPortName_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void FeuerWacheMuehlheimTerminal_Load(object sender, EventArgs e)
{
}
}
}