Solved

Setting up, sending and recieving serial port

Posted on 2002-07-07
6
710 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
Viewers will learn how to use the Hootsuite Dashboard.
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now