Right now i am working with serial port programming in c on visual studio environment
i want to know about the program
it should read the data on com port and write the data in com port
it should include1) baud rate 2)comport selection 3) data that u wanted send
if any can help out these things with code would be appreciated
Thanks&Regards
chandu
And if you can only use windows APIs,you can try in this way.use CreateFile() to control the port.Following is the sourcecode. It opens the port, sets the Baud Rate,and you can use WriteFile() and ReadFile() functions to receive and send data.You can also search in MSDN for details.
Code:
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM2";
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_57600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
Last edited by jesschen; January 10th, 2008 at 03:36 PM.
i jesschen first of all thanx for your code.Now my problem sttement is tat i want to send char 'a' through the serial port in DevCpp windows.I modified your code and added some to do this,But it is not working can you please help me out?..
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <bios.h>
//#include <afxwin.h> // serial.cpp : Defines the entry point for the console application.
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
Thank you for posting this info. I am trying to migrate from POSIX to Windows. I am dealing with USB-serial devices. For each read operation I need to know the number of bytes read. But I saw this in the ReadFile() manual page http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
...
lpNumberOfBytesRead [out, optional]
A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter. ReadFile sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.
...
I believe serial port is asynchronous, but if I set the lpNumberOfBytesRead to NULL as recommended then how do I know the number of bytes read?
If hFile was opened with FILE_FLAG_OVERLAPPED, the following conditions are in effect:
* The lpOverlapped parameter must point to a valid and unique OVERLAPPED structure, otherwise the function can incorrectly report that the read operation is complete.
* The lpNumberOfBytesRead parameter should be set to NULL. Use the GetOverlappedResult function to get the actual number of bytes read. If the hFile parameter is associated with an I/O completion port, you can also get the number of bytes read by calling the GetQueuedCompletionStatus function.
A serial port is a very dumb device.
You MUST know the protocol you intend to use between the communicating software components. In many cases you are 'talking' to another computing device (scanner, sensor etc.). They will have a clearly defined specification (down to the actual number of bytes and the meaning of the values) for the commands and allowed responses you may expect to receive.
So. If the command to be sent is 'a' then SerialPutc(hcom, 'a') ONCE.
The device should already be switched on and 'listening' on the serial port.
It ought to respond to the 'a' command and send one of the EXPECTED responses.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.