kirillbobyrev
kirillbobyrev

Reputation: 1739

issue with serial port + C# data receiving

I want to transfer data that appears in Arduino to my C# application and do not know what's wrong in my code. Here comes Arduino code:

int switchPin = 7;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean flashLight = LOW;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    Serial.print("UP");

    digitalWrite(ledPin, HIGH);
  }
  if (lastButton == HIGH && currentButton == LOW)
  {
    Serial.print("DOWN");

    digitalWrite(ledPin, LOW);
  }

  lastButton = currentButton;
}

As you can see, this simple sketch sends message to port while the button is pressed. I have created a console C# app to receive data:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
using System.IO.Ports;

namespace ArduinoTestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort("COM3", 9600);
            port.Open();
            string lane;
            while (true)
            {
                lane = port.ReadLine();

                Console.WriteLine(lane);
            }

        }
    }
}

But when I push the button console is still empty. Tell me what's wrong, please!

Upvotes: 3

Views: 1600

Answers (1)

kirillbobyrev
kirillbobyrev

Reputation: 1739

It's all simple. I forget to write

Serial.begin()

:D That's all. Now it works.

Upvotes: 2

Related Questions