Click here to Skip to main content
15,070,224 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am able to receive data from serial port, but i want to check which data is received.

Here is the code :

If int = 0 Then
    If SerialPort1.ReadExisting() = "a" Then
    'Do Something i want
    End If

So, i used "ReadExisting()" funtion for receiving data.
It allows me receive data but, not let me check what data is received.

"I want to check the received data and if data is received is same as as 'Character - a' then have to do the task i want."

Thanks in Advanced. :)
Updated 26-Dec-15 4:21am
[no name] 26-Dec-15 10:02am
Why the hell you are using a timer to read?
Protonzz Baba 26-Dec-15 10:07am
oh sorry for that, i had a condition to use timer there, forget it.
[no name] 26-Dec-15 10:13am
I think a timer for a first Approach to get familar with it is ok. But as soon you are familar with it, replace it with SerialPort.DataReceived :-)
Protonzz Baba 26-Dec-15 10:46am
can you provide the code?
[no name] 26-Dec-15 10:54am
Only in c#, but I think there is no big need to support code.
Hook into "SerialPort.DataReceived". Here you take every received char into an intermediate buffer and also check your "Trigger" for "MyDataRecievied", which seems in your case receiving an "a". If condition is there, send an Event to the class which handle the received data... and don't forget to delete the handled data from the intermediate buffer.
Protonzz Baba 26-Dec-15 11:52am
thanks for your reply, i got my answer, it wasn't the "SerialPort.DataReceived" fault.
[no name] 26-Dec-15 11:56am
Great that you found your answer. Take care while posting an answer onto your own question. Some members here have the Feeling of needing downvote such things. At least do not accept your own answer, then you shuld be on the safe side ;)
Protonzz Baba 26-Dec-15 12:03pm
[no name] 26-Dec-15 12:06pm
Don't understand me wrong. From my Point of view it is very ok to post "my own soltuion", because it helps also others. I will upvote it if it makes sense for me :-)
Protonzz Baba 26-Dec-15 12:08pm
Yeah, your view is totally right. :)

1 solution

Remember that when you read data from a serial port, you get all available characters - so if the device at the other end sends "a" followed by a new line, you will receive two or three characters, not one. So your test may not be workign because you are gettign too much data!

Have a look: use the debugger to put a breakpoint on the start of the method, and "break out" what you are doing to make looking at it easier:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If int = 0 Then
            Dim read = SerialPort1.ReadExisting()
            if read = "a" Then
                'Do Something i want
            End If

Look at read and see exactly what you are getting.
[no name] 26-Dec-15 10:02am
I think using a timer is a very bad decision here....
Protonzz Baba 26-Dec-15 10:14am
forget the timer. I want the timer for something there. Its not the Timer issue.
I tried this code outside the timer but it still doesn't work.
OriginalGriff 26-Dec-15 10:22am
I'd agree - but let's see if he can receive anything at all before we start sorting out the rest of his design! :laugh:
[no name] 26-Dec-15 10:26am
Yes, sorry, I commented to fast without thinking first. Timer is very ok to get a "first Feeling"/make some first trials.
Sergey Alexandrovich Kryukov 26-Dec-15 11:55am
No, it's not ok at all. I would say, after one reads a port in a timer event, there is nothing to discuss. It's a more principle point then the port operations themselves. See my other comment below.
Protonzz Baba 26-Dec-15 10:16am
Thanks @OriginalGriff but your given is not working.
Can you explain in detail what you want to say
OriginalGriff 26-Dec-15 10:21am
"is not working" is the least helpful error report you can give!
Remember, I can't see your screen - I only get to work with exactly what you tell me.
So what happened when you looked in the debugger?
Protonzz Baba 26-Dec-15 10:26am
it didn't show me the errors, but its not doing anything.
I will add the code.

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim read = SerialPort1.ReadExisting()
If read = "a" Then
End If
End Sub

i added a button and put the code in it.
OriginalGriff 26-Dec-15 10:33am
I don;t expect it to work - first we have to find out what *is* happening.
So what do you get in "read" when you look in the debugger?
Protonzz Baba 26-Dec-15 10:44am
Nothing :P
OriginalGriff 26-Dec-15 10:55am
Then that's the start of your problem.
The first thing you need to do is make sure you can receive some data - because until you do you can't compare it with anything!

So start with Hyperterminal or similar, and get it working that way - that starts "eliminating variables" before you start trying to code your own software.
Protonzz Baba 26-Dec-15 11:08am
wait, your code is working with different project.
but not this one.
OriginalGriff 26-Dec-15 11:19am
So...what are the differences?
Protonzz Baba 26-Dec-15 12:06pm
Actually, i used "SerialPort1.ReadExisting()" somewhere else in code for testing purposes. I just removed it and TADA it worked. Also if we didn't use "read" variable, it works. and Also thanks, for letting me know where wrong i am.
OriginalGriff 26-Dec-15 12:13pm
"Also if we didn't use "read" variable, it works"
Yes, it will - it just makes it easier to see what is being received in the debugger, is all.
"thanks, for letting me know where wrong i am"
You're welcome!
Sergey Alexandrovich Kryukov 26-Dec-15 11:52am
Unfortunately, there is nothing worse than reading anything by timer. There can be different, robust and safe approaches. It would be much better, say, to just read unconditionally in a separate thread, so the read operation will be a blocking call.
[no name] 26-Dec-15 12:02pm
It is bad by timer, but still I think for a beginner a controllable/intuitive way to observe the situation. No reason to downvote the help of OG that much anyway. Only my mind. Thank you, Bruno
Sergey Alexandrovich Kryukov 26-Dec-15 21:37pm
How "bad" and really unsafe way could possibly be "controllable", even if it is "intuitive"? I'm not sure you really considered all consequences of such approach...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900