Step 8Add some code
Imports System.IO
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 ModifyFlag As Boolean
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' need all this rubbish stuff - .net puts it in automatically when go form1events above/load
Timer1.Enabled = True ' put this in code as defaults to false when created
Timer1.Interval = 20000 ' every 20 seconds
PictureBox1.BackColor = Color.Gray ' start with the comms boxes gray
PictureBox2.BackColor = Color.Gray
ModifyFlag = False ' if modify a value manually then skip download
RichTextBox1.Multiline = True ' so can display more than one line
Call DisplayPicaxeRegisters() ' display the 14 registers
Call ReadFTPFilename() ' read the filename off the disk (resaved every 20 secs)
End Sub
Sub SerialTxRx()
Dim DataPacket(0 To 17) As Byte ' entire data packet "Data"+14 bytes
Dim i As Integer ' i is always useful for loops etc
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 = "COM1" ' 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(300) ' 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
PicaxeRegisters(i - 4) = DataPacket(i) ' move the new data packet into the register array
Next
PictureBox1.BackColor = Color.GreenYellow ' working
Catch ex As Exception
PictureBox1.BackColor = Color.Red ' not working
End Try
End Sub
Sub FTPUpload(ByVal Filename As String)
Dim localFile As String 'place to store data
Dim remoteFile As String ' filename is case sensitive this is really important
Const host As String = "ftp://ftp.0catch.com" ' note the 0 is a zero not a character O
Const username As String = "picaxe.0catch.com"
Const password As String = "picaxetester"
Dim URI As String
localFile = Filename ' maybe not needed but if define a location eg c:\mydirectory can add easily this way
remoteFile = "/" + Filename ' file on ftp server needs "/" added in front
URI = host + remoteFile
Try
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(username, password) ' log in
ftp.KeepAlive = False ' will be disconnecting once done
ftp.UseBinary = True ' use binary comms
ftp.Timeout = 9000 ' timeout after 9 seconds - very useful as ftp sometimes dies
'timeout (and the clock frequency of 20 secs) may need to be slower for dialup connections
ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile ' start sending file
Dim fs As New FileStream(localFile, FileMode.Open) ' open local file
Dim filecontents(fs.Length) As Byte ' read into memory
fs.Read(filecontents, 0, fs.Length)
fs.Close() ' close the file
Dim requestStream As Stream = ftp.GetRequestStream() ' start ftp link
requestStream.Write(filecontents, 0, filecontents.Length) ' send it
requestStream.Close() ' close the link
PictureBox2.BackColor = Color.GreenYellow ' change the box to green to say worked ok
Label2.Text = "FTP Connected" ' text saying it connected
Catch 'can't connect
PictureBox2.BackColor = Color.Red ' box to red as no connection
Label2.Text = "FTP Upload Fail" ' text saying connection failed
End Try
End Sub
Sub FTPDownload(ByVal Filename As String)
' downloads remotefile to localfile
Dim localFile As String 'place to store data
Dim remoteFile As String ' filename is case sensitive this is really important
Const host As String = "ftp://ftp.0catch.com"
Const username As String = "picaxe.0catch.com"
Const password As String = "picaxetester"
Dim URI As String
'localFile = "C:\" + Filename ' store in root directory but can change this
localFile = Filename ' so can add c:\ if need to define actual location
remoteFile = "/" + Filename ' added to remote ftp location
URI = host + remoteFile ' make up full address
Try
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(username, password) ' log in
ftp.KeepAlive = False ' will be disconnecting after finished
ftp.UseBinary = True ' binary mode
ftp.Timeout = 9000 ' timeout after 9 seconds
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile ' download a file
' read in pieces as don't know how big the file is
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length) ' piece from ftp
fs.Write(buffer, 0, read) ' and write to file
Loop Until read = 0 ' until no more pieces
responseStream.Close() ' close the ftp file
fs.Flush() ' flush clear
fs.Close() ' and close the file
End Using
responseStream.Close() ' close it even if nothing was there
End Using
response.Close()
PictureBox2.BackColor = Color.GreenYellow ' green box as it worked
Label2.Text = "FTP Connected" ' and text saying it worked
End Using
Catch ' put error codes here
PictureBox2.BackColor = Color.Red ' red box as it didn't work
Label2.Text = "FTP Download Fail" ' and message to say this
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If ModifyFlag = False Then 'if user changed a byte then don't download
Label3.Text = "Downloading"
System.Windows.Forms.Application.DoEvents() ' so new label text displays
Call FTPDownload(TextBox3.Text) ' download remote file
Label3.Text = "Downloaded"
System.Windows.Forms.Application.DoEvents()
Call ReadRemoteFileToRegisters() ' save file numbers to the register array
Label3.Text = "Talking to picaxe"
System.Windows.Forms.Application.DoEvents()
Else
ModifyFlag = False 'reset the flag
End If
Call SerialTxRx() ' send to the picaxe and read it back
Label3.Text = "Sent and recieved from picaxe"
System.Windows.Forms.Application.DoEvents()
Call DisplayPicaxeRegisters()
Call SaveRegistersToLocalFile() ' save numbers to file
Label3.Text = "Uploading"
System.Windows.Forms.Application.DoEvents()
Call FTPUpload(TextBox3.Text) ' send back up to ftp site named as my name
Label3.Text = "Resting"
Call SaveFTPFilename() ' so reads in when restart
End Sub
Sub DisplayPicaxeRegisters()
Dim i As Integer
Dim registernumber As String
RichTextBox1.Multiline = True ' so can display more than one line in the text box
RichTextBox1.Clear() ' clear the text box
For i = 0 To 13
registernumber = Trim(Str(i)) ' trim off leading spaces
If i < 10 Then
registernumber = "0" + registernumber ' add 0 to numbers under 10
End If
RichTextBox1.AppendText(registernumber + " = " + Str(PicaxeRegisters(i)) + Chr(13))
Next ' chr(13) is carriage return so new line
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
' check out of range first
i = Val(TextBox1.Text)
If i < 0 Or i > 13 Then
TextBox1.Text = 0
End If
i = Val(TextBox2.Text)
If i < 0 Or i > 255 Then
TextBox2.Text = 0
End If
PicaxeRegisters(Val(TextBox1.Text)) = Val(TextBox2.Text) ' change the value
Call DisplayPicaxeRegisters() ' and refresh the display
ModifyFlag = True ' and next ftp link skip downloading
End Sub
Sub SaveRegistersToLocalFile() ' save register array in a local text file
Dim i As Integer
FileOpen(1, TextBox3.Text, OpenMode.Output) ' open the text file named in the text box
For i = 0 To 13
PrintLine(1, Str(PicaxeRegisters(i))) ' save 14 values
Next
FileClose(1) ' close the file
End Sub
Sub ReadRemoteFileToRegisters() ' read local text file into the register array
Dim i As Integer
Dim LineOfText As String
Try
FileOpen(1, TextBox3.Text, OpenMode.Input) ' read the remote file name
For i = 0 To 13
LineOfText = LineInput(1) ' read in the 14 lines
PicaxeRegisters(i) = Val(LineOfText) ' convert text to values
Next
FileClose(1)
Catch ex As Exception
FileClose(1) ' file doesn't exist so do nothing
End Try
End Sub
Sub ReadFTPFilename() ' so the name of the remote ftp file is the same next time this program is run
Dim LineOfText As String
Try
FileOpen(1, "FTPFilename.txt", OpenMode.Input) ' open the file
LineOfText = LineInput(1)
TextBox3.Text = LineOfText ' read the name
FileClose(1)
Catch ex As Exception
FileClose(1)
End Try
End Sub
Sub SaveFTPFilename()
FileOpen(1, "FTPFilename.txt", OpenMode.Output) ' save the remote ftp file name
PrintLine(1, TextBox3.Text)
FileClose(1)
End Sub
End Class
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|



















































