Solved

Setting up, sending and recieving serial port

Posted on 2002-07-07
6
718 Views
Last Modified: 2013-11-15
I am using DEV-C++ 4 complier. Tried using inport() and outport() for communicating the serial port, but it return compling error. Later found out that WIN32 API is needed. Can someone tell me how to implement WIN32 API into C++ programming. If possiable provide some examples for setting up the serial port communications...

Thanks
0
Comment
Question by:PsychoMantis
6 Comments
 
LVL 3

Expert Comment

by:jlsjls
ID: 7137037
In order to use the serial port in Win32, you need indeed the Win32-API.
Here you'll find the different steps in using a serial port (f.e. COM1).
1. Create handle to serial port and set communication params :
HANDLE hPort;
DCB dcb;
hPort= ::CreateFile( Device, GENERIC_READ | GENERIC_WRITE,
                0,                    // exclusive access
                NULL,                 // no security attrs
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL |
                0,
                NULL );

if (hPort== INVALID_HANDLE_VALUE)
{
     DWORD dwLastError;    
     dwLastError = ::GetLastError();
     return FALSE;
}

::GetCommState(hBanksys,&dcb);
dcb.BaudRate = CBR_9600;
dcb.fParity =TRUE;
dcb.ByteSize = 8;
dcb.Parity = EVENPARITY;
dcb.StopBits = TWOSTOPBITS;
::BuildCommDCB(Port+":"+PortSettings,&dcb);
::SetCommState(hPort,&dcb);

2. Use the serial port (read and write) :µ
READ :
COMMTIMEOUTS CommTimeout;
BOOL bResult;
DWORD dwBytesRead;

CommTimeout.ReadIntervalTimeout = 100;
CommTimeout.ReadTotalTimeoutMultiplier =1;
CommTimeout.ReadTotalTimeoutConstant = lTimeout;
::SetCommTimeouts(hPort,&CommTimeout);
     
bResult = ::ReadFile(hPort,(void *lpszText,lCount,&dwBytesRead,NULL);
if (bResult == FALSE)
{
return bResult;
}

WRITE :
COMMTIMEOUTS CommTimeout;
char szBuf[100];
DWORD dwLastError;

CommTimeout.WriteTotalTimeoutConstant = 2000;     // 2 seconds
CommTimeout.WriteTotalTimeoutMultiplier = 0;
::SetCommTimeouts(hPort,&CommTimeout);

bResult = ::WriteFile(hPort,lpszOutString,lLength,&dwBytesWritten,NULL);
if (bResult == FALSE)
{
   dwLastError = ::GetLastError();
   return FALSE;
}

3. Stop using the serial port :
::CloseHandle(hPort);


Hope this helps.
jlsjls
0
 

Author Comment

by:PsychoMantis
ID: 7137956
Could you also show me what i need, for example include header for the win32 API... Thanks.. Cause i am confused on Win32 API. Win32API is just another porgramming lanaguae?
0
 
LVL 3

Accepted Solution

by:
jlsjls earned 110 total points
ID: 7137986
In order to use the specified methods you need to add
#include <windows.h>

These methods are part of the Win32 API which means
that these methods allow you to access the functionality
of Windows OS. You must see Win32 API as a set of functions
by whom you can control the OS.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 86

Expert Comment

by:jkr
ID: 7138175
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp ("Serial Communications in Win32") - this is a comprehensive tutorial & comes with sample code...
0
 
LVL 4

Expert Comment

by:mblat
ID: 7138341
Since you have C++ compiler try this one:

http://www.naughter.com/serialport.html


should work fine for u...

Hope it helps...
0
 

Author Comment

by:PsychoMantis
ID: 7139248
Thanks  jlsjls the points is yours!
Finally my confuse ends...after days of search :)
Thanks again guys!
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 132
stamp tool on acrobat 1 39
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 142
FreeFileSync Batch Files 1 8
A list of useful business intelligence software.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question