Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++ serial port

Status
Not open for further replies.

susuke

Technical User
Jul 22, 2002
17
0
0
HU
Hello!

I have connected a n6310 to the COM2 through the phone's serial cable and write this code in C++:

-----
...
void* hPort;

hPort=CreateFile("COM2:",GENERIC_READ|GENERIC_WRITE,0, NULL,OPEN_EXISTING,0,NULL);

AnsiString Message;
DWORD dwNumBytesWritten;
Message="atdt+36702636366\n";
WriteFile(hPort,&Message,18,&dwNumBytesWritten,NULL);
CloseHandle(hPort);
----

but it doesn't work. The CreateFile and WriteFile functions return with 0 error code. The AT command on the Message variable is correct, because it works in HyperTerminal.
Can anybody tell what the problem is?

thanx
 
In the source code i did not set the serial port. I use the default. Should i set? (i thouhgt if it works in hyperterminal then the port settings are correct and the c++ use the windows settings...)

If i'm wrong then please write what should i set.

thanx
 
I am only guessing here - so I could be wrong, but I think that hyperterminal has default baud rates and hand shaking - and there isn't a default windows setting for this.
I am guessing that your phone probably speaks at 9600 baud - have a look in the phone specs.
Let me know how you get on.

M.

Hollingside Technologies, Making Technology work for you.
 
i've add these settings:
----
DCB PortDCB;

PortDCB.DCBlength=sizeof(DCB);
GetCommState(hPort,&PortDCB);

PortDCB.BaudRate=9600;
PortDCB.ByteSize =8;
PortDCB.Parity =NOPARITY;
PortDCB.StopBits =ONESTOPBIT;
SetCommState(hPort,&PortDCB);
----
but it still doesn't work. I know there are more settings (fBinary, fParity...) but i don't know what should i set for these...

I've never programmed serial port and i'm a beginner in c++ too. So, can you tell me what does hand-shaking mean?
 
By handshaking I mean, establishing comms with the device - ensuring the baud rates etc are correct, and initiating the communications.
For instance, with a modem you might type AT - which stands for attention - then you go DT to get a dialing tone etc.

Your C++ seems to be up to the job - however the info you need seems to be specfic to your phone.

M.

Hollingside Technologies, Making Technology work for you.
 
I have not used the CreateFile command in awhile, however, I thought that the first parameter is simply "COM1" and not "COM1:".

try this for your CreateFile line

hPort=CreateFile(
"COM",
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);


Hope this works.
 
Thanx for the help, but i found a component for com port (ComPort Library v2.64). I think it's more useful. But i couldn't use this for comuunication too, because the message was wrong, because the message has to be closed with \r\n not only \n.

Thanx.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top