Click here to Skip to main content
15,883,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, i want to develope an application on a windows mobile that communicate over serial port. i want to program it on c/c++.

this the code that i used
C++
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <assert.h>

int _tmain(int argc, _TCHAR* argv[])
{FILE *fp;

    HANDLE m_hUsbCom ;
LPTSTR portName = TEXT("COM4");

        m_hUsbCom = CreateFile (
                    portName ,                    // Pointer to the name of the port
                    GENERIC_READ | GENERIC_WRITE,    // Access (read/write) mode
                    0,                                // Share mode
                    NULL,                         // Pointer to the security attribute
                    OPEN_EXISTING,     // How to open the serial port
                    0,                                // Port attributes
                    NULL);                        // Handle to port with attribute to copy
  
 
        if ( m_hUsbCom == INVALID_HANDLE_VALUE ) 
        {
          fp = fopen("error.txt", "w+");
   fclose(fp);
        }

		else
		{
 fp = fopen("succed ", "w+");
   fclose(fp);
		}

    

}


The problem that i could'nt open the port ??? why
please help

What I have tried:

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <assert.h>

int _tmain(int argc, _TCHAR* argv[])
{FILE *fp;

HANDLE m_hUsbCom ;
LPTSTR portName = TEXT("COM4");

m_hUsbCom = CreateFile (
portName , // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy


if ( m_hUsbCom == INVALID_HANDLE_VALUE )
{
fp = fopen("error.txt", "w+");
fclose(fp);
}

else
{
fp = fopen("succed ", "w+");
fclose(fp);
}



}
Posted
Updated 10-May-16 18:09pm

Use the debugger. Moreover, as shown in the documentation page Configuring a Communications Resource (Windows)[^] call GetLastError to know about the reason of the failure.
 
Share this answer
 
Comments
Jammes_Ca 10-May-16 6:20am    
i did this
if ( m_hUsbCom == INVALID_HANDLE_VALUE )
{

DWORD err=GetLastError();



fp = fopen("error.txt", "w+");
fprintf(fp, "Error code= %0X\n", err);
fclose(fp);
}

else
{
fp = fopen("Succed ", "w+");
fclose(fp);
}

and i got error code = 2
what does it mean ??
CPallini 10-May-16 6:30am    
ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified.
Jammes_Ca 10-May-16 7:30am    
but with other application i could connect to port 4, so what is wrong ???
CPallini 10-May-16 7:37am    
What's the 'ther application'? Is the serial port busy (it cannot be shared on Windows)?
Jammes_Ca 10-May-16 7:43am    
i build an application with c#and it connect well send and read data, now i want to do an application with c++. but in c++ does'nt work. it an application for smart device pc pocket that run on windows mobile. i think on windows mobile i should add somthing befor or after "com4" maybe ike this "\\come4" i tried this but it didn"t work
Your instinct was correct, it is "COM4:", you left the poor little ":" out.

It is expressly told to you here:
CreateFile (Windows CE 5.0)[^]
Quote => points to a COM port to open, you must include a colon after the name

The other alternate format that is valid is "\\.\COM4" and that format allows you to go above COM9 the short format does not.

The final thing to check is COM4 actually exists :-)
 
Share this answer
 
v6
Comments
Jammes_Ca 10-May-16 11:25am    
Hey, the port COM4 existe, i told you i did an applciation with c# and it worked fine.

i tried this


#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <assert.h>

