Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Visual basic (express i guess)


  • Please log in to reply

#1
dsenette

dsenette

    Je suis Napoléon!

  • Community Leader
  • 26,047 posts
  • MVP
trying to write some code (very poorly by the way) that will send an ascii string out to the serial port to control a relay box that i've got...

basically what my process (logical) is:

press the go button
sends string1 to the serial port
waits 3 seconds
sends string2
waits 3 seconds
sends string 3
waits 5 seconds
sends string4
waits 3 seconds
sends string5
waits 3 seconds
sends string6
waits 8 seconds
sends string 7
waits 5 seconds
sends string 8
waits 1 second
sends string 9
waits 1 second
sends string 10
waits 1 second
sends string 11
waits 1 second
sends string 12
waits 1 second
sends string 13
waits 1 second
sends string 14
waits 1 second
sends string 15
waits 1 second
sends string 16
waits 1 second
sends string 17
waits 1 second
sends string 18
done

i've found the method for sending the data to the com port...so that's not the issue...my issue is that i've NEVER been good at timers...they never work for me...and i've never had to use this complex of a timer structure....any suggestions?
  • 0

Advertisements


#2
dsenette

dsenette

    Je suis Napoléon!

  • Topic Starter
  • Community Leader
  • 26,047 posts
  • MVP
i've made a little headway with the vb help file...here's what i've got..and a slight explanation

the things in () after the .writeline portions are codes for the controller that i'm using for this project...N turns a relay on F turns a relay off and the number following it corresponds to the relay that i'm wathing to be turned on/off..there's a counter that increments on each tick...set up an if then/else if kind of loop that will incriment the counter...then based on the value of the counter...will change the increment of the timer (say from 3 seconds to 500 milliseconds back to 10 seconds...etc)...right now the only way i know it's working is by printing something to a label when the loop increments...so...i think it's working...i mean...it runs with no errors...and the label changes at the proper time...anyone see a cleaner way to do this?

Public Class Form1
	' This variable will be the loop counter.
	Private counter As Integer

	Private Sub InitializeTimer()
		' Run this procedure in an appropriate event.
		counter = 0
		Timer1.Interval = 1000
		Timer1.Enabled = True
	End Sub

	Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

		If counter <= 0 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N01")
			End Using
			Timer1.Interval = 3000
			counter = counter + 1
			Label1.Text = "^fire #1^"

			REM ^fire #1^

		ElseIf counter = 1 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N02")
				com1.WriteLine("F01")
			End Using
			Timer1.Interval = 3000
			counter = counter + 1
			Label1.Text = "^fire #2 and turn off #1 ^"

			REM ^fire #2^

		ElseIf counter = 2 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N03")
				com1.WriteLine("F02")

			End Using
			Timer1.Interval = 3000
			counter = counter + 1
			Label1.Text = "^fire #3 and turn off #2^"

			REM ^fire #3 and turn off #1^

		ElseIf counter = 3 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N04")
				com1.WriteLine("F03")
			End Using
			Timer1.Interval = 3000
			counter = counter + 1
			Label1.Text = "^fire #4 and turn off #3^"

			REM ^fire #4 and turn off #2^

		ElseIf counter = 4 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N05")
				com1.WriteLine("F04")
			End Using
			Timer1.Interval = 3000
			counter = counter + 1
			Label1.Text = "^fire #5 and turn off #4^"

			REM ^fire #5^

		ElseIf counter = 5 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N06")
				com1.WriteLine("F05")
			End Using
			Timer1.Interval = 10000
			counter = counter + 1
			Label1.Text = "^fire #6 and turn off #05^"

			REM ^fire #6^

		ElseIf counter = 6 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N07")
				com1.WriteLine("F06")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #7 and turn off #06^"

			REM fire ^#7^

		ElseIf counter = 7 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N08")
				com1.WriteLine("F07")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #8 and turn off #07^"

			REM fire ^#8^

		ElseIf counter = 8 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N09")
				com1.WriteLine("F08")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #9 and turn off #08^"

			REM fire ^#9^

		ElseIf counter = 9 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N10")
				com1.WriteLine("F09")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #10 and turn off #09^"

			REM fire ^#10^

		ElseIf counter = 10 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N11")
				com1.WriteLine("F10")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #11 and turn off #10^"

			REM fire ^#11^

		ElseIf counter = 11 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N12")
				com1.WriteLine("F11")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #12 and turn off #11^"

			REM fire ^#12^

		ElseIf counter = 12 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N13")
				com1.WriteLine("F12")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #13 and turn off #12^"

			REM fire ^#13^


		ElseIf counter = 13 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N14")
				com1.WriteLine("F13")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #14 and turn off #13^"

			REM fire ^#14^

		ElseIf counter = 14 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N15")
				com1.WriteLine("F14")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #15 and turn off #14^"

			REM fire ^#15^

		ElseIf counter = 15 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N16")
				com1.WriteLine("F15")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #16 and turn off #15^"

			REM fire ^#16^

		ElseIf counter = 16 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N17")
				com1.WriteLine("F16")
			End Using
			Timer1.Interval = 500
			counter = counter + 1
			Label1.Text = "^fire #17 and turn off #16^"

			REM fire ^#17^

		ElseIf counter = 17 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N18")
				com1.WriteLine("F17")
			End Using
			Timer1.Interval = 8000
			counter = counter + 1
			Label1.Text = "^fire #18 and turn off #17^"

			REM fire ^#18^

		ElseIf counter = 18 Then
			Using com1 As IO.Ports.SerialPort = _
			My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("N01")
				com1.WriteLine("N02")
				com1.WriteLine("F18")
			End Using
			Timer1.Interval = 3000
			counter = counter + 1
			Label1.Text = "^fire snakes^"
			REM ^fire snakes^

		ElseIf counter = 19 Then
			Using com1 As IO.Ports.SerialPort = _
		   My.Computer.Ports.OpenSerialPort("COM1")
				com1.WriteLine("F00")
			End Using
			Label1.Text = "^count your fingers^"

			REM ^count your fingers^ 

		End If
	End Sub




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

	End Sub
