COM port access error using CreateFile()

A

ashah

Hi,

I'm using a Symbol PDT8037 device with a GPRS modem and am trying to
access the modem (which is on COM9) so that I can query/change the
band information if it is incorrect. With the GPRS band set correctly
(to 1900 MHz), I am able to open up the COM9 port to talk to the SIM
chip in my device. However, if I change the band to 900/1800 MHz
through GSMDemo (a program provided by Symbol that lets you change
GPRS parameters), then after exiting GSMDemo I can no longer open the
COM9 port using my code (included below - the GetDevice method is
being passed "COM9:" as a parameter). GetLastError() returns error
code 55 (ERROR_DEV_NOT_EXIST). Even if I soft reset the device, I am
unable to open the COM port again till I go back into GSMDemo and
change the band information back to 1900 MHz. With the band set back
to 1900 MHz, the code below works just fine.

Any suggestions on what might be causing this problem?

private static uint GENERIC_READ = 0x80000000;
private static uint GENERIC_WRITE = 0x40000000;
private static int OPEN_EXISTING = 3;

private static int m_ihandle = -1;

[DllImport("coredll.dll", SetLastError=true)]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFlag);

[DllImport("coredll.dll", SetLastError=true)]
private static extern int GetLastError();

private static bool GetDevice(string Port)
{
// open the existing port...
m_ihandle = CreateFile(Port,
GENERIC_WRITE | GENERIC_READ,
0, // comm devices must be opened w/exclusive-access
0, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0,
0); // hTemplate must be NULL for comm devices

//if the handle value is -1, that means you got an error....
if(m_ihandle == -1)
{
int errCode = GetLastError();
MessageBox.Show("Failed to get COM port - error " + errCode);
return false;
}
else
return true;
}


Thanks,
Amol
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top