Software in Visual C# to ZStar3

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Software in Visual C# to ZStar3

1,672 Views
dionnecm
Contributor I
Hi everyone,

I am using the ZStar3 with the development API Zstartlib.dll. I develop a program in Visual C# 2008 Expression Edition, but it does not work correctelly. I search other references in the internetcode but don't exits.
There are some diferences between the demo project and the following ?

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 Freescale.ZStarLib;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private ZStar3 theZStar = new ZStar3();
        private ZStarSensor theSensor = null;

        public Form1()
        {
            theSensor = theZStar.GetSensor(0);

            InitializeComponent();

            // Event when new burst data was received
            theZStar.OnBurstDataReceived += new ZStar3.OnBurstDataReceivedHandler(theZStar_OnBurstDataReceived);
            // Event that occur when list of Active sensor was changed
            theZStar.OnActiveSensorsChanged += new ZStar3.OnActiveSensorsChangedHandler(theZStar_OnActiveSensorsChanged);
            // Event that occur if ZStarLib lost Connection on Comport
            theZStar.OnConnectionLost += new ZStar3.OnConnectionLostHandler(theZStar_OnConnectionLost);
        }

        // List Of Active sensor was changed
        void theZStar_OnActiveSensorsChanged(object sender, byte sensor)
        {
            // Is first sensor present???
            if ((theZStar.ActiveSensorsMask & 0x0001) == 0x0001)
            {
                // Enable  Burst Data Receive for this sensor
                theSensor.BurstDataReceiveEnable = true;
                // Enable global Burst mode
                theZStar.BurstModeEnabled = true;
                // Switch status label to Connected
                label1.Text = "Conectado";
            }
            else // Sensor index 0 is not connected           
            {
                // Disable  Burst Data Receive for this sensor
                theSensor.BurstDataReceiveEnable = false;
                // Disable global Burst mode
                theZStar.BurstModeEnabled = false;
                // Switch status label to DisConnected
                label1.Text = "Desconectado";
            }
        }

        // ZStarLib lost connection with ZStar hardware
        void theZStar_OnConnectionLost(object sender)
        {
            // Perform Close port button
            button2.PerformClick();
            // Show Error Message
            MessageBox.Show("Conexão serial perdida!", "Conexão ZStarLib", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        // New Burst data was received
        void theZStar_OnBurstDataReceived(object sender, byte sensor)
        {
            // Are these data from our sensor 0
            if (sensor == 0)
                while (theSensor.GetBurstData()) // Get all pending data in Burst Data FIFO Buffer
                {
                    // Update labels with values of actual acceleration for all axes
                    richTextBox1.AppendText( String.Format("{0:F2}g", theSensor.RealX) );
                    richTextBox1.AppendText(String.Format("{0:F2}g", theSensor.RealY));
                    richTextBox1.AppendText(String.Format("{0:F2}g", theSensor.RealZ));
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText("Hello World!!!!\n\n");

            // Check ZStarLib ComPort status and selected item in List box
            if (!theZStar.IsPortOpen && comboBox1.SelectedItem != null)
            {
                // If ComPort exist and ZStarLib has closed port
                // Try to open selected port
                if (!theZStar.OpenPort(((ComPortInfo)comboBox1.SelectedItem).PortNum))
                {
                    // If Failed, show Error message
                    MessageBox.Show("Comunicação serial não iniciada", "Conexão ZStarLib", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    // And go out
                    return;
                }

                // Check USB_Stick type (has to be known!!)
                if (theZStar.ZStarUsbStickType == ZStar3.UsbStickType.Unknown)
                {
                    // Unknown USB Stick type
                    // Close opened port
                    theZStar.ClosePort();
                    // And show Error Message
                    MessageBox.Show("Este não é um dispositivo ZStar3!!!");
                    return;
                }

                // Disable / Enable all depends objects

                // SetUp Enable of all controls on form
                comboBox1.Enabled = false;
                button3.Enabled = false;
                button1.Enabled = false;
                button2.Enabled = true;

                // Disable for all sensor burst data
                theZStar.BurstDataReceiveEnableMask = 0x0000;

                // Keep new sensors(without power switch) sensor awake up when ZStarLib runs
                theZStar.SleepDisabled = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button3.PerformClick();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Get all ComPorts in PC
            ComPortInfo[] ports = ZStar3.GetComPorts();

            // Clear ListBox of Comports
            comboBox1.Items.Clear();

            // walk through all Comports
            foreach (ComPortInfo p in ports)
            {
                // Add each to Comport list
                int ix = comboBox1.Items.Add(p);

                // Check name and look for ZSTAR device
                if (p.FriendlyName.Contains("ZSTAR"))
                {
                    // remember last index of ZSTAR port
                    comboBox1.SelectedIndex = ix;
                }
            }

            // If ZSTAR ComPort wasn't found
            if (comboBox1.SelectedIndex == -1)
            {
                // Select first in List
                comboBox1.SelectedIndex = 0;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Switch of sleepDisabled Sensor capatibilities
            theZStar.SleepDisabled = false;
            // Switch of Burst mode of ZStar
            theZStar.BurstDataReceiveEnableMask = 0x0000;
            theZStar.BurstModeEnabled = false;

            // Close Port
            theZStar.ClosePort();

            // SetUp Form
            label1.Text = "Desconectado";
            comboBox1.Enabled = true;
            button3.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = false;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            button2.PerformClick();
        }
    }
}


Thanks in advance,

Dionne Monteiro
Labels (1)
0 Kudos
1 Reply

318 Views
thisobj
Contributor III
Hello dionnecm,

Without knowing the methods or properites in starlib.dll it's nearly impossible to trace through this code.  However, one thing to try first is to makeInitializeComponent() the first statement in Form1's constructor.

Frank
0 Kudos