Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question in serial communication in vc++

Status
Not open for further replies.

mankan

Programmer
Apr 17, 2002
3
CH
Hi,
Can anybody please help me.I am doing serial communication in vc++ and I have done the following:

createfile
DCB---parameter setting
writefile
readfile
close port.

But I would like to know whether I have to do DCB setting separately for transmission and reception? and I am not able to send the data...

If anyone has any code for the above that will be great help..

Thx in advance,
Mankan
 
Hi I am new in programming in Visual c++ but i have written a small programm which transmits integers to serial port.Below is the code of tree functions 1)CreateCommunication which open a serial port , 2)Send which sends integers to serial port and 3)Get whinh receives integers from serial.

Send
int CMC35ProjectDlg::Send(BYTE data)
{
///////////
//Begin
//////////

//Send Data to serial Port

DWORD dwBytes=0;
BYTE d=data;
int ret=d;
BYTE* sbuf=&d;
BOOL stest=WriteFile(hcom,sbuf,1,&dwBytes,NULL);

if ((stest&&dwBytes)==FALSE)
{
MessageBox("Connection between your PC and MC35 device connot be establish.\nPlease try again later.\nCheck the serail cabel.\n(ËÜèïò êáôÜ ôçí åêôÝëåóç ôçò óõíÜñôçóçò send).",
"Error!",MB_OK | MB_ICONSTOP);
ret=-1;

}
return ret;

//////
//End
//////

}

Get
int CMC35ProjectDlg::Get()
{
//Get Data from Serial

////////
//Start
////////

DWORD dwBytes=0;
int ret;
BYTE data;
BYTE* rbuf=&data;
BOOL rtest=ReadFile(hcom,rbuf,1,&dwBytes,NULL);
ret=data;
if ((rtest && dwBytes)==FALSE)
{
MessageBox("Cannot read from device MC35.\nPlease try gain later by pressing the request button.\nCheck the serial cable.\n(ËÜèïò óôçí åêôÝëåóç ôçò óõíÜñôçáçò get).",
"Error!",MB_OK | MB_ICONSTOP);
}
return ret;

//////
//End
//////
}



CreateFile
HANDLE CMC35ProjectDlg::CreateCommunication()
{
HANDLE handle;
handle=CreateFile((LPCTSTR)ComSelect,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (handle==INVALID_HANDLE_VALUE)
MessageBox("Can't open COM.\nPlease Close any application using it and try again.",
"Error!",MB_OK | MB_ICONSTOP);
else
{
DCB dcb;
BOOL getcom=GetCommState(handle,&dcb);

//Setup of BaudRate
if (BaudRate=="110")
dcb.BaudRate=110;
if (BaudRate=="300")
dcb.BaudRate=300;
if (BaudRate=="1200")
dcb.BaudRate=1200;
if (BaudRate=="2400")
dcb.BaudRate=2400;
if (BaudRate=="4800")
dcb.BaudRate=4800;
if (BaudRate=="9600 (Default)")
dcb.BaudRate=9600;
if (BaudRate=="19200")
dcb.BaudRate=19200;
if (BaudRate=="38400")
dcb.BaudRate=38400;
if (BaudRate=="57600")
dcb.BaudRate=57600;
if (BaudRate=="115200")
dcb.BaudRate=115200;

//Setup of DataBits
if (DataBits=="6")
dcb.ByteSize=6;
if (DataBits=="7")
dcb.ByteSize=7;
if (DataBits=="8 (Default)")
dcb.ByteSize=8;

//Setup of Parity
if (Parity=="None (Default)")
dcb.Parity=NOPARITY;
if (Parity=="Odd")
dcb.Parity=ODDPARITY;
if (Parity=="Even")
dcb.Parity=EVENPARITY;
if (Parity=="Mark")
dcb.Parity=MARKPARITY;
if (Parity=="Space")
dcb.Parity=SPACEPARITY;

//Setup of StopBits
if (StopBits=="1")
dcb.StopBits=ONESTOPBIT;
if (StopBits=="1,5")
dcb.StopBits=ONE5STOPBITS;
if (StopBits=="2")
dcb.StopBits=TWOSTOPBITS;


BOOL setcom=SetCommState(handle,&dcb);
COMMTIMEOUTS times;
BOOL getcomt=GetCommTimeouts(handle,&times);
//BOOL setcomt=TRUE;

times.ReadIntervalTimeout=MAXDWORD;
times.ReadTotalTimeoutMultiplier=MAXDWORD;
times.ReadTotalTimeoutConstant=300; //wait 300msec
times.WriteTotalTimeoutMultiplier=0;
times.WriteTotalTimeoutConstant=300; //wait 300msec
BOOL setcomt=SetCommTimeouts(handle,&times);

if ((setcomt && getcom && setcom)==0)
{
MessageBox("COM can't be initialised.\nPlease restart your computer and retry.",
"Error!",MB_OK | MB_ICONSTOP);
CloseHandle(handle);
handle=INVALID_HANDLE_VALUE;
}
}
return handle;
}

void CMC35ProjectDlg::DestroyCommunication()
{
//Close Communication Port

//Destroy Communication
CloseHandle(hcom);

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top