|
GDavy wrote: forgot to commit...
Been there, seen it, got the T-shirt. Glad you got it sorted.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi everyone,
I have a project where i need my C# application to communicate with a microcontroller. I have set up a UART communication at 9600 baud, 8 data bits, no parity and 1 stop bit.
i send data using the following function
serialPort1.Write(messageBuf,0,10);
where messageBuf is declared as a byte array containing 10 bytes of data. This works perfect.
My problems is receiving the data form the uC. I use the following code for this:
byte[] messageRead = new byte[8];
serialPort1.Read(messageRead, 0, 8);
where messageRead is declared like this:
byte[] messageRead = new byte[8];
when i check the messageRead buffer i only find data in the first element. I have checked the bus using a logic analyzer and i can clearly see all the 8 bytes being sent out from the uC to the pc but for some reason my program only accepts the first element. I have tried adding delays between the bytes (now i send them immediately after eachother) and increased the serialPort1.ReadTimeout but nothing seems to work. I only recieve one byte.
the first byte arrives at the bus 3ms after request and then the following 7 bytes immediately after.
Any ideas on what im doing wrong?
Thank you!
|
|
|
|
|
OK, let's give you some ideas:
1.
Correctly and reliably receiving serial data is orders of magnitude more difficult, than sending data. Sending is easy, all it takes is a Write() and it will happen eventually. Reading is difficult as you have to read at an appropriate moment, and you may or may not get troubled by data holding messages and messages being chopped to pieces by your reading code.
2.
you did not explain when and where you execute SerialPort.Read(). Is it in a button click handler? a timer tick handler? a DataReceived handler?
3.
When it is a command-response situation, your PC app sends a command to the peripheral; first it takes a few milliseconds for the entire command to leave the PC's serial port; then the peripheral will get those bytes one by one, and probably not do much until the entire command has been received. Then it needs to react to it (interpret command, execute command, maybe wait on the termination of an analog/digital conversion operation, etc), then construct a reply message, then start sending that.
4.
If your PC app sits waiting for the response, it probably wants to react as soon as possible. So having a polling loop or simply use the DataReceived event, will launch the reading code as soon as something got received, however that something will not be the entire reply as it takes about 8 msec for it to travel the serial channel (9600Bd is roughly 1 byte per millisecond). So what to do?
5.
Here are some schemes, none is perfect, you must decide which fits your situation most:
A. not event-driven: wait long enough, then read; as in write,sleep, read.
B. text communication: the serial driver knows about text lines, if you ask for a line of text, you will obtain all characters up to the first end-of-line (You can set the port's NewLine string). This only works for text, and requires your text not to contain an accidental end-of-line in the middle of the data.
C. event-driven: use DataReceived event, and in its handler sleep long enough to cover the time span from first byte to last byte, say 8 milliseconds. Disadvantages: your sleep time will be rounded up to probably 10 to 30 msec (see my timers article), and if your peripheral hesitates in the middle of its transmission, you still won't get it all.
D. use DataReceived event, and don't read until there are enough bytes available; then read. This will fail I guess, as there is no guarantee that each and every incoming byte will cause a DataReceived event.
E. Buffer it yourself: collect incoming data, independent of what you expect; then investigate what you have so far, and when it makes sense, consume it. When it does not, either wait for more, or discard and wait for more.
6.
To complicate matters, communication can go wrong. The other side may suddenly fail; the cable could become disconnected; a byte could get damaged; a parity error could occur; etc. You should code defensively in general, you should do even more so in communication. Therefore (E) above is the best option, not the easiest one.
7.
Conclusion
if you can rely on a unique character to indicate the end of a message (such as a CR or LF in a text string), then that is by far the easiest way to go, as now the driver is in charge of gathering messages. Otherwise, consider your own buffering, read everything, and locate, decode and process packets in your buffer; do not expect messages to always be received correctly without problems (unless you can afford to wait long enough because nothing is urgent and messages are few and far apart).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I'm using VS 2008
From a form I pass a class (Facility ) to another class (FCalc ) in the instantiator
Facility is a partial class and has the properties with NotifyPropertyChanged in the setter
The property in question is bound to a textbox text property.
When the Facility value is updated in the FCalc class the textbox is not updated. The NotifyPropertyChanged event is fired but when I get back to the form the values between the Facility object and the textbox are different.
To try and force this through I have used the ReadValue on the DataBinding but this does not help.
txtROE.DataBindings[0].ReadValue();
I have tested the values of the textbox and the object directly after ReadValue() and they are different.
Any suggestions are welcome
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Nothing springs to mind but I found Bind Better with INotifyPropertyChanged[^], don't know if it helps/applies in your situation.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
Thanks but this looks like a wrapper around INotify to make it apply generically, useful in it's own right by not in this case!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I managed to get the desired result by dropping and rebuilding the binding
txtROE.DataBindings.Clear();
Binding oBinding = new Binding("Text", oFSet, "ROE");
oBinding.FormatString = "#,#.00";
oBinding.FormattingEnabled = true;
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hmm.... Are you certain the Facility object is not re-instantiated somewhere after the binding. That could explain this behaviour.
|
|
|
|
|
That was my first thought and no it is not. My second was to chase down any ref/value issues when passing an object.
Also by checking the values at both ends of the binding after the ReadValue() I am know the binding is still valid.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
bizarre situation indeed...
I know I'm pointing out the obvious here, but anyways you never know where you could have overlooked something.
Are you certain the NotifyPropertyChanged event is called with the correct Property name perhaps there is a casing error?
public int MyProp
{
get{return _myProp;}
set
{
_myProp = MyProp value;
NotifyPropertyChanged("MyProp");
NotifyPropertyChanged("myProp");
}
}
#region INotifyPropertyChanged Members
protected void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
|
|
|
|
|
I did check before I exclaimed, of course it is spelt correctly. Besides the bloody thing is auto generated code so I would have fallen of the chair if it was wrong.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I am currently researching licensing systems for applications developed using C# targeting the .NET Framework v3.5. Are there any licensing systems that are 100% free? I don't plan on selling software on a large scale or anything like that and I'm not terribly worried about piracy. I'm simply looking to sell software (one particular application at the moment) to a small scale audience, priced around only about $15-$20. So I'm thinking that if there are no free systems I will just write my own. Because I'm not worried too much about piracy I figured writing my own may suffice. But I thought I'd ask in order to make it a little easier.
All I am really interested in is some algorithm to see if a user's key matches a "template" so to speak. If you could provide some links to related materials I can read them on my own. I am just having trouble finding anything that is useful for what I want to do.
Thanks,
Matt U.
|
|
|
|
|
There are a few articles right here on CP about that.
Search for them.
|
|
|
|
|
What licensing systems are concerned, my experience is that systems that cost nothing are worth as much.
I just had a look at a few of the solutions here on CP, and the ones I looked at are not to be recommended. Nuff said.
|
|
|
|
|
Hi,
In my propertygrid, I want to add a textbox with a arrow at the right side which can show all previous values.
is it possible ?
thanks for your help
|
|
|
|
|
Probably, you can add vscrollbar control to your application and you can display the previous values from a stored data file like .xml or .txt. You can implement this using serialization.
|
|
|
|
|
look for UITypeEditor class in MSDN. You can do it
|
|
|
|
|
|
maybe there is a class or structure that deals with dates, and times, and time spans, and the like. If you want me to read the documentation to you, open your book and we will start at the letter 'A'.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: If you want me to read the documentation to you
Better send CODEZ that reads the documentation
Just an irritated, ranting son of ... an IT guy.
At your trolling services
|
|
|
|
|
thanx 4 rply
i dnt want u to read the documentation.
i just want to calulate time betwen two dates.
plz help me in this context.
thanx
|
|
|
|
|
I think he is suggesting (remarkably politely, it must be said) that perhaps a little, tiny, teeny, intsy-bitsy little touch of research on your part would find the answer to this rather easily.
Perhaps if there was something on a large computer network, that you could ask to look for things? If it took English questions, and gave you a list of documents which would answer the question? If it prioritised those documents into best match, less good match, and so on for you? Wouldn't that be handy!
Now, what could there be, that does that...
It's a puzzler, to be sure...
Hang on! What's that you say Sooty? There is? It's name has even become the verb to use it? What's it called then, Sooty? GOOGLE
By the way, you will probably get less abuse in the future if you:
1) Realize your keyboard has vowels.
2) Start using them.
TxtSpk is frowned upon here, as computing tends to require precision, and accuracy.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
OriginalGriff wrote: remarkably politely, it must be said
My right honorable friend, what are you suggesting here?
Keeping calm and polite, while offering a suggestion and a lot of keywords, that is me, and not remarkable.
I can't help it I'm no JSOP, CG, POH, or Led Mike.
For the interested reader who may get confused by the original question being removed (which is in violation with the forum guidelines), here it is once more:
Hi
how can i calculate minutes from 5/5/2010 7:00 PM to 6/1/2010 8:00 AM in C# ?
basically i have to find out the time difference in minutes between two transactions from the same IP Address.
Thanx
I aim to please.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Hi Luc!
I meant no slight; you are as always, polite and calm, despite considerable provocation at times. There are however, others (myself included on occasion ) who are not so considerate: the OP is lucky he was found by you and not them! It is remarkable only in that none of the usual suspects have hurled abuse at the OP for being quite so lazy.
I can recall myself instructing one gentleman to RTFM in 300 point red letters...
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
Yes, the usual suspects seem to wear down. And so do a lot of others.
I'm not sure what is going on, the Q&A question rate is low, and forum activity is low too. Is the CP audience shrinking? Did entire continents suddenly see the light and run out of questions? is it the Guatemala Syndrome? what do you think?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|