matt

matt

  • NA
  • 1
  • 0

ZigBee GUI using C#

Jun 5 2008 4:29 AM

hi everyone,

i am writing a program on ZigBee using Microsoft visual Studio 2005 C#. But currently i couldn't get the result i desired. I was able to establish PAN and scan for node, but i couldn't get my node to read temperature, sound the buzzer and ON/OFF the LEDs. So if possible, can anyone out there please help me check and recorrect my program. Thanks, your help is truely appreciated.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.IO.Ports;

using System.Text.RegularExpressions;

namespace Telegesis_ZigBee_Kit_Control_Terminal

{

public enum textmsgtype { Incoming };

public partial class terminal : Form

{

SerialPort port1 = new SerialPort();//Declaring serialport

public terminal()

{

InitializeComponent();

InitializeComports();

btnrefresh.Enabled = false;

btnsend.Enabled = true;

btnclear.Enabled = true;

button1.Enabled = true;

}

//Check the available COM ports and list in the combobox

private void InitializeComports()

{

cmbcom.Items.Clear();

foreach (string s in SerialPort.GetPortNames())

{

cmbcom.Items.Add(s);

}

}

private void btnsetup_Click(object sender, EventArgs e)

{

if (cmbcom.Text.Equals(""))

{

MessageBox.Show("Please Choose COM port!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

else if (cmbbaud.Text.Equals(""))

{

MessageBox.Show("Please Choose Baud Rate!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

else

{

port1.Close();

port1.PortName = cmbcom.Text;//Declare COM name

port1.BaudRate = Convert.ToInt32(cmbbaud.Text);//Set Baudrate of COM port

port1.Parity = Parity.None;

port1.DataBits = 8;

port1.Open();

byte[] serialData = new byte[6];//Declare NO. of byte

//Enter command to setup PAN

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x2B;//+

serialData[3] = 0x45;//E

serialData[4] = 0x4E;//N

serialData[5] = 0x0D;//Enter

port1.Write(serialData, 0, 6);

btnsetup.Enabled = true;

btnrefresh.Enabled = true;

}

}

private void btnpan_Click(object sender, EventArgs e)

{

byte[] serialData = new byte[5];//Declare NO. of byte;

//Display network parameters

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x2B;//+

serialData[3] = 0x4E;//N

serialData[4] = 0x0D;//Enter

port1.Write(serialData, 0, 5);

btnpan.Enabled = true;

btnrefresh.Enabled = true;

}

private void btnscan_Click(object sender, EventArgs e)

{

timer1.Enabled = false;

timer2.Enabled = true;

byte[] serialData = new byte[6];//Declare NO. of byte

//Scan network for other nodes

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x2B;//+

serialData[3] = 0x53;//S

serialData[4] = 0x4E;//N

serialData[5] = 0x0D;//Enter

port1.Write(serialData, 0, 6);

btnsetup.Enabled = true;

btnrefresh.Enabled = true;

}

private void btnbuzzer_Click(object sender, EventArgs e)

{

timer2.Enabled = false;

timer1.Enabled = true;

string IDRight = textBox00.Text;

byte[] serialData = new byte[27];//Declare NO. of byte

//Send command to activate buzzer

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x2B;//+

serialData[3] = 0x49;//I

serialData[4] = 0x44;//D

serialData[5] = 0x45;//E

serialData[6] = 0x4E;//N

serialData[7] = 0x54;//T

serialData[8] = 0x3A;//:

for (int i = 0; i <= 15; i++)

{

serialData[i + 9] = Convert.ToByte(System.Convert.ToInt32(IDRight[i+1]));

}

serialData[26] = 0x0D;//Enter

port1.Write(serialData, 0, 27);

btnsetup.Enabled = true;

btnrefresh.Enabled = true;

}

private void btnled_Click(object sender, EventArgs e)

{

byte[] IDRight = Encoding.ASCII.GetBytes(textBox00.Text);

byte[] serialData = new byte[31];//Declare NO. of byte

//Send command to turn on LED

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x53;//S

serialData[3] = 0x52;//R

serialData[4] = 0x45;//E

serialData[5] = 0x4D;//M

serialData[6] = 0x30;//0

serialData[7] = 0x46;//F

serialData[8] = 0x3A;//:

int bytes =port1.BytesToWrite;

for (int i = 0; i <= 15; i++)

{

serialData[i + 9] = IDRight[i+1];

}

serialData[25] = 0x3D;//=

serialData[26] = 0x30;//0

serialData[27] = 0x30;//0

serialData[28] = 0x30;//0

serialData[29] = 0x30;//0

serialData[30] = 0x0D;//Enter

port1.Write(serialData, 0, 31);

btnsetup.Enabled = true;

btnrefresh.Enabled = true;

}

private void btnexit_Click(object sender, EventArgs e)

{

byte[] serialData = new byte[9];//Declare NO. of byte

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x2B;//+

serialData[3] = 0x44;//D

serialData[4] = 0x41;//A

serialData[5] = 0x53;//S

serialData[6] = 0x53;//S

serialData[7] = 0x4C;//L

serialData[8] = 0x0D;//Enter

port1.Write(serialData, 0, 9);

btnexit.Enabled = true;

btnrefresh.Enabled = true;

}

private void btnsend_Click(object sender, EventArgs e)

{

//Write line to serial port

port1.WriteLine(txtcmd.Text);

//Clear the text box

txtcmd.Text = "";

}

private void btnrefresh_Click(object sender, EventArgs e)

{

txtmsg.Text = "";

}

private void btnclear_Click(object sender, EventArgs e)

{

NodeIDList.Text = "";

}

private void timer1_Tick(object sender, EventArgs e)

{

try

{

// Obtain the number of bytes waiting in the port's buffer

int bytes = port1.BytesToRead;

if (bytes != 0)

{

byte[] buffer = new byte[bytes];

// Read the data from the port and store it in our buffer

port1.Read(buffer, 0, bytes);

// Show the user the incoming data in hex format

txtmsg.Text += ByteArrayToAsciiString(buffer) + Environment.NewLine;

}

}

catch (Exception exc)

{

// An exception is raised when there is no information to read.

// Don't do anything here, just let the exception go.

}

}

//Convert Byte Array To Hex String

private string ByteArrayToHexString(byte[] data)

{

StringBuilder sb = new StringBuilder(data.Length * 3);

foreach (byte b in data)

sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));

return sb.ToString().ToUpper();

}

//Convert Byte Array To Ascii String

private string ByteArrayToAsciiString(byte[] data)

{

StringBuilder sb = new StringBuilder(data.Length * 3);

foreach (byte b in data)

sb.Append(Convert.ToChar(b));

return sb.ToString().ToUpper();

}

private void timer2_Tick(object sender, EventArgs e)

{

timer1.Enabled = false;

try

{

// Obtain the number of bytes waiting in the port's buffer

int bytes = port1.BytesToRead;

if (bytes != 0)

{

byte[] buffer = new byte[bytes];

// Read the data from the port and store it in our buffer

port1.Read(buffer, 0, bytes);

// Show the user the incoming data in hex format

textBox0.Text += ByteArrayToAsciiString(buffer) + Environment.NewLine;

if (textBox0.Text.Contains("FFD"))

{

string NodeID = textBox0.Text;

string[] field = NodeID.Split(new Char[] { ':' },17);

textBox00.Text = field[1];

NodeIDList.Text += textBox00.Text;

}

else

{

txtmsg.Text = txtmsg.Text + textBox0.Text;

}

if(txtmsg.Text.Contains("S13"))

{

string Hex = textBox0.Text;

string[] field = Hex.Split(new char[] { ':' },4);

int i = Convert.ToInt32(field[1], 16);

int tem = (i - 600) / 10;

txttemp.Text="The Current Temperature is:" + tem.ToString()+"degree";

}

}

}

catch (Exception exc)

{

// An exception is raised when there is no information to read.

// Don't do anything here, just let the exception go.

}

}

private void btntem_Click(object sender, EventArgs e)

{

string IDRight = textBox00.Text;

byte[] serialData = new byte[28];//Declare NO. of byte

//Send command to activate temperature reading

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x53;//S

serialData[3] = 0x52;//R

serialData[4] = 0x45;//E

serialData[5] = 0x4D;//M

serialData[6] = 0x31;//1

serialData[7] = 0x33;//3

serialData[8] = 0x3A;//:

for (int i = 0; i <= 15; i++)

{

serialData[i + 9] = Convert.ToByte(System.Convert.ToInt32(IDRight[i+1]));

}

serialData[25] = 0x3F;//?

serialData[26] = 0x0D;//Enter

port1.Write(serialData, 0, 27);

btnsetup.Enabled = true;

btnrefresh.Enabled = true;

}

private void btnledoff_Click(object sender, EventArgs e)

{

byte[] IDRight = Encoding.ASCII.GetBytes(textBox00.Text);

byte[] serialData = new byte[32];//Declare NO. of byte

//Send command to turn off LED

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x53;//S

serialData[3] = 0x52;//R

serialData[4] = 0x45;//E

serialData[5] = 0x4D;//M

serialData[6] = 0x30;//0

serialData[7] = 0x46;//F

serialData[8] = 0x3A;//:

int bytes = port1.BytesToWrite;

for (int i = 0; i <= 15; i++)

{

serialData[i + 9] = IDRight[i + 1];

}

serialData[25] = 0x3D;//=

serialData[26] = 0x30;//0

serialData[27] = 0x30;//0

serialData[28] = 0x46;//F

serialData[29] = 0x30;//0

serialData[30] = 0x0D;//Enter

port1.Write(serialData, 0, 31);

btnsetup.Enabled = true;

btnrefresh.Enabled = true;

}

 

private void txtmsg_TextChanged_1(object sender, EventArgs e)

{

//Make sure the message display the last line

txtmsg.SelectionStart = txtmsg.TextLength;

// Scrolls the contents of the control to the current caret position.

txtmsg.ScrollToCaret();

}

private void button1_Click(object sender, EventArgs e)

{

txttemp.Text = "";

}

private void btnjoin_Click(object sender, EventArgs e)

{

byte[] serialData = new byte[6];//Declare NO. of byte;

//Display network parameters

serialData[0] = 0x41;//A

serialData[1] = 0x54;//T

serialData[2] = 0x2B;//+

serialData[3] = 0x4A;//J

serialData[4] = 0x4E;//N

serialData[5] = 0x0D;//Enter

port1.Write(serialData, 0, 6);

btnpan.Enabled = true;

btnrefresh.Enabled = true;

}

}

}