CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 68
  1. #1
    Join Date
    Mar 2005
    Location
    Bangalore, India
    Posts
    20

    Serial Port Communication in WinCE Emulator using eVC++

    Here's my doubt! Iam working on Embedded VC++ and I need to access my
    systems Serial Port through Standard SDK WinCE Simulator.
    Is it possible? Can you help me out?
    Thanks in advance!

  2. #2
    Join Date
    Jan 2004
    Location
    Upper Austria
    Posts
    215

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Quote Originally Posted by Aravindan
    Here's my doubt! Iam working on Embedded VC++ and I need to access my
    systems Serial Port through Standard SDK WinCE Simulator.
    Is it possible? Can you help me out?
    Thanks in advance!
    here we go:
    - select "Configure Platform Manager..." from studio's "tools" menu
    - in the treeview (pops up), select the emulator device you are using
    - click "Properties..."
    - in the "Device Properties" dialog, click "Configure..." next to the "Startup Server" dropdown (strange caption, isn't it? )
    - in the "Emulation Configuration Settings" dialog you can configure which serial ports are to be used
    reboot, and everything is good

    visit my website at hochleistung.at

  3. #3
    Join Date
    Mar 2005
    Location
    Bangalore, India
    Posts
    20

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Thanks ! but I want to know whether it is possible to access the serial ports through eVC++ code .

  4. #4
    Join Date
    Jan 2004
    Location
    Upper Austria
    Posts
    215

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Quote Originally Posted by Aravindan
    Thanks ! but I want to know whether it is possible to access the serial ports through eVC++ code .
    sorry, misunderstood your question...

    here is some code that contains everything you need (i assume you're using mfc):

    Code:
    	// NOTE: setErrMsg sets some error message for the class this code belongs to
    
    	handle m_hComm;
    
    	// open interface to reader
    	m_hComm = CreateFile(_T("COM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
    	if (m_hComm == INVALID_HANDLE_VALUE)
    	{
    		setErrMsg("Serial port could not be opened. Wrong port number?");
    		m_hComm = NULL;
    		return; // error opening com port
    	}
    	else
    	{
    		COMMTIMEOUTS noblock;
    		DCB dcb;
    
    		// set communication timeout
      		GetCommTimeouts(m_hComm, &noblock); // get communication timeouts
    		// get answer (ReadFile waits for answer until timeout)
    		// i used this timeouts in this example, because it is easier to handle
    		// (i do not need any timer)
    		noblock.ReadTotalTimeoutConstant = 50; // 50 milliseconds timeout
    		noblock.ReadTotalTimeoutMultiplier = MAXDWORD;
    		noblock.ReadIntervalTimeout = MAXDWORD;
    		if (SetCommTimeouts(m_hComm, &noblock) == 0) // set communication timeouts
    		{
    			setErrMsg("Serial port could not be initialized. Error while setting communication timeouts.");
    			CloseHandle(m_hComm);
    			m_hComm = NULL;
    			return; // error opening com port
    		}
    
    		// set communication state
    		GetCommState(m_hComm, & dcb);
    		dcb.BaudRate = 9600;
    		dcb.ByteSize = 8;
    		dcb.fParity = FALSE;
    		dcb.StopBits = ONESTOPBIT;
    		dcb.fDtrControl = DTR_CONTROL_ENABLE;
    		dcb.fRtsControl = RTS_CONTROL_DISABLE;
    
    		if (SetCommState(m_hComm, &dcb) == 0)
    		{
    			setErrMsg("Serial port could not be initialized. Error while setting communication state.");
    			CloseHandle(m_hComm);
    			m_hComm = NULL;
    			return; // error opening com port
    		}
    
    		CHAR pcVersion[20];
    		memset(pcVersion, '\0', 20);
    		DWORD dwByteCount = 0;
    		if (ReadFile(m_hComm, pcVersion, 20, &dwByteCount, NULL) != 0)
    		{
    			setErrMsg("Connection to reader established.");
    		}
    
    		WriteFile(m_hComm, "S", 1, &dwByteCount, NULL); // stop the scanmode ????
    		ReadFile(m_hComm, pcVersion, 20, &dwByteCount, NULL); // read the firmware version
    
    		if (strncmp(pcVersion, "ISO", 3) != 0)
    		{
    			setErrMsg("An der angegebenen Schnittstelle ist kein Reader angeschlossen.");
    			CloseHandle(m_hComm);
    			m_hComm = NULL;
    			return; // error opening com port
    		}
    	}
    i think setting the timeouts is not necessary, but it may be helpful if your communicating with slow devices or whatever.

    most important for the serial communication:
    - CreateFile (don't forget the ":" in "COM1:")
    - SetCommState
    - ReadFile
    - WriteFile
    - CloseHandle

    look them up in msdn to see what they do exactly
    reboot, and everything is good

    visit my website at hochleistung.at

  5. #5
    Join Date
    Mar 2005
    Location
    Bangalore, India
    Posts
    20

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Thanks a lot for ur effort. Unfortunately I am not using MFC here. Is there a way to accomplish that without using MFC ?

  6. #6
    Join Date
    Jan 2004
    Location
    Upper Austria
    Posts
    215

    Re: Serial Port Communication in WinCE Emulator using eVC++

    any more important details before i think about an answer to your question?
    reboot, and everything is good

    visit my website at hochleistung.at

  7. #7
    Join Date
    Mar 2005
    Location
    Bangalore, India
    Posts
    20

    Re: Serial Port Communication in WinCE Emulator using eVC++

    HI Leo!

    What does "An der angegebenen Schnittstelle ist kein Reader angeschlossen." mean?
    Its given in the final version check in your code.

    Thanks,
    Aravind.

  8. #8
    Join Date
    Jan 2004
    Location
    Upper Austria
    Posts
    215

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Quote Originally Posted by Aravindan
    HI Leo!

    What does "An der angegebenen Schnittstelle ist kein Reader angeschlossen." mean?
    Its given in the final version check in your code.

    Thanks,
    Aravind.
    oops... that's german and it means "There's no reader connected to the given interface" where reader means the device i'm trying to communicate with.

    i hope this helps

    i did not yet have the time to look for a non-mfc way of using the com-port, sorry for that. maybe there's some kind of stream (like the standard-streams "cin" and "cout") for that?
    reboot, and everything is good

    visit my website at hochleistung.at

  9. #9
    Join Date
    Mar 2005
    Location
    Bangalore, India
    Posts
    20

    Unhappy Re: Serial Port Communication in WinCE Emulator using eVC++

    Hi Leo,

    Thanks for your quick response man!
    MFC is fine.
    All i need to do is to access the computers serial port in a WCE application created through eMbedded VC++.
    The program is run in 'Win32(WCE emulator) Debug ' in
    "STANDARDSDK Emulator"

    Regards,
    Aravind.
    Many people can only keep on fighting when they expect to win. I'm not like that, I always expect to lose. I fight anyway, and sometimes I win!

  10. #10
    Join Date
    Jan 2004
    Location
    Upper Austria
    Posts
    215

    Re: Serial Port Communication in WinCE Emulator using eVC++

    does this mean that you are satisfied with the code sample i gave you, or do you still need some more information
    reboot, and everything is good

    visit my website at hochleistung.at

  11. #11
    Join Date
    Mar 2005
    Location
    Bangalore, India
    Posts
    20

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Thanks a lot mate! Ur code is fine!
    Many people can only keep on fighting when they expect to win. I'm not like that, I always expect to lose. I fight anyway, and sometimes I win!

  12. #12
    Join Date
    Jan 2004
    Location
    Upper Austria
    Posts
    215

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Quote Originally Posted by Aravindan
    Thanks a lot mate! Ur code is fine!
    you are welcome
    reboot, and everything is good

    visit my website at hochleistung.at

  13. #13
    Join Date
    Mar 2005
    Location
    Bangalore, India
    Posts
    20

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Can u send me ur email id?
    mine is : aravindtherock AT yahoo DOT com
    Many people can only keep on fighting when they expect to win. I'm not like that, I always expect to lose. I fight anyway, and sometimes I win!

  14. #14
    Join Date
    Mar 2005
    Posts
    34

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Hi!
    I'm trying to do something similar in eMbedded Visual C++.
    Is it the same code?
    Where must I put it??? In wich class (xxxxxxApp or xxxxxxDlg)??? (I want to create a dialog).

    I'm compiling it but it gives me a lot of errors beginning for the 'handle' expresion.
    Please, reply ASAP. A lot of Thanks!

  15. #15
    Join Date
    Jan 2004
    Location
    Upper Austria
    Posts
    215

    Re: Serial Port Communication in WinCE Emulator using eVC++

    Quote Originally Posted by pachas
    Hi!
    I'm trying to do something similar in eMbedded Visual C++.
    Is it the same code?
    Where must I put it??? In wich class (xxxxxxApp or xxxxxxDlg)??? (I want to create a dialog).

    I'm compiling it but it gives me a lot of errors beginning for the 'handle' expresion.
    Please, reply ASAP. A lot of Thanks!
    the code i posted is for embedded vc++.

    in order to get this code running, it is necessary, that your project supports MFC! you should enable mfc support while you create the project using the project wizard. of course, you could add mfc support at any time, but don't ask me which includes and libraries you need...

    concerning your 'handle' problem: i just had a look into my code and recognized, that i wrote 'HANDLE' (capital letters). try it.
    if this doesn't help, you may be missing mfc (HANDLE is defined in winnt.h which is included with the mfc support - as far as i remember...).

    to answer your question where to put the code: it depends. i would suggest to put all functions which deal with communication through the serial port into one class and then including and instantiating that one whenever you need it.
    for example, i put all my functions to connect to, read from, write to, close the connection to my serial device into one class. then i added an object of that class to my App as a public member, so i can access the device from anywhere in my code using AfxGetApp()->m_myDeviceObject.<any function>.
    Last edited by reset-leo; April 1st, 2005 at 05:22 AM.
    reboot, and everything is good

    visit my website at hochleistung.at

Page 1 of 5 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured