Re: about serial port communication and new operator

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Several issues... see below
On 29 Apr 2006 05:01:40 -0700, "harshalshete@xxxxxxxxx" <harshalshete@xxxxxxxxx> wrote:

hi group,

i got two problems
1) related to serial port communication
2) related to new operator


problem 1)i am opening the com port for serial communication
COM1.then i am trying to communicate with device with this port.
on my computer i get a valid COM1 handle.then i have done GetComState
and SetComstate
and then i am issuing a read call on that port.
my problem is that for some time the program was running fine and then
suddenly it is not reading anything.

and ReadFile is returning 1 but the number of bytes read are zero i
have given the code below.
****
This is expected behavior from a serial port. Why does it surprise you?
*****
please help me if anyone knows about this problem.






BOOL fSuccess;
CString Port = getPort();
DCB dcb;



hCom = CreateFile( Port,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

if (hCom == INVALID_HANDLE_VALUE)
{
MessageBox("Can Not Open the port file","Error",MB_ICONERROR);
return 1;
****
And what, exactly, does "1" mean? This is a BOOL function. It returns TRUE or FALSE.
Values like "1" and "0" are nonsensical!
****
}

// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.

fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
MessageBox("GetCommstate failed","Error",MB_ICONERROR);
return 1;
}

// Fill in DCB: 9600 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
MessageBox("SetCommstate failed","Error",MB_ICONERROR);
return 1;
****
By the way, at what point do you close the handle on an error?
****
}

////for reading from port

char * buff = new char[1024];
int j=0;
DWORD i;
CString GreatBuffer;
while(1)
*****
Would it hurt to write readable code? What's "1" mean? Why not something meaningful,
like "TRUE"?
*****
{
memset(buff,0,512);
*****
Useless and nonsensical. Why are you clearing out only half the buffer? Why are you not
use ::ZeroMemory? Why do you feel you have to clear it out at all?
*****
j = ReadFile(hCom,buff,512,&i,NULL);
****
Have you ever heard of #define? What's with these nonsensical hardwired values like 1024
and 512 doing in a piece of code?
****
GreatBuffer += buff;
****
Why do you think your CString is 8-bit characters, really? Why not something like


if(i == 0)
continue;
GreatBuffer += CString(buff, i);
*****
if(i<512)
break;
****
Why 512? Why not 1024? Why not 729? Why break here? This requires you receive at least
512 characters? Why do you think this is going to happen? Communications channels can
drop characters, for example.

Doing serial communication reliably involves things like worrying about timeouts, never
working with fixed-size limits, and writing REALLY robust code. Note also that this would
block the main GUI thread while the data was being read, always a Really Bad Idea on a
device with unbounded blocking time.
****
}
MessageBox(GreatBuffer,"GreatBuffer");
delete [] buff;
buff=NULL;
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
--
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
.



Relevant Pages

  • Re: XON/XOFF communication problem
    ... >Before running the script I have set the port with the command: ... >communication code? ... systems to do computer to computer serial port comm at 115.2Kbps ... testing them by talking to a modem at 57.6Kbps. ...
    (comp.os.linux.misc)
  • RE: Well, were getting closer. Still having issues installing B itTorrent.
    ... well it isn't really a bittorrent problem but rather a python one. ... I would check with the port maintainer if my ... This email communication is intended as a private communication for the sole ... Cette communication par courrier électronique est une communication privée à ...
    (freebsd-questions)
  • Re: Explain this about threads
    ... Basically I'm trying to do synchronous communication with the parallel ... I guess I'm curious at this point as to why you want to use this kernel ... the unmanaged parallel port access via CreateFile, ... If I do spinwaitthen I'm guaranteed a atleast whatever maximum time ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Telnet port 25
    ... I used IPSec to accomplish this task. ... telnet to port 25 will be closed. ... This email communication and any attachments may ... >contain confidential and privileged information for the use of the ...
    (AIX-L)
  • Re: EMERGENCY: Problem with COM port (RS232) communication
    ... in for the flags value, indicating that you don't want anything to be ... It is possible that there is a buffer that the OS is trying to ... > the communication is done in 'bytes' because the device is pretty stupid> and uses 1-byte commands and responses> ... > we picked up this code from some MSDN page dealing with COM port> communication: ...
    (microsoft.public.dotnet.languages.csharp)