Hi,
I am currently writing a simple form application in C# that uses the SerialPort class (C# 3.5). But I have put my head in the wall several times to solve a simple problem.
I have a datagridview and each time the event on row added is run I need to send some stuff out on the serialport. I am using the SerialPort write method to write byte data.
Each times several rows are added (more than one), in the same time frame (or very near in milliseconds) I get stuck with the problem that the write method is called simultaneously which causes the SerialData send buffer to contain data spread from the different row sources which in the end will be very incorrect. After each write I also need to but a thread sleep (500 ms) otherwise results are "unknown".
I therefore implemented a singleton class(that's holds all the serial comm stuff) that run's on it's own thread and whenever serial data needs to written I put the data into a byte list in the class and the class on it's self handles to writes sequentially after each write is completed. I still need to keep the thread sleep (500 ms) but it runs in the separate thread and does not cause any problem but this solutions feel like doing plumbing.
Is this really a correct implementation or this something simpler or anybody else recommend any other approach ?
Regards,
Jens
I am currently writing a simple form application in C# that uses the SerialPort class (C# 3.5). But I have put my head in the wall several times to solve a simple problem.
I have a datagridview and each time the event on row added is run I need to send some stuff out on the serialport. I am using the SerialPort write method to write byte data.
Each times several rows are added (more than one), in the same time frame (or very near in milliseconds) I get stuck with the problem that the write method is called simultaneously which causes the SerialData send buffer to contain data spread from the different row sources which in the end will be very incorrect. After each write I also need to but a thread sleep (500 ms) otherwise results are "unknown".
I therefore implemented a singleton class(that's holds all the serial comm stuff) that run's on it's own thread and whenever serial data needs to written I put the data into a byte list in the class and the class on it's self handles to writes sequentially after each write is completed. I still need to keep the thread sleep (500 ms) but it runs in the separate thread and does not cause any problem but this solutions feel like doing plumbing.
Is this really a correct implementation or this something simpler or anybody else recommend any other approach ?
Regards,
Jens
Comment