End Class

  • 0

#3
dsenette

dsenette

    Je suis Napoléon!

  • Topic Starter
  • Community Leader
  • 26,047 posts
  • MVP
i guess no one likes monkey programming...<harumpfh>

ANYWAY i've gotten pretty far with this contraption now...i guess there's a little background needed

i'm setting up a fireworks display with two of these which are really nifty if you do any electrical hobby stuff..it's basically a serial port conrolled box of relays...nothing fancy...but you can do really nifty stuff with it...anyway...i'm using it to activate model rocket igniters to fire mortar style fireworks (and two of the best fireworks EVER created...the texas rattlesnake....if you see one at the fireworks store...by at least one...i suggest 3)

so the program loads a splash page...so that i have time to adjust some things before starting the timer (because...once it's started...i don't want to be any where near the thing)..timer starts and the program goes..as the program runs...a counter is incremented which allows the program to reset the timer.interval property so that i can vary the times between firing....as each part of the process runs...it changes the color of a button on a graphical representation of the firing plan so that i can see which one should be going off (that way if it didn't go off...i know where to point my fire extinguisher before i walk up there)...once it's done it closes the com port...

i'm pretty much to a place where i COULD call this thing done...but.. i'd like to have an actual countdown timer on the form so that i can see the full duration of the show (i've tried...and i can't get that timer to actually work...it just cycles the numbers way to fast), and it would be nice to be able to read from the serial port at the same time as the rest of this is going...(every time i try this last bit the program locks)

i've attached the actual project in case anyone want's to take a look...or in case someone has this relay box and needs a starter program
  • 0

#4
Hai Mac

Hai Mac

    Member

  • Member
  • PipPipPip
  • 260 posts
Holy Zarquon! There is only one word I can say: Reallynifty! :whistling:
  • 0

#5
dsenette

dsenette

    Je suis Napoléon!

  • Topic Starter
  • Community Leader
  • 26,047 posts
  • MVP
as an update on the project...i was able to get my second timer running and displaying (apparently when you don't do it right....it doesn't work...who knew?) so now i can track total time (also another timer that initiates a lable change to tell me how long till the next shot....TOTALLY useless...but so much fun)

only thing left is reading from the serial port while still being able to write to it...seems like opening the port to read....either occupies the port in such a way as to dissallow writing....OR it's sitting there waiting for input and not allowing the counter to cycle...i guess it could also be that during testing....i've got absolutely no input comming in...but the code i tried had an "if there's nothing comming in...quit listening" kind of deal in it...
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP