Serial port communication in VC++

S

Thread Starter

Sylvia

I want to write a serial communication program by using VC++ 6.0. Could anyone give some idea of how to write it? Thanks a lot!!
 
R
Log on to Amazon.com and buy a couple of their books on Serial Port Programming in C and C++. First of all, the examples included on disk (CDROM) will help immensly, and secondly, there "ain't no simple answer" to your question.
 
S

Shreekrishna P

Open MSDN.Search for "Serial Communication in Win32". The author has provided a sample code also. Hint : Search String is "MTTTY"
 
Hello i want to write one program that gives me data before it goes in to modem driver from serial port.. Please help me
 
B
Ya I will help you .There is a activex control named MSCOMM 32.ocx .You can add that control to your project from component gallery and use the
functions provided by control.It has its functions SetSettings to set baud rate,stopbits character bits etc.Create obj of that control.
You can set RTC/RTX through the properties of control.

Another method is by using VC++ funs.
Create file fun will return a handle.
you will find the funs in MSDN.
Create file ()
SetupComm()
SetCommTimeouts()
SetCommState()
are funs mostly used u cann find help in MSDN regarding using it.
If any difficulty

Contact
[email protected]
 
> Log on to Amazon.com and buy a couple of their books on Serial Port Programming in C and C++. First of all, the examples included on disk (CDROM) will help immensly, and secondly, there "ain't no simple answer" to your question.
 
Pl provide me the details of books from TMH & BPB with Book Title/Author & if possible ISDN no. etc.
 
Sure!

Try my code ;)

bye

------------------------------
int pleinEcran(void);
int petitEcran(void);
int SetPortCom(void);
HANDLE hCom; //handle du port série



void CRS232Dlg::OnOpen()
{
pleinEcran();
}

void CRS232Dlg::OnClose()
{
petitEcran();
}



// *----------------------------------------------------------------*
// *--------------------------=CODE ROMAIN=-------------------------*
// *----------------------------------------------------------------*


int pleinEcran(void) //fait le 18/07/2001
{

//Declaration des chaines de caracteres, boitier sur "ON"
unsigned char BufferOuvrir[] = "3F,3F,3F,3F,3F,3F,3F,3F,3F,3F,3F,3F,\
00,07,10,D0,00,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
10,D7";

SetPortCom(); //Set port
DWORD length;
WriteFile(hCom, &BufferOuvrir, 1, &length, NULL);
MessageBox (NULL,"ok!","",NULL);
return length;
}

int petitEcran(void)
{
//Declaration des chaines de caracteres, boitier sur "OFF"
unsigned char BufferFermer[] = "3F,3F,3F,3F,3F,3F,3F,3F,3F,3F,3F,3F,\
00,07,10,D4,00,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
10,DB";
CString strTest; // Convertion cahr-string
strTest = BufferFermer[5];


SetPortCom(); //Set port
DWORD length;
WriteFile(hCom, &BufferFermer, 1, &length, NULL);
MessageBox (NULL,strTest,"",NULL);
return length;
}

// --------------------------------------------------
// Reglages du port pour commuter le RS232 du boitier
// --------------------------------------------------

int SetPortCom(void)
{
CreateFile("com1", GENERIC_READ|GENERIC_WRITE,0,0, OPEN_EXISTING,0,NULL); //com1

// Set timeouts, gestion du délai
DCB dcb;
COMMTIMEOUTS CommTimeOuts ;
CommTimeOuts.WriteTotalTimeoutMultiplier = 2;
CommTimeOuts.WriteTotalTimeoutConstant = 100;
if ( !SetCommTimeouts( hCom, &CommTimeOuts )){
CloseHandle(hCom);
return 2;} // Erreurs SetCommTimeouts

// Set DCB, Binary Coded Decimal (parametres du port, ici le com1)
memset(&dcb, 0, sizeof(dcb));

dcb.DCBlength = sizeof(dcb); //le dcb est la structure du port, ici on le parametre
dcb.BaudRate = 9600; // bauds courant
dcb.fBinary = 1; // mode binaire, sans check EOF
dcb.Parity = NOPARITY; // Parité
dcb.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
dcb.ByteSize = 8; // nombre de bits, 4 ou 8
return 0;
}


 
Open MSDN.Search for "Serial Communication in Win32". The author has provided a sample code also. Hint : Search String is "MTTTY"
 
