Using Visual C++Module 6                   © Akshat Wahi | roboVITics, 2012   1
• The overall goal is to give a basic overview of how Serial  Communications programming works using Windows API          ...
• Make sure to include “windows.h” header file.      HANDLE hSerial;      hSerial = CreateFile(TEXT("COM1“),              ...
• Now we have a HANDLE to the serial port, we need to set certain  parameters for communication (baud rate, stopbits etc.)...
• Now we have an open serial port with correct  parameters, so we can begin writing data .         char data =‘f’;        ...
• Reading is similar to writing we use ReadFile()         char data;         DWORD bytesread=0;         if(!ReadFile(hSeri...
• Once you’re done using the serial port, make sure to  close the HANDLE.• If you don’t weird things can happen.          ...
• http://msdn.microsoft.com/en-us/library/ms123401.aspx                    © Akshat Wahi | roboVITics, 2012      8
• Mayank Prasad      President, roboVITics      mayank@robovitics.in• Akshat Wahi      Asst. Project Manager, roboVITics  ...
Upcoming SlideShare
Loading in …5
×

RoboCV Module 6: Hardware Interfacing using Visual C++

2,211 views

Published on

These are the slides of the RoboCV Workshop organized by roboVITics on August 11th-12th, 2012 in TT311 Smart Classroom, VIT University, Vellore.

The workshop was delivered by the following people:
1. Mayank Prasad, President of roboVITics
2. Akash Kashyap, President of TEC - The Electronics Club of VIT
3. Akshat Wahi, Asst. Project Manager of roboVITics

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,211
On SlideShare
0
From Embeds
0
Number of Embeds
1,093
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

RoboCV Module 6: Hardware Interfacing using Visual C++

  1. 1. Using Visual C++Module 6 © Akshat Wahi | roboVITics, 2012 1
  2. 2. • The overall goal is to give a basic overview of how Serial Communications programming works using Windows API © Akshat Wahi | roboVITics, 2012 2
  3. 3. • Make sure to include “windows.h” header file. HANDLE hSerial; hSerial = CreateFile(TEXT("COM1“), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); © Akshat Wahi | roboVITics, 2012 3
  4. 4. • Now we have a HANDLE to the serial port, we need to set certain parameters for communication (baud rate, stopbits etc.)• Windows does this through a struct called DCB . DCB dcbSerial = {0}; dcbSerial.DCBlength=sizeof(dcbSerial); if (!GetCommState(hSerial, &dcbSerial)) { //error getting state } dcbSerial.BaudRate=CBR_19200; dcbSerial.ByteSize=8; dcbSerial.StopBits=ONESTOPBIT; dcbSerial.Parity=NOPARITY; if(!SetCommState(hSerial, &dcbSerial)){ //error setting serial port state } © Akshat Wahi | roboVITics, 2012 4
  5. 5. • Now we have an open serial port with correct parameters, so we can begin writing data . char data =‘f’; DWORD byteswritten=0; if(!WriteFile(hSerial,&data,1,&byteswritten,NULL)){ //error occurred ,data not written } © Akshat Wahi | roboVITics, 2012 5
  6. 6. • Reading is similar to writing we use ReadFile() char data; DWORD bytesread=0; if(!ReadFile(hSerial,&data,1,&bytesread,NULL)){ //error occurred ,data not written } © Akshat Wahi | roboVITics, 2012 6
  7. 7. • Once you’re done using the serial port, make sure to close the HANDLE.• If you don’t weird things can happen. CloseHandle(hSerial); © Akshat Wahi | roboVITics, 2012 7
  8. 8. • http://msdn.microsoft.com/en-us/library/ms123401.aspx © Akshat Wahi | roboVITics, 2012 8
  9. 9. • Mayank Prasad President, roboVITics mayank@robovitics.in• Akshat Wahi Asst. Project Manager, roboVITics +91 909 250 3053 akshat@core.robovitics.in• Akash Kashyap President, TEC – The Electronics Club of VIT akash130791@gmail.com © Akshat Wahi | roboVITics, 2012 9

×