Results 1 to 3 of 3

Thread: Can't Get the current data

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    2

    Can't Get the current data

    I am doing the VB programming for my microcontroller to communicate with PC.
    The microcontroller have set the program which the PC send the command signal "character Q" and microcontroller will transmit the data in ASCII code (0 to 255) to PC. From the ASCII code, I will convert it to value and do some calculation to get my exact number.

    The problem is the 0 showed when I click for get the first data. Then, the second displayed data was the previous one.

    e.g. the 1st data suppose is 56 but the 0 showed when I click to get data at first time.
    Then, I click again, it showed the 56 which is the previous data.
    Suppose the data for that time is 255!!

    I have using the hyperterminal to test my hardware and it showed the current data all the time when I click on Q button and no 0 showed at first time clicking. I think that was something problem with my coding.

    How to get the current data from it???
    I post my code for expert like u to give me guidance and tell me the mistake.
    Thanks for helping me.

    Private Sub cmdStart_Click() 'GET DATA
    Dim ADCValue As String
    Dim SolarValue As Double

    MSComm1.Output = "Q"
    X = MSComm1.Input
    Z = (Val(X) * 4.4)
    lblDisplay.Caption = Z
    End Sub

    Private Sub cmdStop_Click() 'stop timer
    lblDisplay.Caption = ""
    cmdStart.Enabled = True
    cmdStop.Enabled = False
    End Sub

    Private Sub Form_Load() 'when project 1st load
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.PortOpen = True 'open the COMM port
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False 'close the COMM port
    End Sub

  2. #2
    Hyperactive Member gtilles's Avatar
    Join Date
    Dec 2004
    Location
    Planet Earth
    Posts
    394

    Re: Can't Get the current data

    you need a delay between outputting and inputting to give the controller time to respond.

    I believe about 50 ms is enough. It has been awhile but I think you can you Sleep for your delay but I beleive there may be a better function to create delay for your needs.

    VB Code:
    1. Private Sub cmdStart_Click() 'GET DATA
    2. Dim ADCValue As String
    3. Dim SolarValue As Double
    4.  
    5. MSComm1.Output = "Q"
    6. 'you need delay here before reading
    7. 'to give time for a response.
    8. 'also good idea to clear the buffer first
    9. X = MSComm1.Input
    10. Z = (Val(X) * 4.4)
    11. lblDisplay.Caption = Z
    12. End Sub
    Last edited by gtilles; Dec 13th, 2006 at 11:51 AM.
    Truly, you have a dizzying intellect.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Can't Get the current data

    Or use the OnComm event so you're only looking at data when you have some.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width