Shop OBEX P1 Docs P2 Docs Learn Events
One click - two data transmission from VB to Basic Stamp 2 — Parallax Forums

One click - two data transmission from VB to Basic Stamp 2

R0y4LR0y4L Posts: 23
edited 2009-09-16 22:05 in BASIC Stamp
Hi everyone

So, I have finally managed my VB application to send data to BS 2. It's all working and fine. The VB app suppose to send two digits through serial port to BS 2, and the BS 2 suppose to insert those digits into different memory location in the EEPROM. I am trying to achieve all of this with one button click, however I have been unsuccessfull, so I thought to ask for some help. How can I make my "thing" to send two pieces of data with one click?

This is my VB code. As you can see I have no SerialPort connection on this form, but don't worry it is linked to a form that has SerialPort connection and its working fine.
 Private Sub cmdSet_Clock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetClock.Click

        If txtClockHours.Text = "" Or txtClockMinutes.Text = "" Then

            MsgBox("Please check time entered!", MsgBoxStyle.OkOnly, "Clock Time")
            txtClockHours.Select()

        Else


            Dim hours As Byte = txtClockHours.Text
            frmMainGUI.SerialPort1.WriteLine(hours)
            Dim mins As Byte = txtClockMinutes.Text
            frmMainGUI.SerialPort1.WriteLine(mins)

            lblClock_Status.Text = "Clock Has Been Successfully Updated"

        End If

    End Sub




This is my PBASIC code:


mins VAR Byte
hours VAR Byte
speaker CON 9 

start:

  SERIN 16, 16468,[noparse][[/noparse]DEC hours]

  WRITE 101, hours

  SERIN 16, 16468, [noparse][[/noparse]DEC mins]

  WRITE 100, mins

  FREQOUT speaker, 100,2000

GOSUB start   





I have added the FREQOUT statement so, I know if the second SERIN worked.

Thanks

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit: www.vbnoobs.co.uk

Comments

  • davejamesdavejames Posts: 4,047
    edited 2009-09-01 18:20
    ...mmmmmm, that "GOSUB start" makes me nervous. I'd suggest changing it to "GOTO start" and see if it affects anything.

    When you say you were unsuccessful, what does that mean? No data in 100 or 101? Incorrect data in 100 or 101?


    DJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Instead of:

    "Those who can, do.· Those who can't, teach." (Shaw)
    I prefer:
    "Those who know, do.· Those who understand, teach." (Aristotle)
    ·
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2009-09-01 18:30
    R0y4L,

    Your question is a little confusing.... are you saying that SERIN 16, 16468,[noparse][[/noparse]DEC hours] picks up hours, but SERIN 16, 16468, [noparse][[/noparse]DEC mins] does not receive the mins? If so, it could be that you need to throttle the data that you are sending from the PC. In other words pause for a little bit from the PC side. Remember that the BS2 does not have a serial receive buffer and can only receive data as it is available.

    Hard to tell without more of your code, but that's my guess.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • R0y4LR0y4L Posts: 23
    edited 2009-09-01 18:50
    Sorry for not describing my code (I should of really).

    davejames:

    When I say unsuccessful, I mean no data in the 100. Basically if I press a button on my VB app value for hours is sent to the BS, but no mins value.If I press the same button again, the mins value does get sent.

    Beau:
    Yes, you are correct: hours does get picked up but not the mins. I will try to work out how I could pause in VB. What do you mean by "serial receive buffer"?


    Thank you

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit: www.vbnoobs.co.uk
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-09-01 18:59
    Hi , heres an example that uses an exclamation as synchronization.

    VB
    Private Sub cmdSet_Clock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetClock.Click
    
            If txtClockHours.Text = "" Or txtClockMinutes.Text = "" Then
    
                MsgBox("Please check time entered!", MsgBoxStyle.OkOnly, "Clock Time")
                txtClockHours.Select()
    
            Else
    
    
                Dim hours As Byte = txtClockHours.Text
     
               [s] frmMainGUI.SerialPort1.WriteLine("!")   'additional code
    [/s]
                frmMainGUI.SerialPort1.WriteLine("!" & hours)
                Dim mins As Byte = txtClockMinutes.Text
                frmMainGUI.SerialPort1.WriteLine(mins)
    
                lblClock_Status.Text = "Clock Has Been Successfully Updated"
    
            End If
    
        End Sub
    


    Stamp (partial code)
    SERIN 16, 16572,[noparse][[/noparse]WAIT("!"),DEC3 hours,DEC3 mins]
    PAUSE 50
    

    Note a baud of 4800

    Although the Stamp will accept upto 3 characters for each value the "Writeline" of VB will terminate the string at 1 , 2 or 3 characters , whatever you send

    Jeff T.

    EDIT I decided to make a small change , should work ok.
  • davejamesdavejames Posts: 4,047
    edited 2009-09-01 18:59
    ROy4l,

    Investigate the VB Sleep() command. I use it in between serial port operations to the Stamp.


    I've recently found that serial communication, VB to/from Stamp, is not a trivial thing...

    DJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Instead of:

    "Those who can, do.· Those who can't, teach." (Shaw)
    I prefer:
    "Those who know, do.· Those who understand, teach." (Aristotle)
    ·
  • davejamesdavejames Posts: 4,047
    edited 2009-09-01 19:03
    ...ahhhh, Unsoundcode to the rescue - again!

    (smiles)

    DJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Instead of:

    "Those who can, do.· Those who can't, teach." (Shaw)
    I prefer:
    "Those who know, do.· Those who understand, teach." (Aristotle)
    ·
  • R0y4LR0y4L Posts: 23
    edited 2009-09-01 19:12
    Thank you everyone. I have used the Sleep statement in VB. It goes like this: "Threading.Thread.Sleep(1000)". I have put this between when the hours are sent and mins variable declared. Did the job. Thank you everyone again. I am going to investigate that "!" and WAIT way of transmitting data too. I have never used it because I did not understood it. Thank you all again for your suggestions [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit: www.vbnoobs.co.uk
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-01 21:56
    I am always puzzled why the slowest service/device (relative to everything else in PC) as serial communication gives programmers such grief. Instead of introducing another slow component – delay - why not utilize Stamps SERIN qualifier STR?
    I would even say that this particular qualifier is the often-stated as missing “serial buffer” of Stamp.
    On ather subject – mixing variable definitions (your Dim statements in your VB code) with code flow is not very good practice. Unfortunately VB is too forgiving.
    Keep coding Vaclav
  • allanlane5allanlane5 Posts: 3,815
    edited 2009-09-01 22:27
    If you use a single SERIN statement:

    SERIN 16, 16468,[noparse][[/noparse]DEC hours, DEC mins]

    that should work better. What was happening is the delay while the BS2 wrote to the eeprom (which DOES take a while, like a millisecond or so) it would miss the beginning of the 'mins' serial stream. The BS2 can't recieve serial unless it's waiting in a SERIN command.

    This does assume you 'terminate' the 'hours' value with a '\n' (vbCrLF).· Which I assume the "WriteLine" does.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-09-01 23:46
    Writeline does indeed terminate with a newline character ( a value of 10 ) as the default , this could be changed to another value if you wished.

    The idea of using a header such as the exclamation to synch up the data is always a good idea·. It prevents a situation where the SERIN misses the first byte and loads the minutes value into the hour variable , SERIN would just sit there then waiting for the next byte which would be the hour value at the next button click , that value would get loaded into the minute variable

    Thats why sometimes you might see the correct values in the wrong slot.

    Jeff T.
  • R0y4LR0y4L Posts: 23
    edited 2009-09-08 20:40
    vaclav_sal:

    You got me interested with the STR bit. Any tips where i could find more info about it or how i could implemented in my code that i have provided above. I have Basic Stamp book, but i could not find any reference to STR.

    Thank you

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit: www.vbnoobs.co.uk
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-09 03:55
    It is in OnLIne help for SERIN - kind of in the middle. Here is a copy.
    The "character string" term ·is litlle· missleading. It should say "bytes into byte array".

    attachment.php?attachmentid=73799
    780 x 229 - 15K
  • Mike2545Mike2545 Posts: 433
    edited 2009-09-16 22:05
    I have been using VB 2008, I could not find a 'sleep' command so I had to make a sub that kept the CPU busy for awhile.

    One click and I can set the time, and date on a DS 1340

    VB code:

    Public Class Form1
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myhour As String
            Dim myminute As String
            Dim mysecond As String
            Dim myday As String
            Dim _myday As Integer
            Dim mydate As Integer
            Dim mymonth As String
            Dim _mymonth As Integer
            Dim myYear As Integer
    
            myhour = TimeOfDay.Hour
            myminute = TimeOfDay.Minute
            mysecond = TimeOfDay.Second
            myday = _Day.Text
    
            mydate = Val(_date.Text)
            mymonth = _month.Text
            myYear = Val(_Year.Text)
            If myday = "Sunday" Then _myday = 1
            If myday = "Monday" Then _myday = 2
            If myday = "Tuesday" Then _myday = 3
            If myday = "Wednesday" Then _myday = 4
            If myday = "Thursday" Then _myday = 5
            If myday = "Friday" Then _myday = 6
            If myday = "Saturday" Then _myday = 7
            If mymonth = "January" Then _mymonth = 1
            If mymonth = "Feburary" Then _mymonth = 2
            If mymonth = "March" Then _mymonth = 3
            If mymonth = "April" Then _mymonth = 4
            If mymonth = "May" Then _mymonth = 5
            If mymonth = "June" Then _mymonth = 6
            If mymonth = "July" Then _mymonth = 7
            If mymonth = "August" Then _mymonth = 8
            If mymonth = "September" Then _mymonth = 9
            If mymonth = "October" Then _mymonth = 10
            If mymonth = "November" Then _mymonth = 11
            If mymonth = "December" Then _mymonth = 12
            If myYear = 2009 Then myYear = 9
            If myYear = 2010 Then myYear = 10
            If myYear = 2011 Then myYear = 11
            If myYear = 2012 Then myYear = 12
            If myYear = 2013 Then myYear = 13
            If myYear = 2014 Then myYear = 14
            If myYear = 2015 Then myYear = 15
            If myYear = 2016 Then myYear = 16
            If myYear = 2017 Then myYear = 17
            If myYear = 2018 Then myYear = 18
            If myYear = 2019 Then myYear = 19
            If myYear = 2020 Then myYear = 20
    
            If _port.IsOpen Then
            Else
                SerialPort1.Open()
                _port = SerialPort1
            End If
            _port.WriteLine(250) ' set  clocks
            Call Pause()
            Call Pause()
    
            _port.WriteLine(myhour)
            Call Pause()
            _port.WriteLine(myminute)
            Call Pause()
            _port.WriteLine(mysecond)
            Call Pause()
            _port.WriteLine(_myday)
            Call Pause()
            _port.WriteLine(mydate)
            Call Pause()
            _port.WriteLine(_mymonth)
            Call Pause()
            _port.WriteLine(myYear)
    
    
            _port.Close()
        End Sub
        Public Sub Pause()
            Dim mypause As Integer
            For mypause = 1 To 62000000
            Next
            'For mypause = 1 To 40000000
            'Next
        End Sub
    



    and some corresponding basic stamp code

    
     #SELECT $stamp
    #CASE BS2P,BS2E,BS2PE
    
    t38k4 CON 45
    #ENDSELECT
    inverted CON $4000
    
    baud CON t38k4 + inverted
    
    SDA  PIN   0                 ' I2C SDA pin
    'SCL  PIN   SDA+1             ' I2C SCL pin
    I2C_WR CON  $D0              ' I2C write address
    I2C_RD CON  $D1              ' I2C read address
    
    'Readtime VAR Bit             ' Set/Read time flag
    Second VAR Byte              ' Store second value
    Minute VAR Byte              ' Store minute value
    Hour VAR Byte                ' Store hour value
    Day VAR Byte                 ' Store day value
    Date VAR Byte                ' Store date value
    Month VAR Byte               ' Store month value
    Year VAR Byte                ' Store year value
    
    [color=#990000]   I had a serin here to look at what I was sending and in this case the #250 meant I want to set the clock [/color]
    '**************************************************************
    ' Set time subroutine
    '**************************************************************
    
    Settime:    '250
    
        SERIN 15\14, baud,[noparse][[/noparse]HEX2 Hour]
       Hour = Hour & %00111111      ' Disable century
    
        SERIN 15\14, baud,[noparse][[/noparse]HEX2 Minute]
        SERIN 15\14, baud,[noparse][[/noparse]HEX2  second]
       Second = Second & %01111111  ' Enable oscillator   1000,_day,  1000,_date,  1000,_month,  1000,_year,
        SERIN 15\14, baud,[noparse][[/noparse]HEX2  day]
        SERIN 15\14, baud,[noparse][[/noparse]HEX2  date]
        SERIN 15\14, baud,[noparse][[/noparse]HEX2  Month]
        SERIN 15\14, baud,[noparse][[/noparse]HEX2 year]
        I2COUT SDA, I2C_WR, [noparse][[/noparse]0,Second,Minute,Hour,Day,Date,Month,Year]
        DEBUG CR, CR, "The current time has been successfully set!", CR, CR
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike2545

    This message sent to you on 100% recycled electrons.

    Post Edited (Mike2545) : 9/16/2009 10:18:11 PM GMT
Sign In or Register to comment.