Hi I have this C# code example and I'm trying to run it on monodevelop:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace Serial
{
class Program
{
static SerialPort port;
static void Main(string[] args)
{
Console.WriteLine("Beging Serial...");
BeginSerial(9600, "/dev/ttyACM0");
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
Console.WriteLine("Serial Started.");
Console.WriteLine(" ");
Console.WriteLine("Ctrl+C to exit program");
Console.WriteLine("Send:");
for (; ; )
{
Console.WriteLine(" ");
Console.Write("> ");
port.WriteLine(Console.ReadLine());
}
}
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
for (int i = 0; i < (10000 * port.BytesToRead) / port.BaudRate; i++); //Delay a bit for the serial to catch up
Console.WriteLine(port.ReadLine());
}
static void BeginSerial(int baud, string name)
{
port = new SerialPort(name, baud);
}
}
}
I debug the code step by step but static void port_DataReceived(object sender, SerialDataReceivedEventArgs e) doesn't seem to trigger so I don't get the arduino reply
arduino code that works on arduino IDE serial monitor:
int sensorPin = 0;
int photocellPin = 1;
int photocellReading;
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
if (Serial.available() > 0)
{
char sr = Serial.read();
switch(sr)
{
case '0':
{
// Temp measurement
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
// print out the voltage
//Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.println(temperatureC); //Serial.println(" degress C");
// now convert to Fahrenheight
/*float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degress F");*/
break;
}
case '1':
{
// Light measurement
photocellReading = analogRead(photocellPin);
Serial.print("Photocell reading = ");
Serial.print(photocellReading); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
Serial.println(" - Dark");
} else if (photocellReading < 200) {
Serial.println(" - Dim");
} else if (photocellReading < 500) {
Serial.println(" - Light");
} else if (photocellReading < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
break;
}
case '2':
{
Serial.println("No implementation for analog pin A2");
break;
}
case '3':
{
Serial.println("No implementation for analog pin A3");
break;
}
case '4':
{
Serial.println("No implementation for analog pin A4");
break;
}
case '5':
{
Serial.println("No implementation for analog pin A5");
break;
}
}
}
}
Any ideas?
Thanks in advanced!
PS: Arduino rocks!