Hi:
I am trying to write a serial communication GUI application. My
application has the following specifications. When I set my board up in
hyperterminal I do the following set up: bits per second = 38400, data
bits = 8, parity = none, stop bits = 1, flow control = none. Then once I
hit OK if I type in a "V" my board responds with 6 bits of data.
x[Xdata]y[YData]z[ZData] I have the following code to set up the com
port and then write a V and I have set up an event for when data is
received. But for some reason I never get the event. Am I doing
something wrong with the setup or am I not writing the "V"? Please
help! Below is the code.
*************** ***COMPORT.CS *************** *********
public partial class COM_Port : Form
{
SerialPort SCIPort;
TriaxReloaded tr;
string readVal;
public COM_Port()
{
InitializeCompo nent();
// Clear the CmbPorts combo box
cmbPorts.Items. Clear();
//Get Port names in the System that are currently being used
this.cmbPorts.I tems.Add("<SELE CT PORT>");
foreach (string s in SerialPort.GetP ortNames())
{
cmbPorts.Items. Add(s);
}
if (cmbPorts.Items .Count == 0)badu
{
MessageBox.Show ("No Ports Available!", "COM Port Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
this.Close();
}
}
private void btnOpenPort_Cli ck(object sender, EventArgs e)
{
try
{
SCIPort = new SerialPort();
SCIPort.PortNam e = cmbPorts.Text;
SCIPort.BaudRat e = 38400;
SCIPort.DataBit s = 8;
SCIPort.Parity = Parity.None;
SCIPort.StopBit s = StopBits.One;
SCIPort.Open();
SCIPort.ReadTim eout = 50;
SCIPort.Write(" R");
this.Hide();
tr = new TriaxReloaded() ;
tr.ShowDialog() ;
this.Close();
}
catch
{
MessageBox.Show ("COM Port Selelection Can Not Be
Empty!", "COM Port Warning", MessageBoxButto ns.OK,
MessageBoxIcon. Warning);
}
}
}
*************** ***RAWDATA.CS *************** *********
public RawData()
{
InitializeCompo nent();
COM_Port.SCIPor t.Write("V");
COM_Port.SCIPor t.ReadTimeout = 50;
COM_Port.SCIPor t.Write("V");
getData();
}
private void getData()
{
try
{
if (COM_Port.SCIPo rt.IsOpen)
{
COM_Port.SCIPor t.Write("V");
COM_Port.SCIPor t.DataReceived += new
SerialDataRecei vedEventHandler (port_DataRecei ved);
}
/*if(COM_Port.SC IPort.IsOpen == false)
{
MessageBox.Show ("Port is not open!", "COM Port
Warning", MessageBoxButto ns.OK, MessageBoxIcon. Error);
}*/
}
catch (Exception ex)
{
MessageBox.Show ("Error while opening " + ex.Message);
}
}
void port_DataReceiv ed(object sender, SerialDataRecei vedEventArgs e)
{
//txtXVolt.Text = "data received 1";
try
{
string data2 = "";
//txtXVolt.Text = "data received 1";
while (COM_Port.SCIPo rt.BytesToRead 0)
{
//txtXVolt.Text = "data received 2";
data2 +=
Convert.ToChar( COM_Port.SCIPor t.ReadByte());
VoltX(data2);
}
data2 = "";
}
catch (InvalidOperati onException ex)
{
MessageBox.Show ("Unable to show data!" + ex.Message,
"Data Received Error", MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
}
private void VoltX(string data)
{
txtXVolt.Invoke (new EventHandler(de legate
{
try
{
txtXVolt.Select edText = string.Empty;
char[] delimiters = { 'x', 'y', 'z' };
string[] words = data.Split(deli miters);
string xVolt = words[1];
txtXVolt.Text = "xVolt";
}
catch
{
MessageBox.Show ("Error Reading the Accelerometer
Voltage Value, incorrect data.", "Accelerome ter Value Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
}));
}
*** Sent via Developersdex
http://www.developersdex.com ***