Skip to content

Instantly share code, notes, and snippets.

@MKo-xx
Created February 14, 2011 14:26
Show Gist options
  • Save MKo-xx/825942 to your computer and use it in GitHub Desktop.
Save MKo-xx/825942 to your computer and use it in GitHub Desktop.
ADXL335 C#
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 ADXL335
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SerialPort serial_port_;
private Thread thread_;
private void refresh_button_Click(object sender, EventArgs e)
{
portlist_comboBox.Items.Clear();
foreach (string s in SerialPort.GetPortNames())
{
portlist_comboBox.Items.Add(s);
}
}
private void connect_button_Click(object sender, EventArgs e)
{
if ("Connect" == connect_button.Text)
{
if (-1 == portlist_comboBox.SelectedIndex)
{
MessageBox.Show("Select com port");
return;
}
string port_name = portlist_comboBox.SelectedItem.ToString();
serial_port_ = new SerialPort(port_name, 9600);
serial_port_.Open();
thread_ = new Thread(read_serial_port);
thread_.Start();
connect_button.Text = "Disconnect";
}
else
{
try
{
thread_.Abort();
serial_port_.Close();
}
catch
{
}
connect_button.Text = "Connect";
}
}
private void read_serial_port()
{
while (serial_port_.IsOpen)
{
try
{
string str = serial_port_.ReadLine();
set_axis(str);
}
catch (TimeoutException) { }
}
}
delegate void SetTextCallback(string newText);
private string str_;
private void set_axis(string text)
{
if (this.X_progressBar.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(set_axis);
this.Invoke(d, new object[] { text });
}
else
{
str_ += text;
int i1 = str_.IndexOf("BEG");
int i2 = str_.IndexOf("BEG", 1 + i1);
if ((-1 != i1) && (-1 != i2))
{
string current = str_.Substring(0, i2);
current = current.Substring(current.IndexOf("BEG"));
str_ = str_.Substring(i2);
int beg_index = current.IndexOf("BEG");
int x_index = current.IndexOf("X");
int y_index = current.IndexOf("Y");
int z_index = current.IndexOf("Z");
if ((0 != beg_index) || (3 != x_index) || (-1 == y_index) || (-1 == z_index))
{
MessageBox.Show("Error " + current + ", beg_index = " + beg_index.ToString() + ", x_index = " + x_index.ToString() + ", y_index = " + y_index.ToString() + ", z_index = " + z_index.ToString());
return;
}
int x = Convert.ToInt32(current.Substring(x_index + 1, y_index - x_index - 1));
int y = Convert.ToInt32(current.Substring(y_index + 1, z_index - y_index - 1));
int z = Convert.ToInt32(current.Substring(z_index + 1));
if (x > X_progressBar.Maximum) x = X_progressBar.Maximum;
if (y > Y_progressBar.Maximum) y = Y_progressBar.Maximum;
if (z > Z_progressBar.Maximum) z = Z_progressBar.Maximum;
if (x < X_progressBar.Minimum) x = X_progressBar.Minimum;
if (y < Y_progressBar.Minimum) y = Y_progressBar.Minimum;
if (z < Z_progressBar.Minimum) z = Z_progressBar.Minimum;
X_progressBar.Value = x;
Y_progressBar.Value = y;
Z_progressBar.Value = z;
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
serial_port_.Close();
}
catch
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment