Arduino在以太网上开启/关闭不起作用

时间:2016-04-25 15:04:18

标签: c# arduino ethernet

我正在尝试通过以太网(用于串行端口通信工作)从C#上的桌面应用程序开启/关闭指向Arduino,我正在连接到IP地址以与Arduino进行通信,但是我的代码看起来有问题或者我的Arduino草图。 (我正在使用以太网屏蔽) 我的代码Arduino

//////////INCLUDE HEADER FILES //////////////////
#include <Ethernet.h>
#include <SPI.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <SD.h>
#include <Wire.h>

EthernetServer server(23 );
String content;
File myFile;
int ledPin = 7;
int  outputpin = 0;
void setup()
{
  IPAddress ip( 192, 168,1,110);
  byte mac[] = { 
    0xDE,0xAE,0xBE,0xEF,0xFE,0xED
  };

  Ethernet.begin( mac, ip );
  server.begin();
  if ( ! SD.begin( 4 ) ) 
  {
    return;
  }

  Serial.begin(9600);
}

void loop()
{
  waitIncommingConnection();                                                             
}

void waitIncommingConnection()
{

  String pwd            = "";
  String inData         = "";
  EthernetClient client = server.available();

  if ( client  )
  {
    while ( client.connected() ) 
    {      
      if ( client.available() > 0)
      { 
        char recieved = client.read();
        inData += recieved;                       
        if (recieved == '\n')
        {                           
          switch( inData[0] )
          {                            
            case (char)'o' : 
              client.println("o");
              digitalWrite(ledPin, HIGH);
            break;

            case (char)'f' :
              client.println("f");
              digitalWrite(ledPin, LOW);

            break;

            case (char)'*' :
              Logout(client);  
            break;

            default:
               client.println('d');
            break;
          }
          inData = ""; 
        }

      }
    }
  }
  else
  {
   client.println('v');
  }
}

void Logout(EthernetClient client )
{ 
  client.print('x');
  client.stop(); 

}

我的C#代码

public partial class Form1 : Form
{
    public Client client;
    public delegate void FeedBackCallback(string text);
    public delegate void UpdateMessageBoxCallback(string text);

    public Form1()
    {
        InitializeComponent();
    }

    private void connect_Click(object sender, EventArgs e)
    {
        string ipAddr = ip1.Text + "." + ip2.Text + "." + ip3.Text + "." + ip4.Text;
        string port = portInput.Text;

        if (IsValidIPAddress(ipAddr) == true)
        {
            try
            {
                if (client == null)
                    client = new Client(this);

                client.Connect(ipAddr, port);
                //  client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes("c"+'\n'));
                disconect.Enabled = true;
                connect.Enabled = false;
                on.Enabled = true;
            }
            catch (SocketException se)
            {
                MessageBox.Show("Unable to Connect.\r\n" + se.ToString());
            }
        }
        else
        {
            MessageBox.Show("Invaild Ip Adrress", "Invaild Ip Adrress", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private bool IsEmptyUserNamePasswordFields(string userName, string password)
    {

        if (userName.Length == 0 || password.Length == 0)
        {
            MessageBox.Show("Password and Username field is required!", "Required Fileds Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
        else
        {
            return true;
        }
    }

    private bool IsValidIPAddress(string ipaddr)//Validate the input IP address
    {
        try
        {
            IPAddress.Parse(ipaddr);
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    private void on_Click(object sender, EventArgs e)
    {
        disconect.PerformClick();
        connect.PerformClick();
        try
        {
            if (client == null)
                client = new Client(this);
client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes("o" + '\n'));
            off.Enabled = true;
            on.Enabled = false;
        }
        catch (SocketException se)
        {
            MessageBox.Show("Unable to Connect.\r\n" + se.ToString());
        }
    }

    private void off_Click(object sender, EventArgs e)
    {
        disconect.PerformClick();
        connect.PerformClick();
        try
        {
            if (client == null)
                client = new Client(this);
client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes("f" + '\n'));
            on.Enabled = true;
            off.Enabled = false;
        }
        catch (SocketException se)
        {
            MessageBox.Show("Unable to Connect.\r\n" + se.ToString());
        }
    }

    private void disconect_Click(object sender, EventArgs e)
    {
        connect.Enabled = true;
        disconect.Enabled = false;
        client.Disconnect();
    }
}

Arduino的图片 enter image description here 我不知道我的问题在哪里,应用程序连接到arduino,但On / Off按钮不适用于led。 如果你想看到串口通信的代码,这就是这里的链接,Serial Port

0 个答案:

没有答案