1

I'm currently working on a project that will read data from a micro-controller via serial communications.

As of right now, the program (on my computer), opens a /dev/tty* file and is able to read/write to it. The micro-controller will send a packet of n bytes at any time. I want to know if there is any way I can tell when all of the bytes have been written to the file?

I've been looking at the select() and poll() functions, but they seem to be only able to tell when a byte is ready, but not when every byte has been written.

Any help is appreciated. Thanks!

mange
  • 3,172
  • 18
  • 27
Benjamin
  • 1,223
  • 1
  • 13
  • 22

2 Answers2

3

If your n is hardcoded you can just do (with pseudocode):

received_data = offset_from_last_round
while( received_data < n )
{
    use select() for waiting data to arrive
    read() all data you can, dont forget to check buffer oveflow here
    received_data += how much data was red
}
full_message = buffer[ 0 ... N - 1 ]
offset_to_next_round = buffer[ N .. received_data ]

If n is not hardcoded you need to do something like what @Golgauth suggested, or add some "end of transmission" sequence/byte to your message (which is tricky if you have binary file/data to transmit). In short: You need some sort of protocol.

susundberg
  • 650
  • 7
  • 14
  • The 'n' is not fixed. I was thinking about crafting some sort of protocol, but I'm still doing some more research to see what tools linux already gives me. – Benjamin Jun 26 '13 at 02:08
  • @Benjamin Your question is interesting, and this would be great if you could answer your own question on this same page once you have something or chosen an option ;) – Gauthier Boaglio Jun 26 '13 at 02:27
  • @Golggauth I'm working on a personal project right now that involves certain stuff like this. I already have a sort of solution thought of, but it isn't really elegant, and I want to do some more research to see if there are any other tools that are available to me before I go forward with this. Thanks for the interest though. – Benjamin Jun 28 '13 at 01:07
2

Well, the idea is that your binary file should start by some bytes which actually give the size that has to be read next.

  • Read N bytes => gives the DATASIZE, i.e. how many bytes remaining: (FILESIZE - N)
  • Read (DATASIZE) bytes => gives the data themselves (readable by blocks/packets of size n)

This is the kind of discussion we were having here actually (this about how to interpret a raw PCM wav sound file, but this is the same point: getting the number N of samples, to determine how many blocks are to be read next to get the file with concern to the integrity).

Community
  • 1
  • 1
Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85