int _tmain(int argc, _TCHAR* argv[])
{FILE *fp;

HANDLE m_hUsbCom ;
LPTSTR portName = TEXT("COM4:");

m_hUsbCom = CreateFile (
portName , // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy


if ( m_hUsbCom == INVALID_HANDLE_VALUE )
{
fp = fopen("error.txt", "w+");
fclose(fp);
}

else
{
fp = fopen("succed ", "w+");
fclose(fp);
}



}

and this

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <assert.h>

int _tmain(int argc, _TCHAR* argv[])
{FILE *fp;

HANDLE m_hUsbCom ;
LPTSTR portName = TEXT("\\.\COM4");

m_hUsbCom = CreateFile (
portName , // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy


if ( m_hUsbCom == INVALID_HANDLE_VALUE )
{
fp = fopen("error.txt", "w+");
fclose(fp);
}

else
{
fp = fopen("succed ", "w+");
fclose(fp);
}



}

and this

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <assert.h>

int _tmain(int argc, _TCHAR* argv[])
{FILE *fp;

HANDLE m_hUsbCom ;
LPTSTR portName = TEXT("\\.\COM4:");

m_hUsbCom = CreateFile (
portName , // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy


if ( m_hUsbCom == INVALID_HANDLE_VALUE )
{
fp = fopen("error.txt", "w+");
fclose(fp);
}

else
{
fp = fopen("succed ", "w+");
fclose(fp);
}



}

but not working :( what's wrong !!!!
please help.
Two of the three you did should work, there is a bug or we are missing something somewhere so lets check some basics

1.) Check the registry entry
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM and see what ports are listed

2.) What version of Windows CE are we dealing with

3.) Are you compiling in unicode or ascii mode?

4.) Can you invert the order of includes so windows.h is before tchar.h, like so which is the normal
C++
#include <Windows.h>
#include <tchar.h> 

5.) Can you show the line of code you open the port in the C# program
 
Share this answer
 
v5
Comments
Jammes_Ca 11-May-16 4:26am    
Thanks, i restarted the pda and it worked, i have a question what do you mean by compiling in unicode or ascii mode? ???.

and i have other question. how do i to change the modulation for THE IR on the pda ?? can we change it over the regestry ???
leon de boer 11-May-16 5:04am    
You are using the include file TCHAR.H which simply allows compiling in multiple language setting modes. It's changed in the General tab -> character set in the project properties you have 3 options, "Not set" (ascii mode), "use unicode character set" (unicode mode) or "Use Multibyte character set" (MBCS mode) which changes what a "character" is and means. It basically allows you to have more than 256 characters which you may need for languages other than English. Anyhow you can change the settings and all will be fine, without TCHAR.H you would be forced to select one of the settings and write to that specification.

When you say modulation is the port connected to a modem?? Or do you mean how do you change the port settings such as it's baud rate, stop and start bits etc??

Anyhow I will assume you mean BAUD rate etc which you can do once you have your comm handle

DCB PortDCB;
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the port setting information from your handle you got from open
GetCommState (m_hUsbCom, &PortDCB);

// You can change settings in the return DCB structure to what you want
// I will do 9600,n,8,1
PortDCB.BaudRate = 9600;
PortDCB.ByteSize = 8;
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT;

// Then write it back
if (!SetCommState (m_hUsbCom, &PortDCB)){
// Some error occured if in here
}

FINALLY DON'T FORGET TO CLOSE HANDLE WHEN FINISHED OR EXITING

// Close the comm handle
CloseHandle(m_hUsbCom);
Jammes_Ca 11-May-16 13:00pm    
Hi thanks for teh answer, for the modulation i want to change frequency of the IR.
i send data through IR . i want to change the data to 135.6 KHz. can we do a program that change this frequency ???

i use a "PDA PIDION BIP6000", it has an IRDA so i send data via the port com4,
leon de boer 11-May-16 14:38pm    
The manual says that is set via the settings menu on the device. There is probably a way to do it via a program but there is no program developer support documentation on it up on there website. I notice they do have support documentation for the barcode reader up there which is similar to what you need.

The device has 4 standards it uses on the RFID UHF: 840Mhz, 960Mhz OR Low Freq: 125Khz, 134.2Khz. Which does it default to at the moment?

So its really a question of getting a copy of the support documentation for the iRDA OR a copy of there CE driver code both which require you to talk to the supplier because they have nothing available on the website. It will be pretty basic but there really isn't anything to go on without more detail.

Now being an embedded programmer by trade I am going to let you in on a little secret which might work. Get a terminal program (like hyperterminal) and open com4 using it with 9600,n,8,1. Now type ?? and then the enter key. Most of us embedded programmers use this ?? to spew the commands for modules connected via a com port out to you. If someone like me has made the module it will give you the command to change the frequency via a command on the com port right onto your screen, as well as all the other commands to change things on the RFID.

I guess you have the port open in your code above so you could just write "??\r\n" to the port. Then go read the data from the port but be prepared if the ?? works the amount of data could be massive like several screens full or something like a thousand characters.
Jammes_Ca 12-May-16 8:17am    
Hi thanks for the answer, but i sent the "??" and i receive te ??.
i didn't see the commande to change frequency

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900