Solved

Setting up, sending and recieving serial port

Posted on 2002-07-07
6
706 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks  jlsjls the points is yours!
Finally my confuse ends...after days of search :)
Thanks again guys!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A list of useful business intelligence software.
If your app took Google’s lash recently, here are the 5 most likely reasons.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

763 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

7 Experts available now in Live!

Get 1:1 Help Now