Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

Serial Port

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Manu - 11 May 2005 10:32 GMT
Hi,
i need to know how i can interface to RS232 with VC++.NET (MFC). In the old
VC++ 6.0 there is the MSCOMM32 component, i read i can use it but there are
some license problems. Is there some low level interface can i use??

Thank in advance.
William DePalo [MVP VC++] - 11 May 2005 15:24 GMT
> i need to know how i can interface to RS232 with VC++.NET (MFC). In the
> old VC++ 6.0 there is the MSCOMM32 component, i read i can use it but
> there are some license problems. Is there some low level interface can i
> use??

I don't know if MFC provides a class library that addresses serial
communication. The old version of Visual Studio provided a non-MFC sample.
You may still be able to download it at this link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample98/html
/vcsmpserialsampleforcommunicationsdemonstration.asp


In case you don't know, on Win32 every device is accessed by means of a
handle. Reads of the device are done by passing the handle to ReadFile() and
writes by passing the handle to WriteFile().

You will find a little _hack_ below which presumes the existence of a modem
on COM3, gets a handle to a the serial port, posts a read, sends the Hayes
command "identify" and reads the response.

It should get you started.

Regards,
Will

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
char         szBuffer[80];
DCB          dcb = {0};
DWORD        dwRead, dwWritten;
HANDLE       hComm;
OVERLAPPED   ovlr = {0}, ovlw = {0};
COMMTIMEOUTS cto;

// Create events for overlapped operation

ovlr.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ovlw.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

// Open the port

hComm = CreateFile("\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE,
           0, NULL, OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);

// Get the state of the device and modify it

dcb.DCBlength = sizeof(dcb);
GetCommState(hComm, &dcb);
dcb.BaudRate = CBR_9600;
SetCommState(hComm, &dcb);

// Set the timeout parameters (almost completely bogus here)

cto.ReadIntervalTimeout         = 1000;
cto.ReadTotalTimeoutConstant    = 1000;
cto.ReadTotalTimeoutMultiplier  = 1000;
cto.WriteTotalTimeoutConstant   = 1000;
cto.WriteTotalTimeoutMultiplier = 1000;

SetCommTimeouts(hComm, &cto);

// Send a command and receieve a response. Note that
//  we post the receive in advance of sending the
//   command in order not to miss anything

printf("\r\nSending: ATI1\r\n");

ReadFile (hComm, szBuffer, sizeof(szBuffer), &dwRead,    &ovlr);
WriteFile(hComm, "ATI1\r",  strlen("ATI1\r"),  &dwWritten, &ovlw);

// Wait for the receive to complete and display the response

if ( GetOverlappedResult(hComm, &ovlr, &dwRead, TRUE) )
{
 szBuffer[dwRead] = 0;
 printf("Received: %s\r\n", szBuffer);
}

// Close the device

CloseHandle(hComm);

return 0;
}
Jochen Kalmbach [MVP] - 11 May 2005 16:28 GMT
Hi Manu!

> i need to know how i can interface to RS232 with VC++.NET (MFC). In the old
> VC++ 6.0 there is the MSCOMM32 component, i read i can use it but there are
> some license problems. Is there some low level interface can i use??

For C++ you could use:

CSerialPort v1.03 - Serial Port Wrapper
http://www.codeproject.com/sys­tem/cserialport.asp

Serial library for C++
http://www.codeproject.com/sys­tem/serial.asp

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

Rodrigo Corral [MVP] - 11 May 2005 17:42 GMT
You always can use raw windows API.

Use CreateFile and open COMx file where x is the number of port to open.
Then I can use WriteFile and ReadFile functions...

You can configure the serial port using SetCommMask, SetupComm,
SetCommState, SetCommTimeouts...

Signature

Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.