Full width home advertisement

Arduino project

Raspberry project

Post Page Advertisement [Top]

Serial communication between Arduino and Visual Studio C++/CLI will help user design an HMI that easily control I/O port.
An easy example will show how to make it, just in 10 minutes.


Hardware need:
1. Arduino Pro Mini
2. Two leds, resistor
3. Reed-switch
4. Visual Studio 2008



Step 1. Making a circuit

Step 2. Code works in Arduino
In general, arduino code will check if command from computer (Visual Studio), then control on/off output (LED)
The code also send status of sensor reed-switch to computer (Visual Studio) in interval time 100ms
const byte pin_sw = 7;         // reed-switch
const byte pin_red = 8;        // light red
const byte pin_yellow = 9;     // light yellow
String mySt = "";
boolean stringComplete = false;  // whether the string is complete
int timer1_counter; //for timer

void setup() {
  pinMode(pin_sw,INPUT_PULLUP);
  pinMode(pin_yellow,OUTPUT);
  pinMode(pin_red,OUTPUT);
  // start serial port at 9600 bps:
  Serial.begin(9600);
  //--------------------------timer setup
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 59286;   // preload timer 65536-16MHz/256/2Hz (34286 for 0.5sec) (59286 for 0.1sec)

  
  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts
  //--------------------------timer setup
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  if (stringComplete) {
    // clear the string when COM receiving is completed
    mySt = "";  //note: in code below, mySt will not become blank, mySt is blank until '\n' is received
    stringComplete = false;
  }

  //receive command from Visual Studio
  if (mySt.substring(0,8) == "vs_rd_on"){
    digitalWrite(pin_red,1);  //turn on RED
  }
  if (mySt.substring(0,9) == "vs_rd_off"){
    digitalWrite(pin_red,0);  //turn off RED
  }
  if (mySt.substring(0,8) == "vs_yl_on"){
    digitalWrite(pin_yellow,1);  //turn on YELLOW
  }
  if (mySt.substring(0,9) == "vs_yl_off"){
    digitalWrite(pin_yellow,0);  //turn off YELLOW
  }
}

ISR(TIMER1_OVF_vect)        // interrupt service routine - tick every 0.1sec
{
  TCNT1 = timer1_counter;   // set timer
  int input_value = digitalRead(pin_sw);
  if (input_value == 1){
    Serial.println("sw_off");
  }
  else{
    Serial.println("sw_on");
  }
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    if (inChar != '\n') {
      mySt += inChar;
    }
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
Step 3. Code works in Visual Studio
In this code, it will send command (from click on HMI by user) to Arduino. It also shows status of reed-switch by reading command from Arduino in interval 100ms
#pragma endregion
 private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
     serialPort1->Open();
     timer1->Start();
     mStr = "";
    }
 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
     serialPort1->WriteLine("vs_rd_on");
   }
 private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
     serialPort1->WriteLine("vs_rd_off");
    }
 private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
     serialPort1->WriteLine("vs_yl_on");
   }
 private: System::Void button4_Click_1(System::Object^  sender, System::EventArgs^  e) {
     serialPort1->WriteLine("vs_yl_off");
   }
 private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
     if(mStr->Length >= 5)
     {
      if(mStr->Substring(3,2) == "on"){
      label3->Text="ON";
      label3->BackColor = System::Drawing::Color::Green;
     }
     if(mStr->Substring(3,2) == "of"){
      label3->Text="OFF";
      label3->BackColor = System::Drawing::Color::Red;
     }
     }
   }
 private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {
     mStr=serialPort1->ReadLine();
   }
All source code of Visual Studio can be found here (Google link)





No comments:

Post a Comment

Bottom Ad [Post Page]