hello, am currently working on a final year project- water quality monitoring system using the netduino. the system uses RF transceivers for data transmission, the receiving node receives the data and sends it to the computer using the serial port. Am using the prolific USB-to-TTL converter.
The problem is that i can't see anything displayed in my richtextbox because am using charp to build the desktop application, but whenever i use hyperterminal the values are displayed correctly, below is my code for the netduino and the application. please anybody out there who can help me figure out this problem, I deeply appreciate. thanks
///the program on the netduino
using System;
using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
namespace flash_led
{
public class Program
{
private const int MaximumValue = 1023;
private const float analogReference=5.0f;
static SerialPort myserial;
public static void Main()
{
myserial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One); // create a serial port
myserial.Open(); // open serial port for writing
OutputPort led = new OutputPort(Pins.GPIO_PIN_D7, false);
OutputPort led2 = new OutputPort(Pins.GPIO_PIN_D4, false);
AnalogInput adcPort = new AnalogInput(Pins.GPIO_PIN_A0);
int digitalValue;
float analogvalue;
double turbidity;
while (true)
{
led2.Write(true);
Thread.Sleep(1000);
digitalValue = adcPort.Read();
analogvalue = (float)digitalValue / MaximumValue * analogReference;
turbidity = (20.83 * analogvalue - 0.6);
byte[] final_value = System.Text.Encoding.UTF8.GetBytes(turbidity.ToString());
led.Write(true);
Thread.Sleep(50);
myserial.Write(final_value, 0, final_value.Length);
led.Write(false);
Thread.Sleep(2000);
analogvalue = 0;
Thread.Sleep(1000);
}
}
}
}
// and for my desktop application
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;
namespace water_quality
{
public partial class Form1 : Form
{
SerialPort sp = new SerialPort();
string line;
public Form1()
{
InitializeComponent();
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); //
}
private void connect_button_Click(object sender, EventArgs e)
{
try
{
sp.PortName = "COM2";
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.Open();
}
catch
{
MessageBox.Show(" the serialport " + sp.PortName + " could not be found" );
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
System.Threading.Thread.Sleep(100);
byte[] incoming = new byte[sp.BytesToRead];
sp.Read(incoming, 0, incoming.Length);
line = System.Text.Encoding.UTF8.GetString(incoming);
if (sp.BytesToRead > 0)
{
this.Invoke(new EventHandler(display));
}
}
public void display(object sender, EventArgs e)
{
this.turbidity.Text= line; // turbidity is the richtextbox name
}
}
}