Results 1 to 9 of 9

Thread: File Transfer between PDA & Pc

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    File Transfer between PDA & Pc

    Hello All,

    I'm trying to build a very simple program that starts communication between Pocket Pc and Pc using Visual Basic . NET and its emulator.
    Would you pls show me the easiest way to strat such a thing? if there is any sample code or tutorial I would apprciate it also.

    Thanks in advance

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: File Transfer between PDA & Pc

    Well, this might not be even close to what you are asking about, but here's a class I wrote to handle transfering XML strings from a PDA program. The same class can't be used on the PC side, but the functionality of the PC side item isn't much different.

    VB Code:
    1. 'This class opens a file with the name passed into the constructor. It then
    2. 'either writes or reads one item to it for each write/read item call.
    3. 'This Class assumes the file will be located in the AppPath() folder.
    4. Public Class FileDataStorePDA
    5.     Private fileName As String
    6.     Private bisValid As Boolean
    7.     Private readValid As Boolean
    8.     Private sWriter As System.IO.StreamWriter
    9.     Private sReader As System.IO.StreamReader
    10.     Private clearFlag As Boolean
    11.  
    12. #Region "Constructors and Destructors"
    13.     Public Sub New(ByVal st1 As String)
    14.         st1 = AppPath() & "\" & st1
    15.  
    16.         Try
    17.             sWriter = System.IO.File.AppendText(st1)
    18.             bisValid = True
    19.             fileName = st1
    20.             clearFlag = False
    21.         Catch ex As Exception
    22.             bisValid = False
    23.             clearFlag = True
    24.         End Try
    25.  
    26.     End Sub
    27.  
    28.     Public Sub Dispose()
    29.         Try
    30.             sReader.Close()
    31.         Catch ex As Exception
    32.             'Nothing needs to be done here.
    33.         End Try
    34.  
    35.         Try
    36.             sWriter.Close()
    37.         Catch ex As Exception
    38.             'Nothing needs to be done here, either.
    39.         End Try
    40.     End Sub
    41. #End Region
    42.  
    43. #Region "Public"
    44.     Public Function WriteItem(ByVal st1 As String) As Boolean
    45.         If bisValid Then
    46.             If clearFlag Then
    47.                 clearFlag = False
    48.                 sWriter.Close()
    49.                 System.IO.File.Delete(fileName)
    50.                 sWriter = System.IO.File.AppendText(fileName)
    51.             End If
    52.             sWriter.WriteLine(st1)
    53.             WriteItem = True
    54.         Else
    55.             If readValid Then
    56.                 Try
    57.                     sReader.Close()
    58.                     If clearFlag Then
    59.                         clearFlag = False
    60.                         System.IO.File.Delete(fileName)
    61.                     End If
    62.                     sWriter = System.IO.File.AppendText(fileName)
    63.                     sWriter.WriteLine(st1)
    64.                     WriteItem = True
    65.                 Catch ex As Exception
    66.                     'Don't know if I really want this to do anything.
    67.                     WriteItem = False
    68.                 Finally
    69.                     readValid = False
    70.                     bisValid = True
    71.                 End Try
    72.             Else
    73.                 WriteItem = False
    74.             End If
    75.         End If
    76.     End Function
    77.  
    78.     Public Function ClearFile()
    79.         clearFlag = True
    80.     End Function
    81.  
    82.     Public Function ReadItem() As String
    83.         If readValid Then
    84.             Try
    85.                 ReadItem = sReader.ReadLine
    86.                 If ReadItem Is Nothing Then
    87.                     ReadItem = ""
    88.                 End If
    89.             Catch ex As Exception
    90.                 MsgBox("A problem occurred with reading the file:" & vbNewLine & ex.Message, MsgBoxStyle.Information, "Problem")
    91.                 ReadItem = ""
    92.             End Try
    93.         Else
    94.             If bisValid Then
    95.                 Try
    96.                     sWriter.Close()
    97.                     sReader = New System.IO.StreamReader(fileName, System.Text.Encoding.ASCII)
    98.                     ReadItem = sReader.ReadLine
    99.                     readValid = True
    100.                 Catch ex As Exception
    101.                     MsgBox("Can't read item:" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Problem Reading")
    102.                     ReadItem = ""
    103.                     readValid = False
    104.                 Finally
    105.                     'Invalidate the reading, anyways.
    106.                     readValid = True
    107.                     bisValid = False
    108.                 End Try
    109.             End If
    110.         End If
    111.     End Function
    112.  
    113.     Public Function CanRead() As Boolean
    114.         CanRead = readValid
    115.     End Function
    116.  
    117.     Public Function CanWrite() As Boolean
    118.         CanWrite = bisValid
    119.     End Function
    120.  
    121. #End Region
    122.  
    123. #Region "Private"
    124.     'The function that gets the path of the application.
    125.     Private Function AppPath()
    126.         AppPath = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetCallingAssembly.GetName.CodeBase.ToString)
    127.     End Function
    128. #End Region
    129.  
    130. End Class
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: File Transfer between PDA & Pc

    Thanks alot Shaggy, I want to ask if I can use this class in smartdevice project ( emulator ) of VB .NET. Pardon my question because I'm very newbie using such platform

    One more thing. I found chatting application written using VB .NET it consists of three parts: client, server, inbetween, so I wonder if I can use the PC as server. Pocket Pc as client, and then modify the project so I can transfer files btwn them ?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: File Transfer between PDA & Pc

    As for the emulator....I don't know. I would expect that it would work, but the emulator is an odd item. If you can avoid using it, do so, but that requires actually having a PDA to test things on.

    As for the client/server. I believe you HAVE to use the PC as the server because PDA's don't allow listening on a port. That means that only the PC can be set to listen for input.

    I suspect that very little needs to be changed on the code I posted to transfer text files (maybe nothing), or for chat applications. However, all incoming text is interpreted into a string, since I was just moving XML.

    If you liked that, here's the PC part of it. The idea is that the connection class runs in it's own thread. This is mostly untested, and has a bunch of stuff that is specific to the database I was using, but it would be a start.

    VB Code:
    1. Public Class PDAviaTCP
    2.     Private curListen As System.Net.Sockets.TcpListener
    3.     Private quitFlag As Boolean
    4.     Private myTimer As System.Windows.Forms.Timer
    5.     Private lSock As System.Net.Sockets.TcpClient
    6.     Private netStream As System.Net.Sockets.NetworkStream
    7.     Private linOpt As System.Net.Sockets.LingerOption
    8.     Private bTimedOut As Boolean
    9.  
    10. #Region "Constructors and Destructors"
    11.  
    12.     Public Sub New()
    13.         curListen = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 2000)
    14.         quitFlag = True
    15.         'Create the timer
    16.         myTimer = New System.Windows.Forms.Timer
    17.         'Need to tell it where to send time tick messages.
    18.         AddHandler myTimer.Tick, AddressOf TimerEventProcessor
    19.  
    20.         'This is set so that we can know the status if nothing is received.
    21.         bTimedOut = False
    22.  
    23.         ' Sets the timer interval to 5 seconds.
    24.         myTimer.Interval = 5000
    25.         myTimer.Enabled = False
    26.         linOpt = New System.Net.Sockets.LingerOption(True, 5)
    27.     End Sub
    28.  
    29.     Public Sub dispose()
    30.         myTimer.Dispose()
    31.         curListen.Stop()
    32.         If Not lSock Is Nothing Then
    33.             lSock.Close()
    34.         End If
    35.     End Sub
    36. #End Region
    37.  
    38. #Region "Public Functions"
    39.     'StartListening puts the TCPListener class into listening mode. This must be called
    40.     'for a connection to be received.
    41.     Public Sub StartListening()
    42.         Try
    43.             curListen.Start()
    44.         Catch ex As Exception
    45.             'Regardless of the exception, this may be ok.
    46.             MsgBox("Exception was thrown during start." & vbNewLine & vbNewLine & ex.Message, MsgBoxStyle.Information, "Possible Issue")
    47.         End Try
    48.     End Sub
    49.  
    50.     'This function simply checks for a connection, but does not do anything except return
    51.     'true if a connection is pending.
    52.     Public Function CheckForConnection() As Boolean
    53.         Try
    54.             CheckForConnection = curListen.Pending
    55.         Catch
    56.             CheckForConnection = False
    57.         End Try
    58.     End Function
    59.  
    60.     'This function simply accepts the connection and stops the listener.
    61.     Public Function GetConnection() As Boolean
    62.         Try
    63.             If curListen.Pending Then
    64.                 lSock = curListen.AcceptTcpClient
    65.                 lSock.LingerState = linOpt
    66.                 netStream = lSock.GetStream
    67.                 curListen.Stop()
    68.                 GetConnection = True
    69.             Else
    70.                 GetConnection = False
    71.             End If
    72.         Catch ex As Exception
    73.             GetConnection = False
    74.         End Try
    75.     End Function
    76.  
    77.     'Stops listening without bothering with whether or not a connection is available.
    78.     Public Sub StopListening()
    79.         curListen.Stop()
    80.     End Sub
    81.  
    82.     'Use this to send data to the socket. No check is made to see that
    83.     'the socket is available. The return is false if any exceptions are raised.
    84.     Public Function SendData(ByVal st1 As String) As Boolean
    85.         Dim sendBuff(2048) As Byte
    86.  
    87.         'Try
    88.         sendBuff = System.Text.Encoding.Default.GetBytes(st1.ToCharArray())
    89.         netStream.Write(sendBuff, 0, sendBuff.GetLength(0))
    90.         SendData = True
    91.         'Catch ex As Exception
    92.         SendData = False
    93.         'End Try
    94.  
    95.     End Function
    96.  
    97.     'Use this to get data from the socket. This will time out in 5s.
    98.     Public Function RetrieveData() As String
    99.         Dim recBuff(2048) As Byte
    100.         Dim recCount As Integer
    101.         Dim st1 As String
    102.         Dim hasRead As Boolean
    103.  
    104.         hasRead = False
    105.         'Get ready to read.
    106.         quitFlag = True
    107.         myTimer.Enabled = True
    108.         Try
    109.             Do While quitFlag
    110.                 If netStream.DataAvailable Then
    111.                     recCount = netStream.Read(recBuff, 0, recBuff.GetLength(0))
    112.                 End If
    113.                 If recCount > 0 Then
    114.                     hasRead = True
    115.                     myTimer.Enabled = False
    116.                     'Turn it into a string.
    117.                     st1 &= System.Text.Encoding.ASCII.GetString(recBuff, 0, recCount)
    118.                     st1 = st1
    119.                     'Clear out the buffer.
    120.                     recBuff.Clear(recBuff, 0, recBuff.GetLength(0))
    121.                     recCount = 0
    122.                 Else
    123.                     'If hasRead is set, then something has been received, but the last loop
    124.                     'returned nothing. Therefore, we are done reading.
    125.                     If hasRead Then
    126.                         'This is simply cleared to stop the loop.
    127.                         quitFlag = False
    128.                     End If
    129.                     'DoEvents, but only if nothing is coming in!
    130.                     Application.DoEvents()
    131.                 End If
    132.             Loop
    133.         Catch ex As Exception
    134.             'Nothing can be done here, but that's ok.
    135.             MsgBox(ex.Message)
    136.         End Try
    137.  
    138.         'Get out.
    139.         Me.bTimedOut = False
    140.         RetrieveData = st1
    141.     End Function
    142.  
    143.     'If this is true, then the time event fired. Basically, this means that if
    144.     'RetrieveData() returned an empty string, it was because of timing out.
    145.     Public Function TimedOut() As Boolean
    146.         TimedOut = Me.TimedOut
    147.     End Function
    148.  
    149. #End Region
    150.  
    151. #Region "Private Functions"
    152.  
    153.     'The timer is used to stop the endless loop in RetrieveData(), and for no other
    154.     'purpose. If the event fires, the bTimedOut variable is set.
    155.     Private Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs)
    156.         myTimer.Enabled = False
    157.         quitFlag = False
    158.         Me.bTimedOut = True
    159.     End Sub
    160.  
    161.  
    162. #End Region
    163. End Class
    164.  
    165.  
    166.  
    167. Public Class ThreadedConnection
    168.     Private theThread As System.Threading.Thread
    169.     Private mDBHolder As dbHolder
    170.     Private mPDAvia As PDAviaTCP
    171.     Private Shared mWriteDone As System.Threading.ManualResetEvent
    172.  
    173.     Public Sub New()
    174.         theThread = New System.Threading.Thread(AddressOf DoConnection)
    175.  
    176.         'This may be somewhat sketchy, but it should work just fine.
    177.         mDBHolder = dbC.Copy
    178.         theThread.IsBackground = True
    179.         mPDAvia = New PDAviaTCP
    180.         mWriteDone = New System.Threading.ManualResetEvent(False)
    181.  
    182.     End Sub
    183.  
    184.     Public Sub Dispose()
    185.         StopConnection()
    186.         mWriteDone.Close()
    187.         mPDAvia.dispose()
    188.         mDBHolder.Dispose()
    189.     End Sub
    190.  
    191.     'This checks the database in the connection thread, and if it is valid, the thread
    192.     'is started.
    193.     Public Function StartConnection() As Boolean
    194.  
    195.         If mDBHolder.IsValid Then
    196.             theThread.Start()
    197.         End If
    198.         StartConnection = mDBHolder.IsValid
    199.     End Function
    200.  
    201.     Public Sub StopConnection()
    202.         'The event will be signaled (go ahead) throughout, with the exception of writing
    203.         'to the db. For that alone it will be unsignaled (wait). Once the write is complete
    204.         'the event will be signaled.
    205.         mWriteDone.WaitOne()
    206.  
    207.         'At which point, it would be a good idea to stop the comport from listening.
    208.         mPDAvia.StopListening()
    209.         'And end the thread.
    210.         theThread.Abort()
    211.     End Sub
    212.  
    213.  
    214.     Private Sub DoConnection()
    215.         Dim st1 As String
    216.         Dim st2 As String
    217.         Dim v As Integer
    218.         Dim outData As String
    219.  
    220.         'First make the string data for outputting.
    221.         outData = mDBHolder.CreateData()
    222.         mPDAvia.StartListening()
    223.  
    224.         mWriteDone.Set()
    225.         Do
    226.             Do
    227.                 Application.DoEvents()
    228.             Loop While Not mPDAvia.CheckForConnection
    229.  
    230.             'If you get here, then a connection is ready.
    231.             If mPDAvia.GetConnection() Then
    232.                 'Send the type info across.
    233.                 mPDAvia.SendData("1")
    234.                 st1 = mPDAvia.RetrieveData
    235.  
    236.                 'Next, send out the output data.
    237.                 mPDAvia.SendData(outData)
    238.  
    239.                 'Now, st1 holds either 1 (no data) or 4 (new data coming) or 5 (all data coming)
    240.                 v = Val(st1)
    241.  
    242.                 If v > 1 Then
    243.                     'Now the data coming back should be received.
    244.                     st2 = mPDAvia.RetrieveData
    245.                     mWriteDone.Reset()
    246.                     mDBHolder.UpdateTablesWithXML(st2)
    247.                     mWriteDone.Set()
    248.                 End If
    249.             End If
    250.  
    251.             'Now it is necessary to start listening again.
    252.             mPDAvia.StartListening()
    253.         Loop While True
    254.     End Sub
    255.  
    256. End Class
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: File Transfer between PDA & Pc

    Thanks alot Shaggy. I'm trying nw to run your code and see how to modify it to let it transfer file instead of XML strings. and I'll come back when a question come up

    Thanks

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: File Transfer between PDA & Pc

    Hello Shaggy,

    The pc side code "PDAviaTCP" gave me an error with "dbHolder". I thought it is a predefine class but is isn't, Where I can found it?

    the other thing is what is the interface looks like either for Pc or PDA sides ?

    Thanks

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: File Transfer between PDA & Pc

    You want to get rid of that dbHolder, and anything related to it. That's a class I wrote to manage the database connection for the program. It has no bearing on any other program.

    As for a UI, there really isn't one related to those classes.

    In general, any PDA program I write uses a single form (or more if it gets REALLY big). The form has many panels on it that are sized to the size of the PDA screen. All panels are initially set to be off screen, with the top set around 300, and the left set at 0. The panels take the place of forms, since moving a panel is very fast, while loading a form is very slow (on a PDA).

    The basic outline is that there is a state variable called theState (or something like that), and a sub called ShowPanel() that just has a switch on theState. The switch sets the top value for the right panel to 0, so that it becomes visible.

    This isn't my idea, by the way, I got it from a book on DB programming for the .NET CF by Rob Tiffany. The idea works great.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    5

    Re: File Transfer between PDA & Pc

    Thanks again Shaggy,

    May I know the title of the book?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: File Transfer between PDA & Pc

    You're in luck, I happen to have the title with me (though not the book):

    SQL Server CE Database Development with the .NET Compact Framework by Rob Tiffany. Published by aPress.

    While the topic is indeed databases, he's got some strong opinions on PDA program design, and shares them in the first few chapters. Nothing earth-shaking, but I certainly like that panel-swapping alternative to form loading.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width