Click here to Skip to main content
15,038,645 members
Articles / Desktop Programming / MFC
Tip/Trick
Posted 9 Feb 2015

Stats

15.3K views
627 downloads
11 bookmarked

Serial Communication between Laptop and Microcontroller

Rate me:
Please Sign up or sign in to vote.
4.71/5 (4 votes)
9 Feb 2015CPOL3 min read
Under laptop's control, microcontroller sends sensor data to laptop that displays numbers and graphic.

Introduction

The laptop opens, closes the serial port, starts and stops the data coming from the µcontroller. The laptop is a 64 bit with Windows8, but can be a 32 bit with Windows7 as well. The µcontroller is Arduino Uno: download and install on laptop its IDE Arduino Development Environment, that will install its necessary drivers too.

Background

I've used code from two articles present in this site: “Device Information” by Jonathan Davies 14 Dec 2008? and “Serial library for C++” by Ramon de Klein 13 Nov 2003. An article I initially used to test the communication is: "Arduino, C#, and Serial Interface" by Marco Bertschi 22 Apr 2014, also in this site.

App's language is VC++, µcontroller's language is C.

App is compiled Win32, linked with static library and uses Unicode set.

Using the Code

This app is a multidoc template with 3 types of views: Console (CFormView), Initial View (CEditView) and Graphic lines (CScrollView). Console contains commands and controls: red toolbar button 1 or Menu->File->Console. Initial View shows data stream by means of numbers: red toolbar button 2 or Menu->File->Initial View. Graphic lines shows data stream by means of a blue line: red toolbar button 3 or Menu->File->Graphic lines. Views 2 and 3 can be activated only if view 1 is present. Once activated, views 2 and 3 can stay inactive even if view 1 is closed. Connect µcontroller to laptop with USB cable. Check that in the IDE of µcontroller there is a serial port selected: this is the serial port to choose in app. Close IDE of µcontroller. Launch app and press red toolbar button 1. In Console read the serial ports found in Available ports edit control and in the combobox select the port previously selected by µcontroller's IDE. Serial port will open: Tx and Rx richedit controls will be painted blue and red. There is neither communication setting nor event control. In blue Tx type: 1# return. A message registered in µcontroller will pop up in red Rx. Press red toolbar buttons 2 and 3: views 2 and 3 will show up. In blue Tx type: 2# return. Actual data from photocell of µcontroller will be sent periodically in batches and displayed in red Rx, in view 2 and a line will be drawn in view 3. In blue Tx type: 3# return. The flow of data will stop with port still open. Close view 1 and port will close. If, after typing in blue Tx 2# return, no other commands are typed, the app will stop within some minutes and both view 2 and 3 will show yellow background in the end.

Image 1

I don't show here code I've written or parts I borrowed. They are highlighted in the code uploaded. I simply list software I used from the 2 authors.

I used PortInfo.h and some related code from J. D. to enumerate and list serial ports present in my laptop. His code is contained between bookmarks:

C++
//+++ JD START 
... 
//+++ JD END

in KFormView.cpp, with minor adaptations to my app. Add Setupapi.lib in Linker Additional dependencies.

From R.d.K. I used:

  • Serial.h, Serial.cpp, SerialEx.h, SerialEx.cpp, SerialWnd.h, SerialWnd.cpp, SerialMFC.h as they are. I included SerialMFC.h in KFormView.h and CSerialTlcStdView.cpp.
  • RichCommEdit.h and RichCommEdit.cpp, but I modified slightly RichCommEdit.cpp.
  • ChildView.h and ChildView.cpp. I modified both, inserting code in KFormView.h and KFormView.cpp.

As I used R.d.K.'s code in many points, I bookmarked it:

C++
//+++ RdK START number
... 
//+++ RdK END same number

or simply:

C++
//+++ RdK number

Point of Interest

Communication between 2 devices

History

In the download, there is code, release EXE for MFC app and code for Arduino.

ID numbers of the 2 authors from whom I borrowed code are: 10041, 4574348.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
SuggestionIt is very easy,please search serial library Pin
Jason.LYJ6-Dec-15 22:26
professionalJason.LYJ6-Dec-15 22:26 
GeneralPrimitivo Pin
michael_sh_haifa10-Feb-15 20:48
Membermichael_sh_haifa10-Feb-15 20:48 
GeneralRe: Primitivo Pin
Member 1124686116-Mar-15 6:36
MemberMember 1124686116-Mar-15 6:36 
I'm sure there are better ways to do what I did. My answer comes late as I didn't have time before. I'm only an hobbyist. I don't need reply.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.