Click here to Skip to main content
16,005,169 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What is best Exe 2 Exe protocol? Pin
Henry miller10-Dec-04 6:31
Henry miller10-Dec-04 6:31 
GeneralVariable width fonts and GetTextExtent in CEdit Pin
bob169729-Dec-04 8:47
bob169729-Dec-04 8:47 
GeneralRe: Variable width fonts and GetTextExtent in CEdit Pin
bob169729-Dec-04 11:01
bob169729-Dec-04 11:01 
GeneralHTML Help window Message handling Pin
Member 15760289-Dec-04 8:44
Member 15760289-Dec-04 8:44 
Generalswap bytes in a file Pin
Anonymous9-Dec-04 8:09
Anonymous9-Dec-04 8:09 
GeneralRe: swap bytes in a file Pin
David Crow9-Dec-04 10:24
David Crow9-Dec-04 10:24 
GeneralRe: swap bytes in a file Pin
Anonymous9-Dec-04 10:29
Anonymous9-Dec-04 10:29 
GeneralSerial Communication ... problems! Pin
PPatrickFFM9-Dec-04 5:48
PPatrickFFM9-Dec-04 5:48 
Hi!

I was wondering if you could help me with something. I have to write code on serial communication for my computing class, and I was hoping you could take a look at it for me?
I can't figure out what the problem is. It's impossible to read data while the program is executed - there's no data in que then - but debugging it, it works fine ...?! I've also used WaitCommEvent, but it works like crap, too. It seems that actually no data arrives, just why?. Isn't it possible to see how much data is present and read exactly that amount? The interface hooked up at Rs232 works faultlessly. What else could cause such problems I'm confronted with?

I hope this and my English did make any sense to you Wink | ;)

Awaiting your response,

Pit

