PDA

View Full Version : Communication routine


vz
07-26-2002, 21:20
Hello!
Who can share with me working routine for COM PORT that is using WIN32API?

Thanks!

.:hack3r2k:.
08-28-2002, 21:09
This function opens a RS232 connection to a specified com port :)
;---------------------------------------------------------------------------------

HANDLE ConnectRS232(LPSTR PortName)
{
DCB dcb;
HANDLE hCommPort;
DWORD MAX_READ_BUFFER, MAX_WRITE_BUFFER, dwErr;
INT baud_rate[] ={110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 128000, 256000};


MAX_READ_BUFFER = MAX_WRITE_BUFFER = 4096;

hCommPort = CreateFile (PortName, GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);


dwErr = GetLastError();

if (dwErr == ERROR_ACCESS_DENIED)
{
MessageBox(hWnd,
"The selected port is curently in use !", "Error", MB_ICONERROR );
return INVALID_HANDLE_VALUE;
exit(1);
}
else
{

SetupComm (hCommPort, MAX_READ_BUFFER, MAX_WRITE_BUFFER);
PurgeComm (hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
SetCommMask(hCommPort, EV_BREAK | EV_RXFLAG);

comm.ReadIntervalTimeout = DWORD(-1);
comm.ReadTotalTimeoutMultiplier = 0;
comm.ReadTotalTimeoutConstant = 3000; //1000
comm.WriteTotalTimeoutMultiplier = 0;
comm.WriteTotalTimeoutConstant = 3000; //1000

SetCommTimeouts(hCommPort, &comm);

dcb.DCBlength = sizeof(DCB);
GetCommState( hCommPort, &dcb);



dcb.BaudRate = baud_rate[SendMessage(handle[12], CB_GETCURSEL, NULL, NULL)];
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = 0;
dcb.XoffLim = 0;
dcb.XonLim = 0;
if (SetCommState( hCommPort, &dcb) == FALSE)
{
MessageBox(hWnd, "Cannot set port params !", "Error", MB_ICONERROR);
return INVALID_HANDLE_VALUE;
exit(1);

}
EscapeCommFunction (hCommPort, SETDTR);
return hCommPort;
}
}

This function enumerates the com ports and sends the name to a combobox :)
;----------------------------------------------------------------------------------
void EnumSerialPorts(HWND ComboBox)
{
HANDLE hCommPort;
DWORD ErrorID;
CHAR PortName[260];
INT i;

for (i=1;i<=5;i++){

wsprintf(PortName, "COM%d", i);
hCommPort = CreateFile (PortName, GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, // | FILE_FLAG_OVERLAPPED,
NULL);
ErrorID = GetLastError();

if ((ErrorID == ERROR_ACCESS_DENIED) || (ErrorID == ERROR_SUCCESS)){
SendMessage(ComboBox, CB_ADDSTRING, NULL, (LPARAM)PortName);
CloseHandle(hCommPort);
}
}
SendMessage(ComboBox, CB_SETCURSEL, 0, NULL);
}

Well i hope that this will give u enough help (anyway u'll have to change the a little to adapt the m to you prog as the ones aboves are pasted from one of my programs)

About reading/writing routine i can give u a fixed routine as it depends from phone to phone. If u plan to use them use VC++ 7.0 as i used some values included only in 7.0 version (this ver is included in Visual Studio .NET)

P.S: Don't forget about the credit ...

best regards,
.:hack3r2k:.

vz
09-06-2002, 14:59
Thanks! it works for opening com ports.
But now I have the next problem.
Why when I'm trying to write something by the next way:

(for example):
char e[]="abc";
WriteFile(hComm, &e, 3, &writtenbytes, NULL);

on output I have "61 62 63 00" ?
As i know, I'm trying to write only 3 bytes, but ComSpy tell that
there was 4.
I don't know how to solve that problem and I'm asking for help!!
Help! :)

gsmnow
09-06-2002, 20:40
The last digits (00) appears when PC close the COM port.

Best Regards!!!

.:hack3r2k:.
09-07-2002, 00:30
It writes xx xx xx 00 to port cause the string u are trying to write it's a null terminated string. After the bytes of the string u have 00. So the prog is right when it says that u are writing 2 port 4 bytes.


best regards,
.:hack3r2k:.

vz
09-07-2002, 08:36
.:hack3r2k: You are not right. I gave many various number in the parameter of WriteFile's counter of written bytes.

gsmnow: Yes, it's true. But by the way my Siemens don't want to response to at commands. And I don't understand why.
On the command "at+cgmi" ended 0D, Siemens must to say SIEMENSOK and after some delay i can give next commands, but it's not work.

Who can explain the mechanism???

pavlin
09-12-2002, 10:03
Can you give me compiled .exe for sending bytes/files via COM port,regardless of the device, attached on comport?

vz
09-12-2002, 11:27
Try to use any terminal program.
Something like old and good Teleport Pro or something else.
There's many option that you can to configure and also you
can use the simpliest script language.

pavlin
09-12-2002, 13:38
Originally posted by vz
Try to use any terminal program.
Something like old and good Teleport Pro or something else.
There's many option that you can to configure and also you
can use the simpliest script language.

10x for replay.Can you guve me a link? Or if you can, send me at pavlinb@mail.bg

starphone
03-27-2003, 01:19
any example for visual basic how to comunicate with that?


thanks

Jester74
03-27-2003, 09:07
Originally posted by Nutzo
Check baudrate (9600), databits (8), parity (N), stopbits (1),
RTS (disable) and DTR (disable).
BR,

If you use Siemens c25,c35,m35,s35 boudrate must be 19200 to can receive answer to AT commands.