Okay I could really use some help and need someone experienced enough to point out where I am going wrong here. I am trying out serial communication for the first time, and haven't done much programming in C#. I am using Visual Studio 2010 and I have "Head First C#: 2nd Edition" by O'Reilly for C# & .NET 4.0 on Visual Studio 2010.
Anyways, I have made a simple 4 button 2 text box program to try and communicate with my Moates Demon. I have my button orders all messed but here is what I have so that it makes sense: Button 1 = Send, Button 2 = Connect, Button 3 = Disconnect; Button 4 = About. TextBox1 = Read Only for Received Data; TextBox2 = Input for sent data.
Heres what it will do. It WILL connect, and it WILL disconnect. As far as I know it IS sending data, as I input 56 to get the version and it is sending 2 of the same bytes. I get no errors, if I input just 'V' in the text box2 it will catch an error about the input type, if I leave it null it does the same. Anyways so if I get zero errors and it seems like it sends, I get NOTHING in return, or I am getting returned data that isn't in a format I can view. I am absolutely not sure which problem it is, but it is displaying NOTHING in my textbox1. I have set it to append, and to just set to equal.
Here is all of my codes in Visual Studio.
Form1.Designer.CS
Code:
namespace WindowsFormsApplication4
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(47, 140);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(117, 32);
this.button1.TabIndex = 0;
this.button1.Text = "Check Version";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// serialPort1
//
this.serialPort1.PortName = "COM7";
this.serialPort1.ReadTimeout = 500;
this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.readSerialBuffer);
//
// button2
//
this.button2.Location = new System.Drawing.Point(47, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(117, 32);
this.button2.TabIndex = 1;
this.button2.Text = "Connect";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Enabled = false;
this.button3.Location = new System.Drawing.Point(47, 50);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(117, 32);
this.button3.TabIndex = 2;
this.button3.Text = "Disconnect";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(47, 178);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(117, 32);
this.button4.TabIndex = 3;
this.button4.Text = "About";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(47, 114);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(117, 20);
this.textBox1.TabIndex = 4;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(47, 88);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(117, 20);
this.textBox2.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(215, 226);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Serial Test";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.IO.Ports.SerialPort serialPort1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}
}
Main Code: Form1.CS
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
byte command = Byte.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);
byte[] message = new byte[2] { command, command };
serialPort1.Write(message, 0, message.Length);
}
catch (Exception ex)
{
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}
private delegate void readSerialBufferDelegate(object sender, System.IO.Ports.SerialDataReceivedEventArgs e);
private void readSerialBuffer(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (textBox1.InvokeRequired)
{
Invoke(new readSerialBufferDelegate(readSerialBuffer), new object[] { sender, e });
}
else
{
int[] inputbuffer = new int[serialPort1.BytesToRead];
for (int i = 0; i < serialPort1.BytesToRead; i++)
{
inputbuffer[i] = serialPort1.ReadByte();
textBox1.AppendText(inputbuffer[i].ToString());
}
}
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show("Moates Demon Serial Protocol Test and Version Checker");
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Open();
if (serialPort1.IsOpen)
{
button2.Enabled = false;
button3.Enabled = true;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
button2.Enabled = true;
button3.Enabled = false;
}
}
}
}