would you give any information about how we can
use mscomm32.ocx in VC++ 6.0 by a simple program
in VC++ 6.0 and it manual? Thanks very much.
 
F

farid reza fani

Hi sir,
I want some information about serial port programming in vc++.I am in a robodeminer team
and I think for data translation to hardware of
our robot vc++ is the best high level language.
what is your idea?
 
S

shloke hajela

hello
can you please mail me any C code for sending bytes to the serial port using VC++ at desired baud rate.your given code is not getting
compiled on vc++.
thanks
shloke hajela
IIT KANPUR
INDIA
 
R

R.Lakshmanan

Can you tell where can I look for MSCOMM activex help.
What function should I use to transmit a string

regards

R.Lakshmanan
 
M

Madhur Sharma

<pre>
#include <windows.h>
#include "tty.h"

/***********************************************************************

PortInitialize (LPTSTR lpszPortName)

***********************************************************************/
BOOL PortInitialize (LPTSTR lpszPortName)
{
DWORD dwError,
dwThreadID;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;

// Open the serial port.
hPort = CreateFile (lpszPortName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy

// If it fails to open the port, return FALSE.
if ( hPort == INVALID_HANDLE_VALUE )
{
// Could not open the port.
MessageBox (hMainWnd, TEXT("Unable to open the port"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}

PortDCB.DCBlength = sizeof (DCB);

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking.
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement.
PortDCB.fNull = FALSE; // Disable null stripping.
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error.
PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2

// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hPort, &PortDCB))
{
// Could not configure the serial port.
MessageBox (hMainWnd, TEXT("Unable to configure the serial port"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}

// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (hPort, &CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;

// Set the time-out parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
// Could not set the time-out parameters.
MessageBox (hMainWnd, TEXT("Unable to set the time-out parameters"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}

// Direct the port to perform extended functions SETDTR and SETRTS.
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
EscapeCommFunction (hPort, SETDTR);
EscapeCommFunction (hPort, SETRTS);

// Create a read thread for reading data from the communication port.
if (hReadThread = CreateThread (NULL, 0, PortReadThread, 0, 0,
&dwThreadID))
{
CloseHandle (hReadThread);
}
else
{
// Could not create the read thread.
MessageBox (hMainWnd, TEXT("Unable to create the read thread"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}

return TRUE;
}


/***********************************************************************

PortWrite (BYTE Byte)

***********************************************************************/
void PortWrite (BYTE Byte)
{
DWORD dwError,
dwNumBytesWritten;

if (!WriteFile (hPort, // Port handle
&Byte, // Pointer to the data to write
1, // Number of bytes to write
&dwNumBytesWritten, // Pointer to the number of bytes
// written
NULL)) // Must be NULL for Windows CE
{
// WriteFile failed. Report error.
dwError = GetLastError ();
}
}



/***********************************************************************

PortReadThread (LPVOID lpvoid)

***********************************************************************/
DWORD PortReadThread (LPVOID lpvoid)
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;

// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);

while (hPort != INVALID_HANDLE_VALUE)
{
// Wait for an event to occur for the port.
WaitCommEvent (hPort, &dwCommModemStatus, 0);

// Re-specify the set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING);

if (dwCommModemStatus & EV_RXCHAR)
{
// Loop for waiting for the data.
do
{
// Read the data from the serial port.
ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0);

// Display the data read.
if (dwBytesTransferred == 1)
ProcessChar (Byte);

} while (dwBytesTransferred == 1);
}

// Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);

// Set the indicator lights.
SetLightIndicators (dwCommModemStatus);
}

return 0;
}


/***********************************************************************

PortClose (HANDLE hCommPort)

***********************************************************************/
BOOL PortClose (HANDLE hCommPort)
{
DWORD dwError;

if (hCommPort != INVALID_HANDLE_VALUE)
{
// Close the communication port.
if (!CloseHandle (hCommPort))
{
dwError = GetLastError ();
return FALSE;
}
else
{
hCommPort = INVALID_HANDLE_VALUE;
return TRUE;
}
}

return FALSE;
}
</pre>
above prog. is for serial comm. on windows at port com1 . u can change the port no. as per ur requirment . for further info. u can contect
me at my mail-id.<br>
madhur sharma
 
HI Madhur,

I have one software which accesses data only through COM PORT. I want to sent some DATA to this Software using same PC on which it is loaded. Is it possible to sent data to that software using VC++ ?

If anybody knows plzz contact me on [email protected]
 
Top