I am experiencing a confusing problem with reading data from the serial port (COM1), and writing some data into a text file on SD card.
In my scenario, a serial RF transceiver is connected to COM1. The received data is in the form of few messages per second with the length of 40-50 bytes each. Some processes are done on the received data, and processing results are written into the disk. However, application specification results in a higher amount of data to be written into the disk, compared to the amount of data received and delivered by the RF transceiver, about 100 bytes per 10-20 milliseconds, like a data stream.
Having said that, I've made two different threads except the main thread, one to only read data without any processing from the port and store them into a buffer (a byte array), and the other to process the stored data and write the results into the disk (using FileStream and StreamWriter methods).
Everything works fine, when the writing into the disk is disabled (using some flags in my code), and all data can be processed properly as I can validate it from the debug display. However, when the writing is active, data are not received completely from the serial port, and some part of messages are lost. This does not happens all the time, but happens frequently enough to degrade the total processing performance.
Considering that I have used two different threads, and given a higher priority to the "serial port reader" thread and a lower priority to a the "Disk Writer" thread, is it possible that a kind of shared buffer in a lower level is used commonly between that Disk I/O and Serial Port I/O that causes overriding somewhere.
any advice is appreciated. Thanks.