Noob Trying Serial Communication in C#

CentralPaTuning

#1 ◈ 2024-12-11

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;
            }
        }
    }
}

CentralPaTuning

#2 ◈ 2024-12-11

Re: Noob Trying Serial Communication in C#

Okay new thing. I remembered I have a USB -> Serial Adapter. I jumped pins 2&3 so whatever I output is my input. Doing so, I now can read what I send. But what I output in my program is not what I receive.

Examples:
Output Input
56 -> 86
38 -> 56
22 -> 34
1D -> 29
1A -> 26
3B -> 59

blah blah blah.

No idea what the hell is going on.

Smooth1

#3 ◈ 2024-12-11

Re: Noob Trying Serial Communication in C#

New thing, lol. You know what base 16 is, right? Also known as Hex. 0x56 hex = 'V' = Decimal (Base 10) 86. Look it up on Wikipedia if you seriously don't understand.

CentralPaTuning

#4 ◈ 2024-12-11

Re: Noob Trying Serial Communication in C#

I realized, later. I got it working, I didn't even think about my Baud as I was thinking it was sooo much more complicated. Now returns the version of Moates products and going to try to work towards some other stuff.

Thanks for the tip though, if I hadn't looked it up right before that I would be looking it up now.

Smooth1

#5 ◈ 2024-12-11

Re: Noob Trying Serial Communication in C#

For sure. Just have to see the patterns in things to recognize what's going on with stuff that's new to you.