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!
Printable View
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:Quote:
Originally Posted by Aravindan
- 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
Thanks ! but I want to know whether it is possible to access the serial ports through eVC++ code .
sorry, misunderstood your question...Quote:
Originally Posted by Aravindan
here is some code that contains everything you need (i assume you're using mfc):
i think setting the timeouts is not necessary, but it may be helpful if your communicating with slow devices or whatever.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
}
}
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
Thanks a lot for ur effort. Unfortunately I am not using MFC here. Is there a way to accomplish that without using MFC ?
any more important details before i think about an answer to your question? ;)
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.Quote:
Originally Posted by Aravindan
i hope this helps :p
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?
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.
does this mean that you are satisfied with the code sample i gave you, or do you still need some more information :confused:
Thanks a lot mate! Ur code is fine!
you are welcome :wave:Quote:
Originally Posted by Aravindan
Can u send me ur email id?
mine is : aravindtherock AT yahoo DOT com
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++.Quote:
Originally Posted by pachas
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>.