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>.
Thank's
Now, It appears the following error:
xxxxx.cpp(88) : error C2065: 'setErrMsg' : undeclared identifier
(You say in the code "// NOTE: setErrMsg sets some error message for the class this code belongs to" but , how can I avoid it??)
my 'NOTE:' was supposed to tell you, that setErrMsg is a function that i implemented somewhere in my class but did not post. you could for example replace each appearance of setErrMsg() with a call to AfxMessageBox().Quote:
Originally Posted by pachas
for example, instead of:
setErrMsg("Serial port could not be initialized. Error while setting communication timeouts.");
you could write:
AfxMessageBox(_T("serial port..."));
Thank's!!!!
It's beginin to run ........ (or that seems to be)
A lot of thank's!!!
you're welcome.Quote:
Originally Posted by pachas
Hi!
I need to read information from a serial device which downloads the information from barcode terminals. The serial mechanism (that is conected to the computer by the COM1 port) downloads the information (arrays of numbers) from the barcode terminal and I have to read it. This mechanism a old one. I'm using the WinCE emulator (and I have configured it).
So I change some lines in the code you (RESET-LEO) give us, but it doesn't read: the array where I read is empty.
I will be very grateful if someone can't find the possible error or give me some advice. The code I have is the following:
Quote:
HANDLE m_hComm; //Gets the operating system file handle for the file that the current FileStream object encapsulates.
// open interface to reader
m_hComm = CreateFile(_T("COM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (m_hComm == INVALID_HANDLE_VALUE)
{
AfxMessageBox(TEXT("Serial port could not be opened. Wrong port number?"), MB_OK);
m_hComm = NULL;
return FALSE; // error opening com port
}
else
{
COMMTIMEOUTS noblock; //set and query the time-out parameters for a communications device
DCB dcb; //The DCB structure defines the control setting for a serial communications device.
GetCommTimeouts(m_hComm, &noblock); // get communication timeouts
noblock.ReadTotalTimeoutConstant = 50;// 50 milliseconds timeout
noblock.ReadTotalTimeoutMultiplier = MAXDWORD;
noblock.ReadIntervalTimeout = MAXDWORD;
if (SetCommTimeouts(m_hComm, &noblock) == 0) // set communication timeouts
{
AfxMessageBox(TEXT("Serial port could not be initialized. Error while setting communication timeouts."), MB_OK);
CloseHandle(m_hComm);
m_hComm = NULL;
return FALSE; // error opening com port
}
// Initialize the DCBlength member.
dcb.DCBlength = sizeof (DCB); //MSDN
// set communication state
GetCommState(m_hComm, &dcb);
dcb.BaudRate = 19200;
dcb.fBinary = TRUE;
dcb.ByteSize = 7;
dcb.fParity = TRUE;
dcb.fBinary = TRUE; //MSDN
dcb.Parity = ODDPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fDtrControl = DTR_CONTROL_ENABLE;//Enables the DTR line when the device is opened and leaves it on.
dcb.fRtsControl = RTS_CONTROL_DISABLE;//Disables the RTS line when the device is opened and leaves it disabled.
if (SetCommState(m_hComm, &dcb) == 0)
{
AfxMessageBox(TEXT("Serial port could not be initialized. Error while setting communication state."), MB_OK);
CloseHandle(m_hComm);
m_hComm = NULL;
return FALSE; // error opening com port
}
CHAR pcVersion[20];
memset(pcVersion, '\0', 20);
DWORD dwByteCount = 0;
if (ReadFile(m_hComm, pcVersion, 20, &dwByteCount, NULL) != 0) //Devuelve !=0 si tiene exito
{
AfxMessageBox(TEXT("Connection to reader established."), MB_OK);
CString str(pcVersion);
AfxMessageBox(str, MB_OK);//IT DOESN'T PRINT ANYTHING
}
}
the only thing i could imagine is that your DCB settings contain a wrong setting. i would suggest to try if you can receive any data using hyperterminal and then coding the settings you chose there into your program (the dcb settings).
Hi!
I am working at something similar.
My problem is this:
I am using the WinCE Emulator in eVC++. In "Platform Manager" I have set the following "Communications":
Ethernet: NAT (Outgoing only)
Serial Port 1: COM4
Serial Port 2: None
Parallel Port: None
I am also using something called "Virtual Serial Port Kit" which allows me to create two virtual serial ports that are connected to each other. These are called COM2 and COM4. By starting two "HyperTerminal" windows I can see that COM2 and COM4 really are connected and both COM2 and COM4 is listed under "ports" in "Device Manager". So I'm pretty sure that the "Virtual Serial Port Kit" works.
When I try to run your (reset-leo) code (or my own which is pretty much the same) using "COM1:" it works fine but not when I use "COM4:" like this:
m_hComm = CreateFile(_T("COM4:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
In this case all I get is:
"Serial port could not be opened. Wrong port number?"
But COM4 should exist!
Any idea what the problem might be?
Very thankful for any help!
hard to find any failure at first sight.
evaluate the error returned by GetLastError() after your call to createfile. maybe you can get some more information on what exactly went wrong.
i assume you did, but anyway i want to remind you that you have to close all existing connections to the serial port before you can open any new. so if you're running hyperterm on com4, close it.
Yes I did. The fact is that when I started eVC++ and the Emulator with the mentioned settings (Serial Port 1: COM4) and then started Hyperterm (COM4) I got the message from hyperterm that "another program is using the service" like I should.Quote:
Originally Posted by reset-leo
And still I get "Serial port could not be opened. Wrong port number?"...
I'll check GetLastError() and report the results.
Btw, thanks for answering so quick. :)
GetLastError():
(55) The specified network resource or device is no longer available, (ERROR_DEV_NOT_EXIST)
So here COM4 does not exist but to hyperterm it does...
oh! now i know what's wrong! :)
your hyperterm connects directly to the local com4 of your desktop machine (which obviously works). BUT the serial port you connect to from the pocket pc emulator is not directly com4, but com1 which is mapped to the local com4 by the platform manager. got it? so simply use the connection to "COM1:" in your eVC++ app.
I tried that but it did not work either. But you got me on a new track so I thought I could try setting "Serial Port 2" to COM4 instead and, like you suggested, use:
m_hComm = CreateFile(_T("COM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
And that worked!!!
I don't know why really, but I definently think that you are right about the fact that CreateFile should use the "local" COM1 (which in fact is the real COM4).
But why "Serial Port 1" as COM4 and CreateFile(_T("COM1:")... did not work I can not understand.
Maybe the conclusion is that "Serial Port 2" is "local" COM1, but what is then "Serial Port 1"?
-----
Thanks for all the help! I've struggled with this for quite some time and now I can go home relived! :D
sometimes programming is like a puzzle... good that it works now!Quote:
Originally Posted by LA Kings
And, how can I do this (try if I can receive any data using hyperterminal)?Quote:
Originally Posted by reset-leo
Thank's
runQuote:
Originally Posted by pachas
start->all programs->tools->communication->hyperterminal
if it is not there, install it using windows setup ("software" in control panel).
hyperterminal will then guide you through the steps to establish the communication. you can change the baudrate settings for testing at any time by clicking on the "properties" icon in hyperterminal.
Sir,
I had an query on this program, I tried to execute this program onto my device but it is not working , is this only meant for emulators?
Regards
Naveen
not only for emulators, but should also work on any windows ce based device. what exactly is happening?Quote:
Originally Posted by chaps5555
First i want to tell u it is working fine with emulator. I have got pocket pc 2002 which has got serial port with its cradle.I want to acces the serial port and send and receive data from my hardware device . when i am debugging it on my device it is not able to read any thing.wht reallt hapeens if i senpto my device it responds with p back that shows transmission has occured . but when i am sending p it is not returning anyhting.but if i use it with emulator it is working fine.
What can be the problem.
Regards
Naveen
it's hard to tell what's wrong as i know nothing about the devices you're using.
again, i suggest to check all the settings in the code (DCB, ...) as well as the settings of the devices. i also recommend in such cases, to download some terminal application for wince and try how that one is behaving (search google for "vxHpc", there's a free trial download).
i could also imagine that there's some hardware incompatibility between the handheld and you device...
what is vxHpc? how will it help me.Can you tell me in my program i am opening Com1 that is com1 for my cpu that i through emulator.I attach me harware with a serial cable between my transreceiver and my pc ' Com1 which i am am opening through the program.But when i am downloading same program on real device wht should be the port number how will it recognise the cradles serial port.As i tried com1 to com40
UI am sending u piece of code
port.OpenPort("com1");
port.ConfigurePort(CBR_38400,8,0,NOPARITY ,ONESTOPBIT );
unsigned char data;
label1:
port.WriteByte('p');
port.WriteByte('q');
port.WriteByte('r');
::Sleep(5);
port.ReadByte(data);
port.ReadByte(data);
port.ReadByte(data);
at open .port i am opening com1 what should i write for pocket pc device 2002.
Regards
Naveen
first of all, your code did not help me much, as you seem to be using some (custom) software components that i don't know. what i'm trying to say is: what kind of object is your "port"????? :confused:
which com port you should use on your handheld device depends on the device itself! i dealt with devices where the port was no. 1, on others it was 6...
vxHpc is a terminal program that let's you connect via serial interface and send/receive characters (i.e. you type "p" and see what the other device returns).
!!!!!!!!! generally spoken, and that's true for everyone who's dealing with serial communication, YOU WILL NEED A TERMINAL PROGRAM AND MUST KNOW HOW TO USE IT! otherwise, finding errors will be very very hard at certain points. !!!!!!!!
finally, did you connect the device you're trying to communicate with directly to your handheld (or it's cradle), or did you try to connect to the device while it is connected to your cpu?
/miaou :sick:
i tried to connect the device ( it is juz a transreceiver with serial port with it.)directly to the cradle which has got serial port .My handheld dont have serial port its cradle has. i am not able to check wht is it number also is it com1 2 , or anyhting.
Regards
Naveen
again: try to connect to the device using a terminal program. when you were able to connect with the terminal program, continue testing with you self-written app.Quote:
Originally Posted by chaps5555
i am trying to connect the device through terminal program , it is juz showing intializing and not moving ahead .
regards
naveen
well, then there's something wrong in your serial connection.Quote:
Originally Posted by chaps5555
unfortunately, i don't think i can help you with this as i don't have the hardware you're using on my desk... consult the documentation of your devices for any further hints or search the web for some info on your transmitter.
sorry that i can not help you on that issue, but feel free to ask anything when you've got any questions concerning the code.
can i use the terminal program with my pocket pc and another pc wich does not have active sync.
regards
naveen
hey it worked
the code is really good but u require an additional hardware i.e null modem cable between pda serial port and the device .
http://www.waterlog.com/App%20Notes/...roduction.html
i am posting that this thread helps guyz in future.
And thanks to reset -leo for his generous help and support.
Regards
Naveen
i knew that there was something wrong with the serial connection... :cool:Quote:
Originally Posted by chaps5555
your welcome!
Hi,
I have a similar problem., but with some changes.
I need to comunicate through the serial connection of my PPC.
But i dont want to comunicate with the COM port of any PC or device.
I will have one switch (on/off, open/close) connected into my PPC (qtek 2020) through it's serial connection.
I just want to know if the switch is opened or closed.
Will i have to use the same code?? Open, read, write, close the COM port???
If i understood what all you had been saying, when you say Open, read, write, close the COM port you are talking about the PC COM port, not the PPC COM port, right???
Any one can help??
Thanks in advance,
Ventura
we were talking about the com (serial) port of the ppc. using mfc, there is no difference in the function calls to open, read, write, ... between pc and ppc.Quote:
Originally Posted by V3N7UR4
i think what may work is to connect one side of the switch to the tx pin of the serial port and the other side to the rx pin. then you send one character on the serial port (using Write()) and immediately call Read(). if read returns the character you sent, the switch is closed. if nothing is received, the switch is open!
i can in no way guarantee that this works, but it's the easiest thing i can imagine. of course, you may need some more electronics to connect the switch to the serial port, but i can not help you with that... don't blame me if you blow up your ppc's com port with such experiments ;)
Hi,
Many thanks for your quick reply reset-leo.
What i will have to trigger is something like this: http://www.beyondlogic.org/serial/serial.htm
I supose those values are for a desktop PC. If someone known the same values but for PPC, i'll be gratefull.
Ventura
if you only want to connect a switch, then (according to the document you sent) you should simply build a loopback plug where you put the switch into the connection between the send- and the receive cable.
i don't know what is your final goal in this, but for testing only, i would suggest to build this loopback device from scratch using a plug and some wires. then leave the ends of the wires connected to the rx and tx pin open (the switch is open) or put the open ends together (the switch is closed).
as i said, and as the document sais, if you put the wire-ends together to close the loop, you should be able to receive what you've sent immediately.
as long as you don't connect any power supplies to some of the pins in your cabeling, there should also be no damage to the com port...
btw: how would you connect to the device? does it offer a sub-d plug on a cradle or will you use the cf-slot? or something else?
Well, i'll try to explain the final goal.
I need to develop one dll using evc++. This dll will have to trigger the beavoir of one or to switches connected to my Qteck 2020 throught it's serial port.
I think DTR or CTS have one property like "enable" or "true" which means that some voltage are supplied to that pin.
I think i just have to develop a thread to continually watch some pins.
If the switch is closed, there are voltage. If the switch is open no voltage at that pin.
Something like i found here: http://www.control.com/1026195756/index_html
Clear??? Or not?? :)
sorry for the late reply...
through DTR and CTS it may be possible as well. if your switches are connected to that pins (mind the correct voltage as described in the first link you posted!), you could continously call GetCommState() and check the values of the DCB structure.
OK,
Many thanks for your replys reset-leo.
Now... another not expected realy BIG PROBLEM.
Unfortunely i was stolen!! My Qtek 2020 has now another owner... Shuinf!! :.(
Forget it!!
Is it possible to simulate the open/close state of the switches in the emulator???
Should i connect the switch to the COM port of my desktop and use it to simulate with de emulator of the evc++???
Will it work??
Many thanks!!!
Ventura