Page 3 of 4 FirstFirst 1 2 3 4 LastLast
Results 21 to 30 of 38

Thread: VB.net serial and ftp code

  1. #21
    Senior Member
    Join Date
    Jan 1970
    Location
    Croydon, UK.
    Posts
    126

    Default

    If you want to send characters that are outside the ASCII range (0..127) you will need to set the SerialPort's Encoding property accordingly (it defaults to ASCII). Windows-1252 is fairly appropriate (New Encoding(1252) or New Encoding("Windows-1252") will create an instance that can be used).
    Code:
    serialPort.Encoding = New Encoding(1252)
    (Rather that P/Invoking Sleep, you can also use the .NET equivalent System.Threading.Sleep()).

  2. #22
    Moderator
    Join Date
    Jan 1970
    Location
    Adelaide, Australia
    Posts
    2,586

    Default

    "New Encoding" sounds like a bug fix to me! It is great there are several solutions to this - I ended up using the method of directly writing a byte array because it then removed the step of cutting up a string into pieces. http://www.instructables.com/id/Cont...-with-your-PC/ shows the steps. The 127 problem also seems to apply to sending strings as well as receiving them but sending and receiving a byte array works out simpler. I still haven't quite worked out how 'offset' works but arrays can easily be copied to add headers and/or checksums at the end and leave the offset in the final array at zero.

    Thanks for the tip re Sleep. Just a quick dumb question re sleep in vb.net (using either method) - do background tasks keep running?
    Last edited by Dr_Acula; 22-10-2007 at 23:55.

  3. #23
    Member
    Join Date
    Jan 1970
    Location
    Washington (state), USA
    Posts
    79

    Default PicaxeWebSender posted

    Next up in my series of posting is the WebSender object. This object queues messages sent from the Picaxe and then sends them to the FTP message repository at a defined, configurable interval.

    The message queue is kept in an in-memory queue, so it does not need to create files on your local hard drive before sending the file to the message repository. I'm planning an enhancement that will enable you to send files to a local disk-based message repository (primarily for testing) and one that will enable using an HTTP server to manage the repository.

    Both of these enhancements should be straight-forward, I'm taking advantage of the System.Net.WebClient object. This object is everything that I wish the WebRequest object could have been. After creating a WebClient object, you can upload data to an FTP site with this line of code:
    Code:
       WebClient client = new WebClient()
       client.UploadData("ftp://ftp.host.com/Test.txt", data<string,file,byte array>);
    To send the same data to a Web site, you write:
    Code:
    client.UploadData("http://htp.host.com/handler.aspx?Test.txt", data<string,file,byte array>);
    Want to save it to the file system? Here's the line:
    Code:
       client.UploadData("file://c:\\temp\\Test.txt", data<string,file,byte array>);
    As usual, I've uploaded the solution containing the C# and VB versions of the code to ftp.0catch.com, but as usual the zip file was removed by the Web site. See later postings for a pointer to the actual location. The test driver program has been updated to include a new button, click the "Send to FTP site" button and every message received will be forwarded on to the FTP site at ftp.0catch.com.

    The VB code for the WebSender is attached as a text file, as is an updated version of the Message object. I needed to add another property to the Message object, it now has the FileAddressString property that returns the destination and source address bytes as 3-digit integers separated by "x" characters ("000x000x000x000x000x000x000x000", the same as Dr. Acula's original program.

    As usual, share and enjoy, and let me know what you think.
    Attached Files Attached Files
    Last edited by puddlehaven; 23-11-2007 at 17:12. Reason: Removed link to missing zip file.

  4. #24
    Member
    Join Date
    Jan 1970
    Location
    Washington (state), USA
    Posts
    79

    Default

    Quote Originally Posted by Dr_Acula View Post
    Thanks for the tip re Sleep. Just a quick dumb question re sleep in vb.net (using either method) - do background tasks keep running?
    Depends. If it's truly a background task running on another thread, then it keeps running. In your case, using the SerialPort object, you can put your UI thread to sleep and the background serial port listener code will keep on running.

    Actually, the System.Threading.Thread.Sleep is just the .NET Framework wrapper to the same System32 sleep call you were using, just all wrapped up in pretty code by the folks at Microsoft.

    "New Encoding" isn't a hack. By default the SerialPort encodes everything coming past it using an Encoding object called "ASCII". port.Encoding = New Encoding() just means "create a new Encoding object and use it instead of the ASCII Encoding object."

    It's just how you change the data encoding coming over the wire. I usually use UTF-8 nowadays, but that's just me.

    Chuck

  5. #25
    Member
    Join Date
    Jan 1970
    Location
    Washington (state), USA
    Posts
    79

    Default PicaxeWebReceiver posted

    I've finished the final piece of an end-to-end Picaxe to Web to Picaxe transfer system. The WebReceiver object retrieves messages from a Web repository and returns them to the PC where they can be passed on to the connected Picaxe network.

    Like the Listener object, you can either handle the MessageReceived event or you can poll the DataAvailable property and read messages out of the WebReceiver's queue. Use of the queue is configurable by the UseMessageQueue property, this property should only be set true if your application will read messages out of the queue, typically if you are handling the MessageReceived event you are not reading messages out of the queue, and the UseMessageQueue property should be set to false.

    The Web receiver polls the Web message repository at a configurable interval for new messages. You should set the interval to meet the expected traffic needs of your application.

    The only "tricky" property on the Web receiver is the MessagePrefix property. You set this to the leading characters of the destination addresses that you want to download messages for. For example, if messages with the destination addresses "112x104x110x000" to "112x104x110x255" are destined for my network I would set the MessagePrefix property to "112x104x110x". That way I only download messages intended for my network, saving processing and bandwidth.

    Here's the processing flow for the PicaxeWebReceiver object:

    • The WebReceiver object uses FTP to get a directory listing of all waiting messages that have the configured prefix.
    • For each message in the Web repository, the WebReceiver
      • Fetches the message from the repository via FTP.
      • Raises the MessageReceived event.
      • If using the queue to store messages, places the messages in the receive queue.
      • Removes the message from the Web repository.
      • Pauses for a configurable interval before getting the next message.
    • Waits until it's delay interval is up before connecting to the FTP site and starting again.


    Once again I've made changes to the Message object to make creating message objects from the data downloaded from the Web easier. And as usual I've attached the VB versions of the WebReceiver and the Message objects to this e-mail.

    Unlike usual I have not uploaded the Visual Studio solution to ftp.0catch.com. For some reason, probably due to a use restriction on the free Web site Dr. Acula is using for the Web repository, the zip file is being removed from the server. I've uploaded the file to my personal Web site, you can retrieve the file from this link:


    And in another exciting development, I've figured out how to transfer documentation comments from the C# source files to the VB source files, so now all the VB source files are fully documented.

    I've made some changes to the Visual Studio solution. I've renamed some objects, and removed the test application and replaced it with a Windows application that will act as a bridge between the Picaxe network and the Web repository. I've also started work on a Windows service that will run in the background on your PC to handle the bridge function, although that application is not ready yet. Please see the ReadMe.txt file in the Zip file for additional details.

    As usual, share and enjoy, and let me know if you have any question, comments, kudos or brickbats.

    Chuck
    Attached Files Attached Files
    Last edited by puddlehaven; 17-10-2011 at 20:36. Reason: Updated link to source.

  6. #26
    Moderator
    Join Date
    Jan 1970
    Location
    Adelaide, Australia
    Posts
    2,586

    Default

    This is really moving forward at a great pace!
    Bit of a "oh darn" that OCatch are removing the zip files - but www.puddlehaven is better!
    I got a couple of picaxes chatting to each other via the web working last night - a picaxe in my shed was able to turn a led on and off on a picaxe in my office inside. Could have been anywhere in the world. Also can link quite a number of picaxes (within the capacity of the ftp server) Am going to write it up in the next day or too - it needs commenting and documenting. The system I have is very simple but I think it can grow into the one Puddlehaven is building so we can grow this step by step.
    I'm going to take back some of my negative comments about vb.net after I discovered that ftp has a timeout function. This turns out to be imperative as about 1 in 20 ftp's to the server were hanging the program. Now it times out after 10 seconds.
    A question I might put on the main forum once we get all this working is to ask Stelios if he can link his little web server into this system.

    Addit: picaxe worldwide microcontroller link is now published at http://www.instructables.com/id/Worl...-for-under-20/
    Last edited by Dr_Acula; 25-10-2007 at 13:52.

  7. #27

    Default

    Dr Acula & Puddlehaven I'm sure I'm not the only one following this post with great interest. Dr Acula I hope your still "going to write it up in the next day or too".
    Thanks guys.....
    Perth
    Western Australia

  8. #28
    Moderator
    Join Date
    Jan 1970
    Location
    Adelaide, Australia
    Posts
    2,586

    Default

    D'oh. I slipped in the writeup as an edit to an old post rather than a new comment. The complete project is at http://www.instructables.com/id/Worl...-for-under-20/

  9. #29
    Member
    Join Date
    Jan 1970
    Location
    Washington (state), USA
    Posts
    79

    Default New library posted

    I've posted a new version of the network connector library to my Web site. This new version adds support for sending messages via UDP packets to support the Internet gateway mentioned in this thread:


    I made two changes to the library for this version:

    1. I refactored the WebSender object into two objects, Sender and WebSender. The new Sender object implements all the methods and properties that are shared between the Web sender and UDP sender, the WebSender object only implements the methods and properties required to send data via FTP or HTTP.
    2. I created the new UDPSender object. Because of the refactoring, I only had to write one new method to implement UDP sending, and most of the code was in looping through the message queue.

    Here's the two lines of code that it takes to send a UDP message using C# or VB.NET and the .NET Framework (I'm not including the lines that set up the message array, or that set the host and port addresses. Those are three more lines...):

    Code:
    DIM client as new System.Net.Sockets.UdpClient
    client.Send(messageBytes, messageBytesLength, Host, Port)
    You can find the new version (and the old version) on my Web site at:


    As always, share and enjoy.

    Chuck
    Last edited by puddlehaven; 17-10-2011 at 20:39. Reason: Updated the library link.

  10. #30
    Senior Member
    Join Date
    Jan 1970
    Location
    Nova Scotia, Canada
    Posts
    1,902

    Default

    Dr_A above: "Getting two picaxes talking to each other anywhere in the world would be quite an achievement and I think we are almost there. I have a protoboard with a servo driving a switch to turn on a lamp. It is a demo system to show a picaxe actually doing something mechanical. It would be great to see it controlled by someone else."

    Any chance of making it so that a UDP message could accomplish this?

    What other items did you have in mind as examples of things which could be turned on by others? How would the sender know that the request has been fulfilled?

Bookmarks

Posting Permissions

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