C# ,DC Motors

Hi!

I need to control 2 dc motors (5V)

so what did I do:

1)wrote scetch for arduino and uploaded to thr board

include <AFMotor.h>;
int outputPin = 13;                //[ch1079][ch1076][ch1077][ch1089][ch1100] [ch1093][ch1088][ch1072][ch1085][ch1080][ch1084] [ch1085][ch1086][ch1084][ch1077][ch1088] [ch1082][ch1086][ch1085][ch1090][ch1072][ch1082][ch1090][ch1072]
int val;
AF_DCMotor motorL(1);
AF_DCMotor motorR(2);

void setup()
{
  Serial.begin(9600);              //[ch1091][ch1089][ch1090][ch1072][ch1085][ch1086][ch1074][ch1082][ch1072] [ch1087][ch1086][ch1088][ch1090][ch1072] [ch1085][ch1072] [ch1089][ch1082][ch1086][ch1088][ch1086][ch1089][ch1090][ch1100] 9600 [ch1073][ch1080][ch1090]/[ch1089][ch1077][ch1082]
  pinMode(outputPin, OUTPUT);      //[ch1091][ch1089][ch1090][ch1072][ch1085][ch1072][ch1074][ch1083][ch1080][ch1074][ch1072][ch1077][ch1084] 13 [ch1082][ch1086][ch1085][ch1090][ch1072][ch1082][ch1090] [ch1074] [ch1088][ch1077][ch1078][ch1080][ch1084] [ch1074][ch1099][ch1074][ch1086][ch1076][ch1072]
}

void loop()
{
  if (Serial.available()) {         //[ch1077][ch1089][ch1083][ch1080] [ch1077][ch1089][ch1090][ch1100] [ch1087][ch1088][ch1080][ch1085][ch1103][ch1090][ch1099][ch1081] [ch1089][ch1080][ch1084][ch1074][ch1086][ch1083],
    val = Serial.read();            //  [ch1090][ch1086] [ch1095][ch1080][ch1090][ch1072][ch1077][ch1084] [ch1077][ch1075][ch1086] [ch1080] [ch1089][ch1086][ch1093][ch1088][ch1072][ch1085][ch1103][ch1077][ch1084] [ch1074] val
    if (val == 'L') {               //  [ch1077][ch1089][ch1083][ch1080] [ch1087][ch1088][ch1080][ch1085][ch1103][ch1090] [ch1089][ch1080][ch1084][ch1086][ch1074][ch1086][ch1083] 'H',...
      motorL.run(FORWARD);
      motorL.setSpeed(128);
      motorR.run(BACKWARD);
      motorR.setSpeed(128);
    } 
    if (val == 'R') {               //  [ch1077][ch1089][ch1083][ch1080] [ch1087][ch1088][ch1080][ch1085][ch1103][ch1090] [ch1089][ch1080][ch1084][ch1086][ch1074][ch1086][ch1083] 'L',
      motorL.run(BACKWARD);
      motorL.setSpeed(128);
      motorR.run(FORWARD);
      motorR.setSpeed(128);      
    }
    if(val=='S'){
      motorL.run(RELEASE);
      motorL.setSpeed(128);
      motorR.run(RELEASE);
      motorR.setSpeed(128);
    }
    if(val=='B'){
      motorL.run(BACKWARD);
      motorL.setSpeed(128);
      motorR.run(BACKWARD);
      motorR.setSpeed(128);
    }
    if(val=='F'){
      motorL.run(FORWARD);
      motorL.setSpeed(128);
      motorR.run(FORWARD);
      motorR.setSpeed(128);
    }
  }
}

2)wrote program in C#,which sent commands to arduiono

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

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort("COM3", 9600);
            port.Open();
            while (true)
            {
                String s = Console.ReadLine();
                if (s.Equals("exit"))
                {
                    break;
                }
                port.Write(s + '\n');
            }
            port.Close();
        }
    }
}

…but when I use my C# program , to control DC motors nothing happenes,while using terminal - is OK!

what did I do wrong?
Coun you explaine me , is it possible t send commands direct to arduino (via com-port) without uploading scetch to arduino board?
thx in advance!

Without thinking too hard on it I'm wondering if the newline character you're sending from your c# program may be causing the == not to work..

Is it possibly trying to evaluate 0x4C == 0x4C 0x0A ? These two would not be equivalent. This is without thinking about it much though and may not be the problem at all. Just an easy thing to check.

well , I've tried to use without '/n' and nothing happened : still , when I send command my DC Motors dont work

Try flashing the onboard LED when serial data is available. I suspect that the C# application is not actually communicating with the Arduino.

Is the Serial Monitor window open while the C# application is running? Is the COM port (COM3) the one that the Arduino is actually connected to?

I use C# to talk to my Arduino without any problems. I open the port a little differently than you do:

            public static System.IO.Ports.SerialPort port;

                  System.ComponentModel.IContainer components = 
                        new System.ComponentModel.Container();
                  port = new System.IO.Ports.SerialPort(components);
                  port.PortName = comPort.SelectedItem.ToString();
                  port.BaudRate = Int32.Parse(baudRate.SelectedItem.ToString());
                  port.DtrEnable = true;
                  port.ReadTimeout = 5000;
                  port.WriteTimeout = 500;
                  port.Open();

(I have drop down boxes on my form to select the port and baud rate. You could hard code them, instead.)

Thx PaulS!It works!

My code:

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

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.Ports.SerialPort port;

            System.ComponentModel.IContainer components =
                new System.ComponentModel.Container();
            port = new System.IO.Ports.SerialPort(components);
            port.PortName = "COM3";
            port.BaudRate = 9600;
            port.DtrEnable = true;
            port.ReadTimeout = 5000;
            port.WriteTimeout = 500;
            port.Open();
            while (true)
            {   
                String s = Console.ReadLine();
                if (s == "exit")
                {
                    break;
                }
                port.Write(s);
            }
            port.Close();
        }
    }
}