I would guess you are overflowing the machine's serial port buffer. There are a few ways you can deal with it. First is let the hardware do the work. That means you need to have a cable with the control signal lines connected appropriately on both sides as described at
Null modem - Wikipedia[
^]. The control signals are used by the machine to tell the sender its buffer is full. A well-behaved serial port driver will pause transmission when the receiving side is not ready to receive data and this would solve your problem. Most serial cables have just three lines so this option may not be available to you.
Another way is to insert delays, as you have already, but delay after some number of characters. You stated there is loss after forty lines so figure out how many bytes that is and delay a few hundred milliseconds after each batch of that many bytes. Usually serial port buffers are 256 or 512 bytes but you can determine that through experimentation. The delays will allow the machine to process the data in its buffer so it can accept more. I recommend that you parameterize the number of bytes between delays and the duration of the delay. Values I would start with are 256 bytes and 200mS. Adjust them as necessary until all of the data is received correctly.