Thank Dr Acula - I really appreciate you trying to help. I like how compact the code is, but I think it needs tweeking - at least based on my limited understanding of VB.net...
For the benefit of people not looking on Instructables, I'm including my results in both places.
I have tried to isolate the differences between 2 results, Acula's and a couple of other programs (Hyperterminal and another vb program. Hyperterminal is fine for those who have it, but I can't modify it and it is only good for testing.. The vb program is quite complex and would take a great deal of extra work to modify - I'm praying I don't have to go down that route).
Acula's will be perfect once I can fix what I'm doing wrong - hopefully the community can help me out...
I'm including all of the code plus the image outputs that shows what is going on...
From VB
form objects:
Text Name
Button1 Button1
Button2 Button2
PictureBox1
Counter lblCounter
Exit btnExit
txtData - multiline text box, with vertial scrolling
plus a timer
Imports System.IO
Imports Microsoft.Win32
Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for strings
Public Class Form1
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer) ' for sleep statements
Dim WithEvents serialPort As New IO.Ports.SerialPort ' serial port declare
Dim PicaxeRegisters(0 To 13) As Byte ' registers b0 to b13
Dim count As Integer = 1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Enabled = True ' put this in code as defaults to false when created
Timer1.Interval = 5000 ' 5 seconds
PictureBox1.BackColor = Color.Red ' set to position 'red'
Array.Clear(PicaxeRegisters, 0, 13) ' probably not needed as array declared blank
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' timer ticks every 5 seconds
Call SerialTxRx() ' talk to picaxe
lblCounter.Text = count
count = count + 1
End Sub
Sub SerialTxRx()
Dim LabelString As String = "" ' string to display byte values
Dim DataPacket(0 To 17) As Byte ' entire data packet "Data"+14 bytes
Dim i As Integer ' i is always useful for loops etc
'lblData.Text = "" ' clear the text on the screen
For i = 0 To 3
DataPacket(i) = Asc(Mid("Data", i + 1, 1)) ' add the word "Data" to the packet
Next
For i = 0 To 13
DataPacket(i + 4) = PicaxeRegisters(i) ' add all the bytes to the packet
Next
If serialPort.IsOpen Then
serialPort.Close() ' just in case already opened
End If
Try
With serialPort
.PortName = "COM3" ' Most new computers default to com1 but any pre 1999 computer with a serial mouse will probably default to com2
.BaudRate = 2400 ' 2400 is the maxiumum speed for small picaxes
.Parity = IO.Ports.Parity.None ' no parity
.DataBits = 8 ' 8 bits
.StopBits = IO.Ports.StopBits.One ' one stop bit
.ReadTimeout = 1000 ' milliseconds so times out in 1 second if no response
.Open() ' open the serial port
.DiscardInBuffer() ' clear the input buffer
.Write(DataPacket, 0, 18) ' send the datapacket array
Call Sleep(700) ' 100 milliseconds minimum to wait for data to come back and more if data stream is longer
.Read(DataPacket, 0, 18) ' read back in the data packet array
.Close() ' close the serial port
End With
For i = 4 To 17
LabelString = LabelString + " " + Chr(DataPacket(i)) ' turn into a text string
Next
'lblData.Text = LabelString ' put the text string on the screen
txtData.Text += LabelString + vbCrLf
Catch ex As Exception
'MsgBox(ex.ToString)' uncomment this if want to see the actual error message
'lblData.Text = "Timeout" ' will display this if picaxe not connected etc
txtData.Text += "Timeout" + vbCrLf
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.BackColor = Color.Red ' change the box to red
PicaxeRegisters(0) = 120 ' an arbitrary value for the servo
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PictureBox1.BackColor = Color.Green ' box to green
PicaxeRegisters(0) = 160 ' arbitrary value for the servo
End Sub
Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
serialPort.Close()
Close()
End Sub
End Class
From picaxe:
'**************************************************************************
; set picaxe type
#picaxe 28x1 'declarative
#com 4
symbol counter = b0
symbol ledstatus = b1
symbol ledport = b2
counter = 0
ledstatus = 1
ledport = 7
; *****************************************************
; *****************************************************
main:
if counter > 250 then
counter = 0
end if
counter = counter + 1
serout 0,9600,("Data", #counter,"","",b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13)
if ledstatus = 1 then
high portc ledport
pause 500' pause a second
ledstatus = 0
end if
if ledstatus = 0 then
low portc ledport
pause 500' pause a second
ledstatus = 1
end if
goto main