Re: why can't i write to a file with this code?
- From: Sam <sam@xxxxxxxxxxxxxx>
- Date: Sat, 01 Jul 2006 21:24:54 -0500
laclac01@xxxxxxxxx writes:
Attached is come code that reads GPS and then write it to a file, but
for some reason the file is always empty... I don't know why its empty.
Can't really say without knowing the format of the data that your hardware device sends through the serial port.
But, in any case, your logic for reading data from the serial port is most definitely broken. You are assuming that your read() statement always returns a single chunk, or a packet, from your hardware device. That's what your code appears to assume.
You cannot make this assumption. The Linux kernel has absolutely no idea, nor does it care, what you have attached to the serial port, and the format of the data you are receiving. As far as the Linux kernel is concerned, the serial port is just a source of arbitrary, random data, that comes it at random times. The Linux kernel will simply give you the bytes received from the serial port, as they are received, whenever you call read(). If, when you call read(), two characters have already been received, and buffered, by the kernel from the serial port, your read() call will return those two bytes, even though your hardware device is in the middle of sending ten bytes to you, and there are eight more bytes to go, which will be received just after your read() call returns.
If nothing has been received from the serial port, and your file descriptor is a blocking descriptor, read() will wait until data is received from the serial port, and you will likely get back only a single byte, as soon as it's received from the serial port. But, if your system is loaded, it's possible that a few more characters might be processed by the serial port's interrupt handler, before your task receives CPU time, and read() will return to you all the bytes that have been received, at once.
Without knowing the specific format of the data that your device transmits through the serial port, it's not really possible to tell you what you need to do. But, generally, you need to keep reading data from the serial port, without making any kind of an assumption as to how many bytes gets returned by each read() call, and then examine the data one byte at a time, until you've received the end of a single message or a packet from your hardware device.
Attachment:
pgp3bAOusYAkl.pgp
Description: PGP signature
- References:
- why can't i write to a file with this code?
- From: laclac01
- why can't i write to a file with this code?
- Prev by Date: Re: why can't i write to a file with this code?
- Next by Date: Re: why can't i write to a file with this code?
- Previous by thread: Re: why can't i write to a file with this code?
- Index(es):
Relevant Pages
|