473,388 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

SerialPort writing byte data

8
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
Oct 29 '09 #1
9 8463
tlhintoq
3,525 Expert 2GB
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
Are you doing some kind of polling loop? Is that where the "time frame" comes in?

I would expect that if you were raising an event each time you add a row, with the data as the arguments then you should be calling your SerialSend method once for each row.
Oct 29 '09 #2
Plater
7,872 Expert 4TB
Depending on your situtation and time requirements, take a look at the System.Collections.Queue object. You can keep pushing things(commands for the serial port) onto it, and then on a timer or something, pullthem off and send them.
This assumes that you would push the entire message onto the queue at one time.
Oct 29 '09 #3
jensa
8
tlhintoq:
I have registered my event handler on the event on row added on the datagridview which means it will be raised once for each row added. If 10 rows are added 10 serialport write commands will be issued. If I hook up this function under the event handler it will cause big time problem, I assume this is because numerous ongoing writes will get a failure in the write buffer (mixed up) , I am not sure about this - but I will not work properly. That's why I used the singleton method with "a queue handler" (a list of bytes that is processed in order, one after one).

Plater:
Thanks, will look it up now!


PS ! I honestly don't know why I need to keep a Thread.Sleep(500) after the write method. I thought the SerialPort class handled problems with concurrent calls to write. Samples and tutorials online also use the Thread.Sleep and I haven't really seen any info why this is really needed. Of course it will not work without it, but why just 500 ms ? 200 ms will not work for an example. This feels like an uknown plumbing method.

Anyone care to enlighten me about this ?
Oct 29 '09 #4
jensa
8
Plater:

I quickly found out another post in another forum, let me quote (his exact words):

My initial response would be to say: create a CommInteraction class that contains a Command and a Response property. Add instances of the CommInteraction class to the output queue when commands are issued. As the worker loop ejects items from the queue and processes their Command property value, it places the response in the Response property and adds the item to the input queue. Now every response object in the input queue contains a copy of the command that caused this response. If needed you could store any additional info about the sender in the CommInteraction class that you need in order to identify it after the response is returned.


This seems like something I also would do to solve the problem, anyone else have any ideas ?
Oct 29 '09 #5
tlhintoq
3,525 Expert 2GB
That was going to be my next suggestion:
The NewRowEvent doesn't cause a serialport write, or the create of a class object that writes.
Instead it adds to a list of things to write by the one and only class doing the writing
Oct 29 '09 #6
jensa
8
tlhintoq:

I may be unclear ;-) .... but it this case I instantiate the SerialPort class during application loading and port is open from then until application closes. SerialPort is added as a private field so it can be accessed by internal methods.

So when the even on new row is fired in the same "class" I can access the SerialPort which is open and call SerialPort.write method. But just this method will not work since I get the trouble with concurrent calls if several rows are added in the very same near timeframe.

Regards,

Jens
Oct 29 '09 #7
tlhintoq
3,525 Expert 2GB
So one serialport object: Good.
But it sounds like all your other methods can send a SerialPort.Write command whenever they like. For now it may just be the AddRow event that does it. But the event causes an immediate attempt to write.

What I was suggesting was that you put the SerialPort object into class that has a list of datalines to send. Your AddRowEvent doesn't try to Serial.Write but instead adds it's data to the list in the SerialPortControlClass. The class will then handle the actual sending at it's own pace.

This should keep the 3rd call from stepping on the 2nd call, because the SerialPortControlClass will send each item in its list in sequence. Basically this because the queue of data to be sent.
Oct 29 '09 #8
tlhintoq
3,525 Expert 2GB
So one serialport object: Good.
But it sounds like all your other methods can send a SerialPort.Write command whenever they like: Even if the SerialPort is in the middle of a Write operation.

For now it may just be the AddRow event that tries it. Later you might have a dozen other things trying to send on the same port for different reasons.

What I am suggesting is that you put the SerialPort object into a class that has a list of datalines to send. Your AddRowEvent doesn't try to Serial.Write but instead adds it's data to the list in the SerialPortControlClass. The SerialPortControlClass will then handle the actual sending at it's own pace.

This should keep the 3rd call from stepping on the 2nd call, because the SerialPortControlClass will send each item in its list in sequence. Basically this because the queue of data to be sent.
Oct 29 '09 #9
jensa
8
Good, I will setup a class using the queue microsoft class in collections to see how it works out.

Regards,

Jens
Oct 30 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Igor Shulgin | last post by:
Hi! What is standard and direct way (within Oracle 9.2 stored procedures) for writing binary data from Oracle to external file on disk? We have tried to use UTL_FILE package, but it operates on...
4
by: Dmitry | last post by:
Hi! Does anybody know how to write binary data to registry? The purpose is to store an boxed object into registry and to restore it.
4
by: Ray Cassick \(Home\) | last post by:
I am trying to set up a form to edit a byte array and am looking for a control that gives me an interface that shows the following: 1) First column is the offset from the start of the array 2)...
1
by: Marquee | last post by:
Hello, This is my first program c#, my background is c++. One thing I would like to do is put binary data (e.g. a record from disk, or network packet) in a struct. In C++ I would create the...
5
by: shivaprasad | last post by:
Hi all, I am a Beginner to c#. I Need to convert array of byte data type to string. How to do this.. ex: byte bytes = new byte; this bytes get filled up. Now I need to convert to string.
1
by: Crirus | last post by:
Is there a way to store a negative value in a byte data type value? I have to store values between -50 and 50 and to use int16 is waiste of space -- Cheers, Crirus ...
1
by: ayasindemir | last post by:
Hi everybody, I want to make a media player that plays video which retrieves it from oracle database. I dont save path of the video, i am using ordsys.ordvideo. I think firstly i make buffer and...
1
by: ayasindemir | last post by:
-------------------------------------------------------------------------------- Hi everybody, I want to make a media player that plays video which retrieves it from oracle database. I dont save...
1
by: Happy2006 | last post by:
Hello I am trying to call a function in C from C# though c ++ so basically C# -> C++ - >C In C#, I have byte bytes - which reads the information from the file. I am passing the byte array...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.