-
May 20th, 2011, 01:41 AM
#1
problem with serial port communication
I am working on an embeeded device.i connect to it using COM port.
It gives the list of all files when i send a command "LIST" to it.
so i wrote an "hello world" which will connect to the port device is connected and will send data.
When i connect my device and run my program it is writing to the port and not receiving any bytes from the port.
but when i open the COM port using PUTTY(which is used to open port and send some data) and send COMMAND it works and when i CLOSE PUTTY and NOW RUN MY PROGRAM now it is working fine,so there is some initialisation to the port which i am missing.
can anyone help me out in this,i am unable to find solution for the past day.thanks in advance...
my program is:-
Code:
#include "stdafx.h"
#include <iostream>
#include <afx.h>
int main()
{
using namespace std;
int i=0;
// cout << "Hello world!" << endl;
HANDLE hSerial;
hSerial = CreateFile("COM5",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
// TRACE("serial port does not exist for reading\n");
//serial port does not exist. Inform user.
}
// TRACE("some other error,serial port does not exist for reading\n");
//some other error occurred. Inform user.
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
{
// TRACE("error getting state for reading\n");
//error getting state
}
dcbSerialParams.BaudRate=9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
dcbSerialParams.fOutX=TRUE;
dcbSerialParams.fInX=TRUE;
if(!SetCommState(hSerial, &dcbSerialParams))
{
//TRACE("error setting state for reading\n");
//error setting serial port state
}
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts))
{
// TRACE("some error occured for reading\n");
//error occureed. Inform user
}
int n=100,n1=100;
char szBuff[100];
DWORD dwBytesRead = 0;
char szBuff1[100];
DWORD dwByteswrote = 0;
memset(szBuff1,0,100);
memcpy(szBuff1,"LIST\r",5);
FlushFileBuffers(hSerial);
LPDWORD uf=0;
GetCommModemStatus(hSerial,uf);
TRACE("%d\n",uf);
if(!WriteFile(hSerial, szBuff1,5, &dwByteswrote, NULL))
{
cout << "error writing" ;
}
cout << szBuff1 << endl;
cout << dwByteswrote << endl;
dwByteswrote=0;
while(1)
{
if(!ReadFile(hSerial, szBuff, n1, &dwBytesRead, NULL))
{
cout << "error reading";
break;
}
else
{
cout << dwBytesRead << endl;
szBuff[dwBytesRead]='\0';
if(dwBytesRead>0)
{
cout << (szBuff);
break;
}
else
{
if(!WriteFile(hSerial, szBuff1,5, &dwByteswrote, NULL))
{
cout << "error writing" ;
}
cout << dwByteswrote << endl;
}
}
}
cin >> i;
}
Last edited by cilu; May 20th, 2011 at 03:25 AM.
Reason: code tags
-
May 20th, 2011, 03:25 AM
#2
Re: problem with serial port communication
I see you are calling SetCommState and SetCommTimeouts but are you sure you are initializing with the correct values? Like baudrate, bits, parity, rd/wr timeouts, etc. Also, maybe it takes a little bit to answer after you send the command, so try putting a Sleep before your ReadFile call.
-
May 20th, 2011, 03:45 AM
#3
Re: problem with serial port communication
i am reading from the port continuosly in a while(1) loop till i read some bytes so there is no problem that data may arrive late.
and my program is running correctly when i first open the port with putty and close that port and then run my program.
so putty is doing some initialisation to the port which i am missing...
i cant figure it out...
-
May 20th, 2011, 05:58 AM
#4
Re: problem with serial port communication
It could be that your device needs a carriage return/linefeed pair (typical from a terminal program in Windows) in order to see the command.
You might try replacing
Code:
memcpy(szBuff1,"LIST\r",5);
with
Code:
memcpy(szBuff1,"LIST\r\n",6);
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
May 20th, 2011, 06:04 AM
#5
Re: problem with serial port communication
I found solution for this,
I need to initialize all members in the DCB structure,thankyou all
i need to add this code..........
dcbSerialParams.fBinary = FALSE;
dcbSerialParams.fParity = FALSE;
dcbSerialParams.fOutxCtsFlow = FALSE;
dcbSerialParams.fOutxDsrFlow = FALSE;
dcbSerialParams.fDsrSensitivity = FALSE;
dcbSerialParams.fErrorChar = FALSE;
dcbSerialParams.fNull = FALSE;
dcbSerialParams.fAbortOnError = FALSE;
-
May 20th, 2011, 06:33 AM
#6
Re: problem with serial port communication
Originally Posted by abhinav123
...
i need to add this code..........
dcbSerialParams.fBinary = FALSE;
dcbSerialParams.fParity = FALSE;
dcbSerialParams.fOutxCtsFlow = FALSE;
dcbSerialParams.fOutxDsrFlow = FALSE;
dcbSerialParams.fDsrSensitivity = FALSE;
dcbSerialParams.fErrorChar = FALSE;
dcbSerialParams.fNull = FALSE;
dcbSerialParams.fAbortOnError = FALSE;
Well, since all these values are the bit-values using 1/0 (one or zero) appears to be a little more preferable than TRUE/FALSE ...
Victor Nijegorodov
-
May 20th, 2011, 06:41 AM
#7
Re: problem with serial port communication
ohk fine,but anyways its working now...
i will change it now...
-
May 20th, 2011, 06:53 AM
#8
Re: problem with serial port communication
While it's true that these DCB members are bit values, the MSDN documentation refers to them as being TRUE. To me that implies that TRUE (or FALSE) would be the preferred way to set them.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
May 20th, 2011, 06:58 AM
#9
Re: problem with serial port communication
Originally Posted by krmed
While it's true that these DCB members are bit values, the MSDN documentation refers to them as being TRUE. To me that implies that TRUE (or FALSE) would be the preferred way to set them.
Well. I'd say that in this case the MSDN sucks (a little!).
BTW, how would you assign the DCB::fDtrControl member? Also TRUE/FALSE?
Victor Nijegorodov
-
May 20th, 2011, 07:06 AM
#10
Re: problem with serial port communication
I would set it as MSDN describes:
DTR_CONTROL_DISABLE 0x00 Disables the DTR line when the device is opened and leaves it disabled
DTR_CONTROL_ENABLE 0x01 Enables the DTR line when the device is opened and leaves it on.
DTR_CONTROL_HANDSHAKE 0x02 Enables DTR handshaking. If handshaking is enabled, it is an error for the application to adjust the line by using the EscapeCommFunction function
.
Last edited by krmed; May 20th, 2011 at 07:08 AM.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
May 20th, 2011, 07:13 AM
#11
Re: problem with serial port communication
Sure you will do it the correct way!
But a lot of beginner could set it to TRUE just by analogy with other members like DCB::fErrorChar
Victor Nijegorodov
-
May 20th, 2011, 07:14 AM
#12
Re: problem with serial port communication
When they created the function and wrote the documentation they didn't probably took into consideration that TRUE might be defined other than 1. And in practice it will never probably be, except that is not hard to undefine and redefine it.
-
May 20th, 2011, 07:15 AM
#13
Re: problem with serial port communication
Originally Posted by VictorN
Sure you will do it the correct way!
But a lot of beginner could set it to TRUE just by analogy with other members like DCB::fErrorChar
Yes, but then we all know that many beginners either don't know about, or don't bother to look at, the documentation in MSDN!
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
May 20th, 2011, 07:26 AM
#14
Re: problem with serial port communication
One more argument that this article is not correct is: why assign BOOL value to non-boolean variable (bit field in this case)?
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|