Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Serial Communication between Laptop and Microcontroller

0.00/5 (No votes)
9 Feb 2015 1  
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.

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:

//+++ 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:

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

or simply:

//+++ 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here