Connect To The Arduino With C#

September 18, 2022 | Author: Anonymous | Category: N/A
Share Embed Donate


Short Description

Download Connect To The Arduino With C#...

Description

 

Connect to tth he Ar Arduino wi with C#

MAIN MENU

http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

Home   Home

Physic Physical al Computing Computing  

Connect to the Ardui Arduino no with C#

Home Sunday, 13 Septem September ber 2009 12:12

John Spicer 

Products Physical Computing

Excel Programming

One of the coolest things about the Arduino environment is its compatibility with almost anything. After doing a few projects using only the board and some other components, I started to realise the limitations of only using such a hardware based environment. Being

 About Technicana

able to make a robot was one thing, but wouldn’t it be much cooler to be able to, say, email

Technicana's Blog

you when something happened or control it from a computer program program? ?

Links

That is what I’ve done on this project. Using an Arduino board, Visual C# and a tutorial from the Microsoft 'Coding for Fun' site, I’ve made a simple program thats controlled from a Windows Form. The user can turn the LED on an Arduino board on and off, then e-mail the state of it to their e-mail e-mail address. It’s a proof of concept p project, roject, that could hopefully be applied to more useful things. Before going on to explaining the program program,, I’d like to say a bit about what I think is the coolest platform for this sort of thing - C#. I spent hours trying to send e-mails using command comm and prompt and php scripts, but C# deals with all the tricky low level coding so it’s a lot easier to make much cooler programs. For home robotics projects, I’d recommend C# to absolutely anyone. Right then, on to the project. Below is the form as it would appear when it first opens. T he user can press the ‘On’ button, which would then turn the LED on the arduino board on.  

Once the ‘On’ button has been pressed, the ’Off’ button becomes pressable:

 And once the ‘Off’ ‘Off’ button is pressed and an e-m e-mail ail address is entered:

2 af 6 Connect to tth he Ar Arduino wi with C#

20-08-2010 15:59 http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

 

 

Arduino Code  

voi sett up( ) voi d se { pi nMode( 13, OUTPU TPUT) T) ;   Se Ser i al . begi n( 9600) ; } voi d l oop( ) {   i f ( Se S er i al . avai l abl e( ) )   {   i nt c = Ser i al . r ead( ) ;  

i f ( c == ' 1' )

 

{ di gi t al Wr i t e( 13, HI GH) ;

 

}

 

el s e i f ( c == ' 0' )

   

{ di gi t al Wr i t e( 13, LOW) ;

  }   } }

C# Code I'I'd d recommend recommend putting in some error checking if this were a real project, which I haven't done here. The two main areas for checking are the e-mail address and the connection to the serial port.

usi ng  Syst em; usi ng  System. Col l ect i ons. Gener i c ; usi ng  Sys t em. Component Model ; usi ng  Syst em. Dat a; usi ng  Syst em. Dr awi ng ng;; usi ng  Syst em. Text ; usi ng  Syst em. Wi ndo ndow ws. For ms ; usi ng  Syst em. I O. Po Porr t s ; names pac pace e  Se Serr i al Commun unii cat i on {  

publ i c   par t i al   c l as s  s   For Fo r m1 :   For For m

3 af 6 Connect to tth he Ar Arduino wi with C#

20-08-2010 15:59 http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

 

 

{

 

bool   l edSt at e =  f al s e;

 

publ i c   For Fo r m1( )

     

{ zeCom ompon onen entt ( ) ; I ni t i al i zeC s er er i al Por t 1. Por t Name  =  " COM4" ; s er i al Por t 1. Ba se BaudR udRat at e  =  9600 9600;;   s er er i al Por t 1. Open Open(( ) ;  

  }  

pr i vat e  voi d  For m1_For mCl osi ng ng(( ob objj ec ectt   sender sender ,

For mCl osi ng ngEv Even entt Ar gs e) e)      

{

   

pr i vat e  voi d  but but t on onO On_C _Cll i ck ck(( ob objj ec ectt   send sender , Ev Even entt Ar gs e) e) {  s er er i al Por t 1. Wr i t e( " 1 " ) ;

i f  f    ( s e err i al Por t 1. I sO sOpe pen n)   s e err i al Por t 1. Cl ose se(( ) ; }

   

t ext Box1 ox1.. Text   =  "LED i s on on!! " ; but t onOn. En but Enab abll ed ed   =  f al a l s e;

   

bu b ut t on onO Of f . En Enab abll ed ed   =  t r u e; e; l edSt at e =  t r u e; e;   }

 

pr i vat e  voi d  bu but t onOf f _ Cl Cl i c k( k( obj ect   sende senderr , Eve ven nt Ar gs e) e)

   

{ se s er i al Por t 1. Wr i t e( " 0 " ) ;

   

t ext Box1 ox1.. Text   =  " L ED ED i s o off f ! " ; but t onOn. En but Enab abll ed ed   =  t r u e; e;

 

but t on bu onO Of f . En Enab abll ed ed   =  f al s e;

   

l edSt at e =  f al s e; }

   

pr i vat e  voi d  send_mai l ( s t r i ng  addr addr ess ess)) {

  Sys t em. Net . Mai l . Mai l Message essage   mes s age =  new Sys t em. Net . Mai l . Mai l Message essage(( ) ;   mes s age. age. To  To.. Ad Add d( ad addr dr ess ess)) ;  

mes s age. Su Subj bj ect   =  " Hi Hi t her e . " ;

 

mes s age. From =  new  Sys t em. Net . Mai l . Mai l Address ( "

you@exampl e. com " ) ;        

i f  f    ( l edSt at e ==  t r u e) e) { Body   =  " The LED i s on" ; mes s age. Body }

   

i f  f    ( l edSt at e ==  f al s e) {

   

}

mes s age. Body Body   =  "Th "The e LE LED D i s of f " ;

  Sys t em. Net . Mai l . Smt pC pCll i en entt   s mt p =  new Sys t em. Net . Mai l . Smt pCl pCl i en entt ( " smt p. ukga ukgatt eway. ay. net " ) ;

4 af 6 Connect to tth he Ar Arduino wi with C#

20-08-2010 15:59 http://www.technicana.com/physical-computing/73-connect-to-the-ardu...

 

 

s mt p. Send Send(( mes s age age)) ;

 

}

 

pr i vat e  voi d  but but t on onS Sen end d_C _Cll i ck ck(( obj obj ec ectt   sende senderr , Ev Even entt Ar gs e) e)

     

{ extt Box ox_m _mai l . Text ) ; send_mai l ( t ex }  }

} Incidentally, the following book is an excellent primer primer for getting started with the Arduino board.

  Comments (3) 3. Thursday, 19 August 2010 22:48

Y o u ' r e We l c o m e

John Spicer  No problem guys. Thanks for the feedback. 2. Sunday, 18 July 2010 23:38

t h an k s

 Administrator   Adm inistrator  thanks man. I was searching a project simple like yours to comm communicate unicate with the arduino in c#. hugs 1. Thursday, 01 April 2010 17:23

T h an k

 Administrator   Adm inistrator  Than man, great collaboration, i`m begin with arduino, i`m from Chile, if you have more stuff about this, i like if you can send me this. Mi intention is learn to programar this micro and appli this un proyect to make businnes, if you whant to go with me, this be great. Mi email i chinodread@hotm c [email protected] ail.com Bye...

Add your comment Your name: Subject:

Comment:

POST

 

PREVIEW yvComment yvComment v .1.24.0

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF