Tech Support > Microsoft Windows > Development Resources > A strange problem about WriteFile and serial I/O
A strange problem about WriteFile and serial I/O
Posted by JR_Selina on April 27th, 2007


I met a strange problem.

I am reading a serial I/O source code from other engineer. Actually,
there are many mistakes in his code but I still have to figure out why
the problem would happen.

Here is the situation.
The program would open a serial port and output 42000bytes.
The device connected to the serial port is a serial printer.
Although the program has many mistakes, it still can work well in some
kind of situation.

Generally, I would use "HyperTerminal" to check if the device is
connected well or not. (I press a key without stopping for few seconds
and check the printer. )
Then I execute the program to print a long strings....
But the printer stopped printing after printing about 10000bytes.
WriteFile return TRUE but mNumberWritten < mNumberHavetoWrite.
I try to use GetLastError()...and the error code is
"ERROR_INVALID_PARAMETER
". I dont understand why the program can transmit characters with
"INVALID PARAMETER" for a while.

However, if I did not use HyperTeminal to check communication, the
program can print all bytes.

Why the execution result was influenced by other software?

I also use a logical analyzer to get the waveform of transmission.
When I used this program, the TX signal would stop without any DSR or
CTS signal. When I used another program that I wrote by myself, the
TX signal worked well with the DSR signal. I think the problem might
be not caused by the printer.

Can somebody help me? This problem has confused me for several days.
The problem happended on COM1 (on board) and COM6 (PCI card, multi-
port serial adapters). I want to know why the program would act like
this.

ps. I have found that the program didn't change every necessay
parameter in "DCB" struct but I have solved it.

Regards.
by SelinaKuo.

Posted by Scott McPhillips [MVP] on April 27th, 2007


JR_Selina wrote:
That almost certainly means that HyperTerminal set things in the DCB
that your program does not change. You inherit HyperTerminal's settings
if you do not change the appropriate DCB field yourself.

The ERROR_INVALID_PARAMETER error should be obvious if you set a
breakpoint at that point and examine all the parameters.

--
Scott McPhillips [VC++ MVP]


Posted by JR_Selina on April 27th, 2007


On 4¤ë27¤é, ¤U¤È12®É05¤À, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcp> wrote:
Dear Scott:

I solved this problem. :-)

I have known about the inherit of DCB and I also write a program to
prevent it from happening.

After doing several tests, I found that the setting in the
COMMTIMEOUTS also inheritable.
The program did use SetCommTimeouts but a function which contain
SetCommTimeouts return too early to execute SetCommTimeouts.

So the WriteFile return TRUE and not all bytes transmitted.

Solving problem always makes me to realize serial communication
more. :-)



Similar Posts