Click to See Complete Forum and Search --> : Question for PDA connect to computer through RS232


AndPray
December 13th, 2005, 11:28 PM
Hello everyone,

I had writen a program for PDA connecting to computer through RS232 interface.
The function setting the connection be shown as follow ....
====

// search a openable comport to open
// return 0 ~ 3 for com1 ~ com4
// return 4 ~ for error....
int SetPortOpen (HANDLE & com_handle, DCB & dcb) {
int i_com;
char s_comPort[4][5];
strcpy(s_comPort[0], "COM1");
strcpy(s_comPort[1], "COM2");
strcpy(s_comPort[2], "COM3");
strcpy(s_comPort[3], "COM4");

// open com port
for (i_com = 0 ; i_com < 4 ; i_com++) {
com_handle = CreateFile(_T("s_comPort[i_com]"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
if (com_handle != INVALID_HANDLE_VALUE) break;
}
if (i_com == 4) return 4; // RETURN HERE

// setup device buffers
SetupComm(*hComDevice , 4096, 4096 );

// setting DCB
dcb.DCBlength = sizeof( DCB );
GetCommState(com_handle, &dcb ) ;
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = FALSE;
dcb.StopBits = ONESTOPBIT;

// configure the port
if (SetCommState(com_handle, &dcb) == 0) {
CloseHandle(com_handle);
return 5;
}

return i_com;
}

========
I try com1 to com4 because of unsuring which com port should be opened on PDA.
But, it still cannot work.
This function always return 4, the line with "//RETURN HERE".

How should I modified this function for PDA connecting to computer?

Thanks a lot <(_ _)>

alex_gusev
January 29th, 2006, 04:42 AM
Hi,

com_handle = CreateFile(_T("s_comPort[i_com]"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);

bold text shows an error, and in addition, com ports are named usually as _T("COM1:") etc.

Max Payne
February 5th, 2006, 07:48 PM
// search a openable comport to open
// return 0 ~ 3 for com1 ~ com4
// return 4 ~ for error....
int SetPortOpen (HANDLE & com_handle, DCB & dcb) {
int i_com;
TCHAR s_comPort[4][5];
_tcscpy(s_comPort[0], _T("COM1:"));
_tcscpy(s_comPort[1], _T("COM2:"));
_tcscpy(s_comPort[2], _T("COM3:"));
_tcscpy(s_comPort[3], _T("COM4:"));

// open com port
for (i_com = 0 ; i_com < 4 ; i_com++) {
com_handle = CreateFile(s_comPort[i_com], GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
if (com_handle != INVALID_HANDLE_VALUE) break;
}
if (i_com == 4) return 4; // RETURN HERE

// setup device buffers
SetupComm(*hComDevice , 4096, 4096 );

// setting DCB
dcb.DCBlength = sizeof( DCB );
GetCommState(com_handle, &dcb ) ;
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = FALSE;
dcb.StopBits = ONESTOPBIT;

// configure the port
if (SetCommState(com_handle, &dcb) == 0) {
CloseHandle(com_handle);
return 5;
}

return i_com;
}


see changes in BOLd maybe should help you..


How should I modified this function for PDA connecting to computer?

it depends on which port your using to connect to the PC, lets say Com1, then open com 1, and to send data to PC use WriteFile(...)
and to read from the com port when pc replys, use ReadFile(...). search MSDN for the WriteFile and ReadFile function.