Maybe someone wanna take a closer look at my code. Here it is:
(Please let me know if I've made any terrible errors!)

<br />
#include "Com_Handle.h"<br />
<br />
unsigned char CRC(void);<br />
<br />
// Construction/Destruction<br />
// -------------------------<br />
// Initializing ELM<br />
<br />
Com_Port::Com_Port()<br />
{<br />
	Init_ELM();<br />
}<br />
<br />
Com_Port::~Com_Port()<br />
{<br />
	Close_Comport(DriverHandle);<br />
}<br />
<br />
//<br />
// Open_Comport<br />
// -------------<br />
<br />
HANDLE Com_Port::Open_Comport (DWORD Comport)<br />
{    <br />
    switch (Comport) <br />
	{<br />
        case 1:<br />
            strcpy (Com_Name, "COM1");<br />
            break;<br />
        case 2:<br />
            strcpy (Com_Name, "COM2");<br />
            break;<br />
		case 3:<br />
            strcpy (Com_Name, "COM3");<br />
            break;<br />
		case 4:<br />
            strcpy (Com_Name, "COM4");<br />
            break;<br />
        default:<br />
            return 0;<br />
    }<br />
<br />
	<br />
	DriverHandle = CreateFile (Com_Name,<br />
							GENERIC_READ | GENERIC_WRITE,<br />
                               0,<br />
							   NULL,<br />
							   OPEN_EXISTING,<br />
							   0,<br />
							   NULL);<br />
							   <br />
    <br />
    if (DriverHandle == INVALID_HANDLE_VALUE) <br />
	{<br />
		Get_Error();<br />
		return (FALSE);<br />
    }<br />
	return DriverHandle;<br />
}<br />
<br />
//<br />
// Config_ComPort<br />
// ---------------<br />
<br />
int Com_Port :: Config_ComPort(HANDLE DriverHandle, DWORD BaudRate)<br />
{<br />
	cout << "Configuring ELM ..." << endl;<br />
	<br />
    switch (BaudRate)<br />
	{<br />
        case 110:<br />
			DCB_Baud_Rate = CBR_110;<br />
            break;            <br />
		case 300:<br />
			DCB_Baud_Rate = CBR_300;<br />
            break;            <br />
		case 600:<br />
			DCB_Baud_Rate = CBR_600;<br />
            break;            <br />
		case 1200:<br />
            DCB_Baud_Rate = CBR_1200;<br />
            break; <br />
		case 2400:<br />
            DCB_Baud_Rate = CBR_2400;<br />
            break; <br />
		case 4800:<br />
            DCB_Baud_Rate = CBR_4800;<br />
            break; <br />
		case 9600:<br />
            DCB_Baud_Rate = CBR_9600;<br />
            break; <br />
		case 14400:<br />
            DCB_Baud_Rate = CBR_14400;<br />
            break; <br />
		case 19200:<br />
            DCB_Baud_Rate = CBR_19200;<br />
            break;<br />
		case 38400:<br />
            DCB_Baud_Rate = CBR_38400;<br />
            break; <br />
		case 56000:<br />
            DCB_Baud_Rate = CBR_56000;<br />
            break; <br />
        default:<br />
            return 0;<br />
	}<br />
    <br />
<br />
<br />
	if(!SetupComm (DriverHandle, 1024, 1024))<br />
		Get_Error();<br />
<br />
        <br />
     if(!GetCommState (DriverHandle, &ELM_DCB))<br />
			Get_Error();<br />
<br />
     ELM_DCB.BaudRate = DCB_Baud_Rate;<br />
     ELM_DCB.fParity = 0;<br />
     ELM_DCB.fOutxCtsFlow = 0;<br />
     ELM_DCB.fOutxDsrFlow = 0;<br />
     ELM_DCB.fDtrControl = DTR_CONTROL_DISABLE;<br />
     ELM_DCB.fDsrSensitivity = FALSE;<br />
     ELM_DCB.fTXContinueOnXoff = 0;<br />
	 ELM_DCB.fOutX = 0;<br />
     ELM_DCB.fInX = 0;<br />
	 ELM_DCB.fErrorChar = 0;<br />
     ELM_DCB.fNull = 0;<br />
     ELM_DCB.fRtsControl = RTS_CONTROL_DISABLE;<br />
     ELM_DCB.fAbortOnError = 0;<br />
     ELM_DCB.ByteSize = 8;<br />
     ELM_DCB.Parity = NOPARITY;<br />
     ELM_DCB.StopBits = ONESTOPBIT;<br />
	 //ELM_DCB.EvtChar<br />
        <br />
     if(!SetCommState (DriverHandle, &ELM_DCB))<br />
		Get_Error();<br />
<br />
        <br />
      if(!SetCommMask (DriverHandle, EV_TXEMPTY))<br />
		Get_Error();<br />
<br />
	COMMTIMEOUTS sTo;<br />
<br />
		if(GetCommTimeouts(DriverHandle, &sTo))<br />
		{<br />
			sTo.ReadIntervalTimeout = 100;<br />
			sTo.ReadTotalTimeoutConstant = 100 ;<br />
			sTo.ReadTotalTimeoutMultiplier = 100;<br />
			sTo.WriteTotalTimeoutConstant = 0;<br />
			sTo.WriteTotalTimeoutMultiplier = 0;<br />
		}<br />
		else<br />
		{<br />
			return (FALSE);<br />
		}<br />
<br />
		if(!(SetCommTimeouts(DriverHandle, &sTo)))<br />
		{<br />
			Get_Error();<br />
		}<br />
	return 0; <br />
}<br />
<br />
<br />
//<br />
// Close_Comport<br />
// -------------<br />
<br />
<br />
int Com_Port :: Close_Comport (HANDLE DriverHandle)<br />
{<br />
    <br />
    if ((DriverHandle == 0) || (DriverHandle == INVALID_HANDLE_VALUE))<br />
    {<br />
		return FALSE;			<br />
	}<br />
    <br />
    CloseHandle (DriverHandle);<br />
	<br />
<br />
    return TRUE;<br />
}<br />
<br />
//<br />
// Write_Comport<br />
// -------------<br />
<br />
int Com_Port :: Write_Comport (HANDLE DriverHandle, DWORD NumBytes, void *Buffer)<br />
{<br />
    DWORD BytesWritten; <br />
	//DWORD EvtMask;<br />
    BOOL status;<br />
<br />
    if ((DriverHandle == 0) || (DriverHandle == INVALID_HANDLE_VALUE)) {<br />
        return FALSE;<br />
    } <br />
<br />
    <br />
    status = WriteFile (DriverHandle, Buffer, NumBytes, &BytesWritten, 0);<br />
    <br />
	//WaitCommEvent(DriverHandle, &EvtMask, NULL);<br />
 <br />
    return TRUE;<br />
}<br />
<br />
<br />
<br />
//<br />
// Read_Comport<br />
// -------------<br />
<br />
<br />
int Com_Port :: Read_Comport (HANDLE DriverHandle,<br />
							  DWORD *BytesRead,<br />
							  DWORD BufferSize,<br />
							  void *Buffer)<br />
{<br />
    COMSTAT     Our_Comstat;<br />
    <br />
    if ((DriverHandle == 0) || (DriverHandle == INVALID_HANDLE_VALUE)) {<br />
        return (FALSE);<br />
    } <br />
<br />
    <br />
    ClearCommError (DriverHandle, &Com_Errors, &Our_Comstat);<br />
<br />
	//if(!SetCommMask(DriverHandle, EV_RXCHAR))<br />
	//	Get_Error();<br />
<br />
	//if(WaitCommEvent(DriverHandle, &EvtMask, NULL))<br />
	<br />
    // ReadFile() only if there is data.<br />
	cout << "QUE: " << Our_Comstat.cbInQue << endl;<br />
<br />
    if (Our_Comstat.cbInQue > 0) <br />
	{<br />
        //do not overrun the buffer<br />
        if (Our_Comstat.cbInQue > BufferSize)<br />
		{<br />
            BytesToRead = BufferSize;<br />
        } <br />
		else<br />
		{<br />
            BytesToRead = Our_Comstat.cbInQue;<br />
        }<br />
<br />
        <br />
        if (!ReadFile (DriverHandle, Buffer, BytesToRead, BytesRead, NULL))<br />
		{<br />
			cout << "Read FAIL!\n";<br />
		}<br />
    }<br />
	else<br />
	{<br />
        *BytesRead = 0;<br />
    }<br />
    return TRUE;<br />
}<br />
<br />
int Com_Port::Get_Error()<br />
{<br />
	Com_Errors = GetLastError();// can be deleted<br />
	cout << "An error occured: " << GetLastError() << endl << endl;<br />
	return 0;<br />
}<br />
<br />
unsigned int Com_Port::Port_No = 0;<br />
<br />
int Com_Port :: Init_ELM ()<br />
{	<br />
	cout << "Searching ComPort, please wait. \n\n";<br />
	do<br />
	{		<br />
		Port_No ++;<br />
		DriverHandle = Open_Comport (Port_No);<br />
	}<br />
	while(!DriverHandle && Port_No != 5);<br />
	if (!DriverHandle)<br />
	{<br />
		cout << "Unable to open port" << endl;<br />
		return 0;<br />
	}<br />
	cout << "Connected on " << Com_Name << endl;<br />
	Config_ComPort(DriverHandle, 9600);<br />
	ComHandle = DriverHandle; //take out!<br />
	return 0;<br />
}<br />
<br />
<br />
unsigned char CRC(void)<br />
{<br />
	unsigned char msg_buf[20], nBytes=0;<br />
	<br />
	unsigned char CRC_Reg=0xff;<br />
	unsigned char poly,i,j;<br />
	unsigned char Checksum=0;<br />
	unsigned char *Byte_point, Bit_point;<br />
<br />
	for (i=0, Byte_point=msg_buf; i<nBytes; i++, Byte_point++)<br />
	{<br />
		for (j=0, Bit_point=0x80 ; j<8; j++, Bit_point>>=1)<br />
		{<br />
			if (Bit_point & *Byte_point)<br />
			{<br />
				if (CRC_Reg & 0x80)<br />
					poly=1;<br />
<br />
				else<br />
					poly=0x1C;<br />
<br />
				CRC_Reg= ( (CRC_Reg << 1) | 1)^poly;<br />
			}<br />
			else<br />
			{<br />
				poly=0;<br />
<br />
				if (CRC_Reg & 0x80)<br />
					poly=0x1D;<br />
<br />
				CRC_Reg= (CRC_Reg << 1)^poly;<br />
			}<br />
		}<br />
	}<br />
	return ~CRC_Reg; //return calculated complement <br />
}<br />
<br />

GeneralRe: Serial Communication ... problems! Pin
bob169729-Dec-04 9:43
bob169729-Dec-04 9:43 
GeneralAdding controls to ActiveX project Pin
greekgoddj9-Dec-04 5:06
greekgoddj9-Dec-04 5:06 
GeneralSymbian OS Pin
Anonymous9-Dec-04 5:00
Anonymous9-Dec-04 5:00 
GeneralRe: Symbian OS Pin
toxcct9-Dec-04 21:50
toxcct9-Dec-04 21:50 
GeneralGraphics in C++ Pin
al_in_the_city9-Dec-04 4:49
al_in_the_city9-Dec-04 4:49 
GeneralDate Add Pin
si_699-Dec-04 4:26
si_699-Dec-04 4:26 
GeneralRe: Date Add Pin
Cedric Moonen9-Dec-04 4:36
Cedric Moonen9-Dec-04 4:36 
GeneralRe: Date Add Pin
David Crow9-Dec-04 5:40
David Crow9-Dec-04 5:40 
GeneralRe: Date Add Pin
si_6912-Dec-04 12:02
si_6912-Dec-04 12:02 
GeneralCheck if file exist Pin
Anonymous9-Dec-04 4:15
Anonymous9-Dec-04 4:15 
GeneralRe: Check if file exist Pin
Prakash Nadar9-Dec-04 4:29
Prakash Nadar9-Dec-04 4:29 
GeneralRe: Check if file exist Pin
si_699-Dec-04 4:35
si_699-Dec-04 4:35 
GeneralRe: Check if file exist Pin
basementman9-Dec-04 5:43
basementman9-Dec-04 5:43 
GeneralRe: Check if file exist Pin
David Crow9-Dec-04 5:48
David Crow9-Dec-04 5:48 
GeneralRe: Check if file exist Pin
toxcct9-Dec-04 21:51
toxcct9-Dec-04 21:51 
GeneralRe: Check if file exist Pin
David Crow14-Dec-04 3:12
David Crow14-Dec-04 3:12 
GeneralLooking for a control... Pin
Mike Zinni9-Dec-04 4:06
Mike Zinni9-Dec-04 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.