使用Com Port自动检测功能使用Arduino进行C#编程 [英] C# programming with Arduino using Com Port Auto detect function

查看:85
本文介绍了使用Com Port自动检测功能使用Arduino进行C#编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Hello All,



我试图控制步进电机使用C#的arduino。该程序在第一次运行时完美无缺。但是当我在第二个实例启动程序时,它失败了。



我试图整合自动检测功能(http://playground.arduino.cc/Csharp/ SerialCommsCSharp)和步进控制代码在一起。但是代码中存在一些问题,它只能运行一次。请帮助我解决问题。



Hello All,

I am trying to control a stepper motor through arduino using C#. The program works perfectly fine at the first instance. But when i launch the program at the second instance it fails.

I have tried to integrate the autodetect function (http://playground.arduino.cc/Csharp/SerialCommsCSharp) and stepper control code together. But there is some issue in the code which lets it run perfectly only once. Kindly help me to solve the issue.

/*

 * Serial Port Monitor
 *
 *
 */

//Setup Output
// int ledPin_3 = 3;
#include <Stepper.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(0, 1);
int out1 = 8;
int out2 = 9;
int enA  = 6;
int enB  = 11;
int dirA = 5;
int dirB = 3;
int steps=0;
int inbyte;
int dela_time;
int frm_cnt=0;
int serialdata;
int rotmode;
int rotdir;
int motspeed;    // assigning the motor speed

//int frames;
const int stepsperrev = 8000;      // No. of micro steps per revolution
Stepper myStepper(stepsperrev, out1, dirA, out2, dirB);

//Setup message bytes

byte inputByte_0;

byte inputByte_1;

byte inputByte_2;

byte inputByte_3;

byte inputByte_4;

//byte inputByte_5;

//Setup
int ledPin_3 =13;

void setup() {

  pinMode(ledPin_3, OUTPUT);
  Serial.begin(9600);
  digitalWrite(ledPin_3, HIGH);//
  delay(2500);//
  digitalWrite(ledPin_3, LOW);//
  delay(2500);//
  pinMode(enA, OUTPUT);
  digitalWrite (enA, HIGH);
  pinMode(enB, OUTPUT);
  digitalWrite (enB, HIGH);
  Serial.begin(9600);

}


void newsetup()
{
  pinMode(enA, OUTPUT);
  digitalWrite (enA, HIGH);
  pinMode(enB, OUTPUT);
  digitalWrite (enB, HIGH);

}
void reset()
{
  digitalWrite (out1, LOW);
  digitalWrite (dirA, LOW);
  digitalWrite (out2, LOW);
  digitalWrite (dirB, LOW);
  digitalWrite (enA, LOW);
  digitalWrite (enB, LOW);
  steps=0;
  frm_cnt=0;
}

void getinput()

{
  //******************************************

 //Get the delay time
getSerial();
dela_time = serialdata;
//Serial.println(dela_time);

// Get rotation mode
getSerial();
rotmode = serialdata;
//Serial.println(rotmode);

// Get direction of rotation
getSerial();
rotdir = serialdata;
//Serial.println(rotdir);

// Get motor speed
getSerial();
motspeed = serialdata;
//Serial.println(motspeed);

// Get number of frames
getSerial();
steps = serialdata;
//Serial.println(steps);

// ****************************************
}

//Main Loop

void loop() {

    //Read Buffer
  while (Serial.available()==5)
  {
    //Read buffer
    inputByte_0 = Serial.read();
    delay(100);
    inputByte_1 = Serial.read();
    delay(100);
    inputByte_2 = Serial.read();
    delay(100);
    inputByte_3 = Serial.read();
    delay(100);
    inputByte_4 = Serial.read();
  }
  //Check for start of Message
  if(inputByte_0 == 16)
  {
       //Detect Command type
       switch (inputByte_1)
       {
          case 127:
             //Set PIN and value
             switch (inputByte_2)
            {
              case 4:
                if(inputByte_3 == 255)
                {

                  digitalWrite(ledPin_3, HIGH);
                  break;
                }
                else
                {
                  digitalWrite(ledPin_3, LOW);
                  break;
                }
              break;
            }
            break;
          case 128:
            //Say hello
            Serial.println("HELLO");
            break;
        }
        //Clear Message bytes
        inputByte_0 = 0;
        inputByte_1 = 0;
        inputByte_2 = 0;
        inputByte_3 = 0;
        inputByte_4 = 0;
        //Let the PC know we are ready for more data
        delay(1000);
        //Serial.print("-READY TO RECEIVE");



        frm_cnt=0;
        getinput();
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// Switch Case statment for Stepped and Continous mode

  switch(rotmode)
  {
      case 1: // Stepped motion
  {

// Switch case statement for Left and Right Motion
    switch (rotdir)
    {
      case 3: // Left motion
      {

        myStepper.setSpeed(motspeed);

    newsetup();

// Looping for a complete cycle
        while (frm_cnt<steps){

            if (frm_cnt<steps)

        {

            if (steps!=0)

                        myStepper.step(-(stepsperrev*4/steps));
                        //
                        //delay(2500);//

            delay(dela_time*1000);

        }
        else
        {
            reset();
        }

                frm_cnt = frm_cnt+1;
            } // End of While loop

        break;
      } // End of Left motion Code

      case 4: // Right motion
      {

        myStepper.setSpeed(motspeed);


        newsetup();
    // While loop for complete Cycle
            while (frm_cnt<steps){
        if (frm_cnt<steps)
        {
            if (steps!=0)
            myStepper.step((stepsperrev*4/steps));

            delay(dela_time*1000);

        }
        else
        {
            reset();
        }
            frm_cnt = frm_cnt+1;
      } // End of While loop

      break;
      } // End of Right motion code
    }

  // End of Stepped motion code
  break;
  }

  case 2: // Continous motion
  {
    break;
  }
  }


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
           //Clear Message bytes
        //inputByte_0 = 16;
        //inputByte_1 = 0;
        //inputByte_2 = 0;
        //inputByte_3 = 0;
        //inputByte_4 = 0;
        //serialdata = 0;

  }


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}

//***************************************************************************

//====================== Get Serial ======================//
 // Get Serial parameters
long getSerial()
{
  serialdata = 0;
  while (inbyte != '/')
  {
    inbyte = Serial.read();
   //Serial.println(inbyte);
    if (inbyte > 0 && inbyte != '/')
    {

      serialdata = serialdata * 10 + inbyte - '0';
    }
  }
  inbyte = 0;
  return serialdata;
}

推荐答案

我没办法测试这个但是没有打开然后每次关闭串口写信给它。您正在打开端口,设置事件处理程序然后关闭端口。这不好,因为设置事件处理程序是浪费时间和资源,因为你立即关闭端口。



点击你的开始按钮打开端口并保留它打开。关闭应用程序时,关闭端口对象的AND DISPOSE。
There's no way for me to test this but don't open then close the serial port every time you write to it. You're opening the port, setting up an event handler and then closing the port. That's not good as setting the event handler is a waste of time and resources since you immediately close the port.

Open the port when your Start button is clicked and leave it open. When you close the application, close AND DISPOSE of the port object.


这篇关于使用Com Port自动检测功能使用Arduino进行C#编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