Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello
i am making a counter which works with pic micro controller. i have made counter and it works but the pulses i gave to counter through serial port with pic micro controller is very fast the counter doesn't count... i want help in counter do not want to miss any of the pulse.. plz help.

my code is following:-

VB
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel

Public Class Form1
    Delegate Sub SetTextCallback(ByVal [text] As Char)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "COM50"
        SerialPort1.BaudRate = "2600"
        SerialPort1.Open()
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())
    End Sub

    Private Sub ReceivedText(ByVal [text] As Char)
        If Me.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)

            Me.Invoke(x, New Object() {(Text)})
        Else

            If Text = "A" Then
                Label1.Text = Label1.Text + 1
                Label1.Refresh()
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Label1.Text = "0000"
    End Sub
End Class


please help
Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 23-Sep-14 15:13pm    
What's the question? The port is serial, so you should not miss anything...
—SA
Saad Sparco 23-Sep-14 15:16pm    
yes serial Com Port
Saad Sparco 23-Sep-14 15:24pm    
yes do not miss any of the received data from serial com port
Sergey Alexandrovich Kryukov 23-Sep-14 16:28pm    
I'm not asking about something that I already read in your post; I'm asking: what's the problem?
By the way, not need to check InvokeRequired. In this context, it's always required.
—SA
Saad Sparco 23-Sep-14 16:41pm    
sir the problem is that when i send data from pic micro controller some data miss.. i have added interrupt in pic micro controller but data misses in it windows application

First of all, do all the serial port activity in a separate thread. Also, use BeginInvoke, instead Invoke. For some understanding, please see my past answers:
(screwed up, please see below),
Problem with Treeview Scanner And MD5[^].

(Sorry, we are experiencing a problem with CodeProject link postings. My first link was screwed up. Please use:
"http://www.codeproject.com/Answers/159125/Control-Invoke-vs-Control-BeginInvoke#answer1".
Sorry for the inconvenience.)

I don't know all the detail. Functionality always depends on what you have on the other end of your RS-232 cable. In some devices, the control is not well implemented, and the data can be lost on the other side: the messages could be lost if you don't send timely confirmation, delay reading from the port or something like that. If that happens, experiment with a very fast application, without UI. You can even boost thread/process priority and use thread affinity to dedicate your serial communication thread to a fixed CPU code. If you do that and spot any difference in the rate of lost messages, my guess is correct. If not, there is a flaw in some logic, again, either on your side, or the side of the device connected to a serial port.

—SA
 
Share this answer
 
v7
Comments
CPallini 23-Sep-14 23:39pm    
5.
Sergey Alexandrovich Kryukov 24-Sep-14 1:30am    
Thank you, Carlo.
—SA
As an addendum to Sergey answer, you usually don't implement the counter on the PC side: if the pulse rate is high there's no way for the PC application to keep up.
The solution is counting pulses on the microcontroller side and send at 'a reasonable rate' the current count to the PC application.
 
Share this answer
 

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