Re: why can't i write to a file with this code?



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



Relevant Pages

  • Re: Will Windows 7 be a laptop upgrade nightmare?
    ... keyboard and receiver that used the serial port. ... DOS but failed under later copies of Windows. ... But on second thought, you said you're sending and receiving Morse code; that sounds pretty time critical, which emulators are not. ...
    (comp.sys.laptops)
  • Re: The big Debate on DoEvents
    ... avoid Application.DoEvents and deal with the serial port processing in ... I have a program that receives information from the serial port. ... a progressbar telling me how much of the information I have received. ... user clicks on the screen at any point whilst receiving information ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Can you help me to explain a question about serial port and resolve the problem.
    ... You shoud not change settings of the serial port while you transmit data or ... In chips side, they are identified by CHIP NUMBER and MARKPARITY ... without receiving followed CMD and SPACEPARITY command from computer. ...
    (microsoft.public.development.device.drivers)
  • Begginer Question about TK::filevent
    ... I am beginer in Perl. ... receiving and transmitting on same or diffrent ports depending on the ... Serial port becomes readable. ... I wish to call &read_comport whenever my comport becomes readable ...
    (comp.lang.perl)
  • Re: How to move binary data into a PC?
    ... USB is not what people often think - you can't just whip up some code ... Connect modems on each remote module's serial port. ... Write collected record with time stamp into data file - WriteFile ... Store records in format of your choice, like CSV, etc. ...
    (sci.electronics.design)