Solved

Setting up, sending and recieving serial port

Posted on 2002-07-07
6
719 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stamp tool on acrobat 1 40
EDI Solution or comparable? 2 41
wireless name in LAN adapter 14 69
WinX Downloader fails to analyze. 10 55
Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

839 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