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 nameport1.BaudRate =
Convert.ToInt32(cmbbaud.Text);//Set Baudrate of COM portport1.Parity =
Parity.None;port1.DataBits = 8;
port1.Open();
byte[] serialData = new byte[6];//Declare NO. of byte //Enter command to setup PANserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x2B;
//+serialData[3] = 0x45;
//EserialData[4] = 0x4E;
//NserialData[5] = 0x0D;
//Enterport1.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 parametersserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x2B;
//+serialData[3] = 0x4E;
//NserialData[4] = 0x0D;
//Enterport1.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 nodesserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x2B;
//+serialData[3] = 0x53;
//SserialData[4] = 0x4E;
//NserialData[5] = 0x0D;
//Enterport1.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 buzzerserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x2B;
//+serialData[3] = 0x49;
//IserialData[4] = 0x44;
//DserialData[5] = 0x45;
//EserialData[6] = 0x4E;
//NserialData[7] = 0x54;
//TserialData[8] = 0x3A;
//: for (int i = 0; i <= 15; i++){
serialData[i + 9] =
Convert.ToByte(System.Convert.ToInt32(IDRight[i+1]));}
serialData[26] = 0x0D;
//Enterport1.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 LEDserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x53;
//SserialData[3] = 0x52;
//RserialData[4] = 0x45;
//EserialData[5] = 0x4D;
//MserialData[6] = 0x30;
//0serialData[7] = 0x46;
//FserialData[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;
//0serialData[27] = 0x30;
//0serialData[28] = 0x30;
//0serialData[29] = 0x30;
//0serialData[30] = 0x0D;
//Enterport1.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 byteserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x2B;
//+serialData[3] = 0x44;
//DserialData[4] = 0x41;
//AserialData[5] = 0x53;
//SserialData[6] = 0x53;
//SserialData[7] = 0x4C;
//LserialData[8] = 0x0D;
//Enterport1.Write(serialData, 0, 9);
btnexit.Enabled =
true;btnrefresh.Enabled =
true;}
private void btnsend_Click(object sender, EventArgs e){
//Write line to serial portport1.WriteLine(txtcmd.Text);
//Clear the text boxtxtcmd.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 bufferport1.Read(buffer, 0, bytes);
// Show the user the incoming data in hex formattxtmsg.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 bufferport1.Read(buffer, 0, bytes);
// Show the user the incoming data in hex formattextBox0.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 readingserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x53;
//SserialData[3] = 0x52;
//RserialData[4] = 0x45;
//EserialData[5] = 0x4D;
//MserialData[6] = 0x31;
//1serialData[7] = 0x33;
//3serialData[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;
//Enterport1.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 LEDserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x53;
//SserialData[3] = 0x52;
//RserialData[4] = 0x45;
//EserialData[5] = 0x4D;
//MserialData[6] = 0x30;
//0serialData[7] = 0x46;
//FserialData[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;
//0serialData[27] = 0x30;
//0serialData[28] = 0x46;
//FserialData[29] = 0x30;
//0serialData[30] = 0x0D;
//Enterport1.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 linetxtmsg.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 parametersserialData[0] = 0x41;
//AserialData[1] = 0x54;
//TserialData[2] = 0x2B;
//+serialData[3] = 0x4A;
//JserialData[4] = 0x4E;
//NserialData[5] = 0x0D;
//Enterport1.Write(serialData, 0, 6);
btnpan.Enabled =
true;btnrefresh.Enabled =
true;}
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.