Click here to Skip to main content
16,006,348 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: ERROR Pin
mitil203904823048-Dec-03 9:52
mitil203904823048-Dec-03 9:52 
GeneralRe: ERROR Pin
Christian Graus8-Dec-03 10:57
protectorChristian Graus8-Dec-03 10:57 
GeneralRe: ERROR Pin
mitil203904823049-Dec-03 8:38
mitil203904823049-Dec-03 8:38 
QuestionHow to bypass Outlook 2002 Security Object Model Guard Pin
Kevin McFarlane8-Dec-03 6:14
Kevin McFarlane8-Dec-03 6:14 
GeneralQuestion: Serial Plotting Pin
foghorn8-Dec-03 5:42
foghorn8-Dec-03 5:42 
GeneralRe: Question: Serial Plotting Pin
JWood8-Dec-03 10:48
JWood8-Dec-03 10:48 
GeneralRe: Question: Serial Plotting Pin
foghorn8-Dec-03 12:52
foghorn8-Dec-03 12:52 
GeneralRe: Question: Serial Plotting Pin
John M. Drescher8-Dec-03 13:18
John M. Drescher8-Dec-03 13:18 
Open the serial port using CreateFile. Read the data using ReadFile. Close the port using CloseHandle.

Here is some code to open the port:
class COMPort
{
public:
   COMPort() {
     m_bOpened=FALSE;
     m_hCom=INVALID_HANDLE_VALUE;
   }
   ~COMPort() {
     if ( m_hCom != INVALID_HANDLE_VALUE ) {
       CloseHandle(m_hCom);
     }
   }
   BOOL OpenSerialPort();
public:
   BOOL m_bOpened;
   HANDLE m_hCom;
};

COMPort::OpenSerialPort()
{
	if(!m_bOpened)
	{
		DCB    dcb;
		BOOL   fSuccess;
		COMMTIMEOUTS CommTimeouts;
		
		m_hCom = CreateFile("COM2", 
			GENERIC_READ | GENERIC_WRITE, 
			0, NULL, 
			OPEN_EXISTING,
			0, NULL);
		
		if(m_hCom == INVALID_HANDLE_VALUE)
		{
//			AtlTrace("Opening Serial Port Failed");
			return FALSE;
		} 
		
		fSuccess = GetCommState(m_hCom, &dcb);
		if(!fSuccess) 
		{
//			AtlTrace("GetCommState failed with error %d.\n", GetLastError());
			return FALSE;
		}
		
		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
		dcb.fBinary         = TRUE;
		dcb.fOutxCtsFlow    = FALSE;
		dcb.fOutxDsrFlow    = FALSE;
		dcb.fDsrSensitivity = FALSE;
		dcb.fOutX           = FALSE;
		dcb.fInX            = FALSE;
		dcb.fParity         = FALSE;
		dcb.fNull           = FALSE;
		dcb.fRtsControl     = RTS_CONTROL_DISABLE;
		dcb.fAbortOnError   = FALSE;
		
		fSuccess = SetCommState(m_hCom, &dcb);
		if(!fSuccess) 
		{
//			AtlTrace("SetCommState failed with error %d.\n", GetLastError());
			return FALSE;
		}
		
		GetCommTimeouts(m_hCom, &CommTimeouts);
		CommTimeouts.ReadIntervalTimeout = 20;
		CommTimeouts.ReadTotalTimeoutMultiplier = 20;
		CommTimeouts.WriteTotalTimeoutMultiplier = 0;
		CommTimeouts.WriteTotalTimeoutConstant = 100;
		fSuccess = SetCommTimeouts(m_hCom, &CommTimeouts);
		if(!fSuccess)
		{
//			AtlTrace("SetCommTimeouts failed");
			return FALSE;
		}
		
		PurgeComm(m_hCom, PURGE_TXABORT | PURGE_RXABORT);
		
		m_bOpened = TRUE; 
	}	
	return m_bOpened;
}
Here is code to read the data:
COMPort::SomeMemberFunction()
{
   DWORD num;
   unsigned char data[10];
   if(m_bOpened) ReadFile(m_hCom, &data[0], 1, &num, NULL);
}
John
GeneralRe: Question: Serial Plotting Pin
JWood8-Dec-03 14:21
JWood8-Dec-03 14:21 
GeneralRe: Question: Serial Plotting Pin
JWood8-Dec-03 14:51
JWood8-Dec-03 14:51 
GeneralRe: Ritch Edit Control Printing: What is wrong with my code Pin
Peter Molnar8-Dec-03 13:42
Peter Molnar8-Dec-03 13:42 
GeneralFunction Pin
Anonymous8-Dec-03 5:12
Anonymous8-Dec-03 5:12 
GeneralRe: Function Pin
BadJerry8-Dec-03 6:22
BadJerry8-Dec-03 6:22 
GeneralRe: Function Pin
David Crow8-Dec-03 7:54
David Crow8-Dec-03 7:54 
GeneralRe: Function Pin
Jörgen Sigvardsson8-Dec-03 8:30
Jörgen Sigvardsson8-Dec-03 8:30 
GeneralRe: Function Pin
Christian Graus8-Dec-03 9:41
protectorChristian Graus8-Dec-03 9:41 
GeneralRe: Function Pin
John M. Drescher8-Dec-03 9:42
John M. Drescher8-Dec-03 9:42 
GeneralRe: Function Pin
Christian Graus8-Dec-03 9:43
protectorChristian Graus8-Dec-03 9:43 
GeneralRe: Function Pin
John M. Drescher8-Dec-03 9:49
John M. Drescher8-Dec-03 9:49 
GeneralRe: Function Pin
David Crow8-Dec-03 10:19
David Crow8-Dec-03 10:19 
GeneralRe: Function Pin
John M. Drescher8-Dec-03 10:25
John M. Drescher8-Dec-03 10:25 
GeneralRe: Function Pin
Jörgen Sigvardsson8-Dec-03 13:11
Jörgen Sigvardsson8-Dec-03 13:11 
GeneralCircular linked list puzzle Pin
melwyn8-Dec-03 4:01
melwyn8-Dec-03 4:01 
GeneralRe: Circular linked list puzzle Pin
Ian Darling8-Dec-03 4:35
Ian Darling8-Dec-03 4:35 
GeneralRe: Circular linked list puzzle Pin
melwyn8-Dec-03 4:57
melwyn8-Dec-03 4:57 

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.