Click here to Skip to main content
15,502,349 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 :

VB.NET
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. :)
Posted
Updated 26-Dec-15 5:21am
v2
Comments
[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.

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:
VB.NET
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.
 
Share this answer
 
Comments
[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.
—SA

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