Grabbing individual files via FTP in VB 2005 / VB
July 9, 2008 at 10:21 pm | In Non Insteon Programming | Leave a CommentTags: caller id, ftp client, ftp in vb, FtpWebRequest, Insteon, system.net.sockets ftp example, System.Net.WebRequestMethods.Ftp.DownloadFile, X10
For my voice mail / Caller-ID function I placed the name / number of the person in a database but also I have the name of my contact (joe.png, luis,jpg etc), After Ive retrieved the information, I FTP back to the server to grab the image file
Here is how its done.
For the example;
FTP server is at 192.168.0.101
username is :webuser
password is :password
File to get: <variable called ImageName> in a folder called Images
Location to save: c:\images\<ImageName>
The default folder on my server have a folder called images/
On the server im running FileZilla FTP Server (Free)
'------------------------------------------------------------------------------
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Function GetFTP(ByVal ImageName As String)
Dim localFile As String = "c:\images\" + ImageName
Dim remoteFile As String = "/images/" + ImageName
Dim host As String = "ftp://192.168.0.101"
Dim username As String = "webuser"
Dim password As String = "password"
'1. Create a request: must be in ftp://hostname format,
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = _
CType(FtpWebRequest.Create(URI), FtpWebRequest)
'2. Set credentials
ftp.Credentials = New _
System.Net.NetworkCredential(username, password)
'3. Settings and action
ftp.KeepAlive = False
'we want a binary transfer, not textual data
ftp.UseBinary = True
'Define the action required (in this case, download a file)
ftp.UsePassive = False
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
'Loop to read & write to file
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
'-----Setup Response stream to grab the FileStream aka Data and send it to a file using the FileStream object.
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
'-----Once Data is detected in the ResponseStream Loop until empty,
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read) '----- Write the 'read' variable into the file
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
Exit function
PRESTO!
Software: Simple VB Caller ID program and Flash lights
June 22, 2008 at 9:39 pm | In Non Insteon Programming | 2 CommentsTags: caller id, get caller info, Insteon, modem, us robotics, vb 2005, vb code, X10

One thing I was always interested was being able to notify me when a certain person calls. And in this case flash a inteson light or two.
My code is in VB 2005, for a VB6 Sample you can go here and basically requires only some little coding. In the case of VB6 I think you only need to add the Serial object and adjust the port via the objects property settings.
What I used,
- Analog phone with Caller ID (Vonage actually)
- Internal / External Modem – Mine is a 56k External modem and have caller Id capability.
- Hook them up, from the wall to the modem, to the modem to the handset.
The best way to see if it does is to open Hyper terminal and type the following command AT #CID=1 if it respond’s ok your set. This of course if for the modem i’m using, depending on your device it may be different. This page includes how you can check for Caller ID capability on your modem
Here is a example of what the modem will pickup and we will parse.
RING
DATE = 0621
TIME = 1113
NMBR = 1407xxxxxxxxx
NAME = Carlos Cobol
If you modem is external like mine you need to make sure AutoAwnser is disabled this is done by the DIP switches, For the US robotics its Dip switch #5 as down. In the case its internal you will need to add the command to disable auto answer. This is done with the command ATS0=0.

My Dip settings are as follows,
1-DOWN
2-UP
3-DOWN
4-UP
5-DOWN
6-UP
7-UP
8-DOWN
Since the code below allows to send some strings up startup, just add the in there if you need to.
Note: I do make reference to textbox called txtDataReceived in my form which will include the output.
First we make our imports include the serial devices (My program writes to a SQL database so some lines may not be needed)
Imports System.IO
Imports System.IO.Ports
Imports System.Net
Imports System.Data
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Threading
In our Class definitions we include the reference to the serial port and we’ll call it ‘Modem’
Public Class Form1
Dim WithEvents Modem As New IO.Ports.SerialPort
In our Load event we will attach to the serial port and give it the basic settings, Here is where you would also add any initialization commands
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Modem.PortName = "COM1"
Modem.BaudRate = 9600
Modem.DataBits = 8
Modem.Parity = Parity.None
Modem.StopBits = StopBits.One
If Modem.IsOpen = False Then Modem.Open()
Modem.Write("AT #CID=1" & vbCrLf) '-------- YOU CAN ADD ADDITIONAL LINES SUCH AS #ATS0=0 TO DISABLE AUTO ANSWER IN INTERNAL MODEMS
End Sub
Next using the “Data Received” event we want to capture the Data which was received, Since there is a thread already running it wont allow us to update the textbox so simple we need to call use using the definition below,
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Modem.DataReceived
Try
txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
Catch EX As Exception
Debug.Print("ERROR")
End Try
End Sub
Second function to update the textbox and check for the number, and check for one of the text information above, Ive stripped my code for simplicity but you can search for specific strings here, match it with fields in a database, even FTP the data somewhere else, but that i’ll show you on another post.
Here we will check for the number ‘123456789′
Public Sub updateTextBox() Dim temp As String With txtDataReceived .Font = New Font("Garamond", 12.0!, FontStyle.Bold) .SelectionColor = Color.Red temp = Modem.ReadExisting.ToString Buffer = Buffer + temp .AppendText(temp) .ScrollToCaret() End With If Buffer.Contains("NMBR") = True Then Dim FILENAME As String = "c:\cALLINFO.TXT" ' - Set a filename File.WriteAllText(FILENAME, Buffer) '---- Write all the buffer for testing if we want to see it If Buffer.contains("123456789") then '----- check for specific # 'DO INSTEON COMMANDS HERE Buffer="" End If End if End sub
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.