Shop OBEX P1 Docs P2 Docs Learn Events
Serial to Ethernet and Back — Parallax Forums

Serial to Ethernet and Back

Mike GMike G Posts: 2,702
edited 2014-06-21 13:20 in Accessories
Work has been crazy but I finally had time to complete a Serial to Ethernet demo. The attached spin code uses the Parallax Serial Terminal to display incoming data and send outgoing data. UDPConsole.zip is a .NET console project. If you don't have Visual Studio installed, unzip the project and copy the executable and config files somewhere on you box. The exe and config files are located in UDPConsole\bin\Release\ and UpdServerConsole\bin\Release\. Then just double click each exe. If you have Visual Studio, I'm assuming you know how to open and run a project. BTW, the console application is a Windows .NET 4.0 app.

Files
  • UDPClientConsole.exe
  • UDPClientConsole.exe.config
  • UpdServerConsole.exe
  • UpdServerConsole.exe.config

Since all networks are not configured the same, you might have to update the configuration. This is how...

1) Spin Configuration
Setup the items in bold according you your network. Run ipconfig from a command prompt or open your router settings to find your network parameters.
  • 192, 168, 1, 1 = Your gateway
  • 255, 255, 255, 0 = Your Subnet mask
  • 192, 168, 1, 130 = Spinneret IP address
  • $00, $08, $DC, $16, $F1, $32 = Spinneret MAC address (Sticker on the bottom of the board)
PUB Main | bytesToRead, bytesSent, receiving, ptr

  wiz.HardReset(WIZ#WIZ_RESET) 
  pause(500)
  pst.Start(115_200)
  pause(500)

  'Set network parameters
  wiz.Start(WIZ#SPI_CS, WIZ#SPI_SCK, WIZ#SPI_MOSI, WIZ#SPI_MISO) 
  wiz.SetCommonnMode(0)
[B]  wiz.SetGateway(192, 168, 1, 1)
  wiz.SetSubnetMask(255, 255, 255, 0)
  wiz.SetIp(192, 168, 1, 130)
  wiz.SetMac($00, $08, $DC, $16, $F1, $32)[/B]

2) Spin Socket Setup
  • 10000 is the Spinneret port
  • 192, 168, 1, 106 is the PC running the .NET code. The IP must be a valid IP address on your network or the demo will not work!
  • 8000 is the port on the PC listening for UDP data
  pst.str(string("Initialize Sock", CR))
  'Initialize Socket 0 port 8080
  Sock.Init(SERIAL_SOCK, UDP, [B]10000[/B])
  Sock.RemoteIp([B]192, 168, 1, 106[/B])
  Sock.RemotePort([B]8000[/B])

3) Application Setup
There are two console applications UDPClientConsole and UDPServerConsole. The client sends messages to the Spinneret. The server receives messages from the Spinneret. If you changed the Spin code above then you must update the console applications.

3a) UDPClientConsole
Open the configuration file. That's UDPClientConsole.exe.config if you do not have Visual Studio, Otherwise it's app.config in the project. You can open the file in a text editor like Notepad. The IP address must match the IP assigned to the Spinneret in #1 bullet 3. 10000 must match the port setting in #2 bullet 1.
<applicationSettings>
        <UDPConsole.Properties.Settings>
            <setting name="remoteIp" serializeAs="String">
                <value>[B]192.168.1.130[/B]</value>
            </setting>
            <setting name="remotePort" serializeAs="String">
                <value>[B]10000[/B]</value>
            </setting>
        </UDPConsole.Properties.Settings>
    </applicationSettings>

3b) UpdServerConsole
Open the configuration file. That's UDPServerConsole.exe.config if you do not have Visual Studio. Otherwise it's app.config in the project. 8000 must match #2 bullet 3.
<applicationSettings>
    <UpdServerConsole.Properties.Settings>
      <setting name="listenerPort" serializeAs="String">
        <value>[B]8000[/B]</value>
      </setting>
    </UpdServerConsole.Properties.Settings>
  </applicationSettings>

That should cover the setup. Next, load the spin code. Open the Parallax Serial Terminal. Launch UpdClientConsole.exe and udpServerConsole.exe.

You'll see a Send: prompt in UpdClientConsole. Type Hello World and press enter. You should see the activity on the Parallax serial terminal.
Send: Hello World

Type Hello Word in the Parallax Serial terminal (very top) and press enter. You should see UdpServerConsole update.

Good luck... Post if you need help.
«1

Comments

  • TumblerTumbler Posts: 323
    edited 2013-02-16 23:44
    Mike, is there a way to test the spinneret in the network? Seems that i have no connection with the spinneret.
  • Mike GMike G Posts: 2,702
    edited 2013-02-17 07:01
    Ping the IP assigned to the Spinneret. What exactly are you trying to do?
    ping 192.168.1.130
    
  • Mike GMike G Posts: 2,702
    edited 2013-02-17 08:02
    Well, sorry all. I changed a namespace on the client and forgot to update the setting class so it defaulted to 192.168.1.130:10000 when it could not grab the values for the config file. I fixed the bug. I added the IP and port display on the console so you can see the configuration. Not sure why I did not do that in the first place.
  • TumblerTumbler Posts: 323
    edited 2013-02-17 11:02
    Cool Mike, nice job.

    About my previous post: somebody disconnected my spinneret from the router :innocent:

    Even works after changing the client ip into my Wan ip and settings in the router (portforwarding)

    Thx for sharing Mike
  • LtechLtech Posts: 380
    edited 2013-05-27 06:41
    Is somebody use this/try with VPSE ?
    It is a free comport router/server/emulator.


    http://www.eterlogic.com/Products.VSPE.html

    Thanks
  • oodesoodes Posts: 131
    edited 2013-08-09 01:56
    Mike,

    Great Work, Got this working and it just what I'm after. Is it a big job to convert this to TCP and instead of talking to the console talk to a remote device? What areas should I look at to convert to?

    Warm Regards

    Des
  • Mike GMike G Posts: 2,702
    edited 2013-08-09 04:12
    Is it a big job to convert this to TCP...
    Not too bad, a light weekend of coding and testing.
    ...and instead of talking to the console talk to a remote device?
    The console apps are running on a remote server. Therefore, talking to a remote server. Can you be a bit more specific? What are you trying to accomplish?
    What areas should I look at to convert to?
    You'll need a TCP server and client application to for testing. Then it is a matter of changing the socket configuration to use TCP and testing.

    I'd divide the work into two stages. First build and test serial to Ethernet. Next, build and test Ethernet to serial. Finally, test the whole package.
  • oodesoodes Posts: 131
    edited 2013-08-09 04:38
    Mike,

    A light weekend? :D . well I had it up and running on the WIZNET serial to Ethernet Gateway(Problem with that is it just does it without seeing any code ) and My Propeller chip application was interfaced with a Printer. My Prop app talks to a printer via RS232 sending and receiving commands but I now want to start using Ethernet and the Printer supports TCP. So I'm trying to get the whole thing onto the spinneret and talk to a Printer over a WAN eventually :P but for now I'm just using a Printer Simulator . Can I use the WIZNET terminal device for TCP server and Client?

    Regards

    Des
  • Mike GMike G Posts: 2,702
    edited 2013-08-09 08:54
    I'm Confused...

    What WIZNET terminal device are you referring to?
  • oodesoodes Posts: 131
    edited 2013-08-11 00:47
    With the Wiznet S2E Gateway comes a Device Terminal that allows you to set up a TCP server or Client , just wondering could I use this
  • Mike GMike G Posts: 2,702
    edited 2013-08-11 07:44
    oodes, According to the documentation the Wiznet S2E software is for use with the WiZ100SR, WiZ105SR, and WiZ110SR. These are physical Serial to Ethernet devices. Can it be used - sure - but it will not work out of the box. The Wiznet S2E software uses a packet protocol and specific ports. Spin code must be written to handle the specifications.

    IMO, it's must easier to write a simple TCP server/listener specific for the job at hand. There are free TCP listeners found through a Google search.
  • oodesoodes Posts: 131
    edited 2013-08-11 11:44
    Cheers for that Mike , a light weeks programming for the week ahead then so :D Are ya free?? :P . Sure ill give it a whirl and see how it goes. I'll be back!

    Thanks

    Des
  • Mike GMike G Posts: 2,702
    edited 2013-08-14 16:35
    Huh, turns out I built a Serial to Ethernet TCP thingy too. I guess I forgot in a haze of barley and hops...

    I wired up a quick and dirty TCP listener and client to test the TCP Spinneret Serial to Ethernet thingy. By quick and dirty I mean not well tested. The .NET source is a slightly modified version copied from MSDN.
  • oodesoodes Posts: 131
    edited 2013-08-15 01:05
    Mike,

    That great Mike, happy days really appreciate it , I have been knee deep learning VB and Embedded Ethernet any chance I had in between work so I'm looking forward to working through your code. I had come across that version on MSDN alright,

    Yours Gratefully

    Des
  • oodesoodes Posts: 131
    edited 2013-08-16 02:40
    Mike ,

    I having some trouble with the tcpListener. The TCP client works perfect , but when I run the Listener console its gets as far as
    Waiting for a connection...
    

    If I try to send message from the PST nothing happens. Do i have to initiate the handshake in some other way?

    Des
  • Mike GMike G Posts: 2,702
    edited 2013-08-16 04:32
    Dunno... have you done any troubleshooting? Verified IP addresses and ports?

    I ran the code and it worked. I realized the client listener IP and Port are hard coded in Console apps so I moved IP and Port to configuration. When ran the client again it threw an socket exception. I looked closer to find the Client is echoing the message so I turned that off. Reloaded every body and that worked. Again this is not production code.

    New code uploaded to post #14
  • oodesoodes Posts: 131
    edited 2013-08-16 05:20
    Console.jpg
    wiz.Start(WIZ#SPI_CS, WIZ#SPI_SCK, WIZ#SPI_MOSI, WIZ#SPI_MISO)   wiz.SetCommonnMode(0)
      wiz.SetGateway(192, 168, 1, 1)
      wiz.SetSubnetMask(255, 255, 255, 0)
      wiz.SetIp(192, 168, 1, 130)
      wiz.SetMac($00, $08, $DC, $16, $F8, $E1)
    
    
      
      pst.str(string("Initialize Sock", CR))
      Sock.Init(SERIAL_SOCK, TCP, 10000)
      Sock.RemoteIp(192, 168, 1, 6)
      Sock.RemotePort(8000)
    

    Thats the error I'm getting on the console when I run the app. AM i doing something wrong ?
    1024 x 672 - 79K
  • Mike GMike G Posts: 2,702
    edited 2013-08-16 05:51
    The client console app setting, clientIp, must be updated to the IP address of the system running the app, 192.168.1.6. Otherwise the error returned is "The requested address is not valid in this context."
  • oodesoodes Posts: 131
    edited 2013-08-16 06:09
    That cleared up the "The requested address is not valid in this context.". But back to the original problem. Could the firewall be blocking the spinneret? I had similar problems problems when I was using the wiznet S2E gateway. I had serial communication in one direction but no TCP to serial in the opposite, as is the case here again
    But the UDP version worked fine.....
  • Mike GMike G Posts: 2,702
    edited 2013-08-16 07:12
    Could the firewall be blocking the spinneret?
    I suppose... did you allow the IP? Do you have a utility like WireShark to watch the network traffic?

    How are you passing data to the serial port? If you are using the Parallax Serial Terminal, are you pressing enter after entering a test string?
  • oodesoodes Posts: 131
    edited 2013-08-16 07:50
    Added the IP of the Spinneret to the Firewall, Installed wireshark and data is definitely coming in over the network
    Wireshark.jpg
    with the destination and source IP address corresponding on wireshark to those on the Spin file. Yep I'm using the PST and yes I'm defo hitting the Enter key :D
    1024 x 821 - 132K
  • Mike GMike G Posts: 2,702
    edited 2013-08-16 10:26
    WireShark shows the packets flying around the network. I guess the next step is find yourself another TCP listener. You might get better results.
  • oodesoodes Posts: 131
    edited 2013-08-16 11:21
    New to this whole network programming craic so probably should try to build one myself , Ok , thanks for the input Mike. Appreciate it.
    Enjoy the weekend,

    Des
  • oodesoodes Posts: 131
    edited 2013-08-22 01:53
    Mike ,

    I ran another TCP listener Hercules and I was having the same Issues , communicating from client to the PST perfect but nothing happening in the opposite direction, as I was having the same problem with the S2E wiznet gateway, figured it had to something on my end. Turned off the Firewall and boom got comms. So happy days. I notice when I use the Hercules Tcp Client after I send data to the PST then as soon as the data is received on the PST the connection closes. Is this what is meant to happen? If so is it possible to keep the connection open ?

    Regards

    Des
  • Mike GMike G Posts: 2,702
    edited 2013-08-22 04:40
    The software is setup to "Send and Forget". Please see the source code.
    oodes wrote:
    I notice when I use the Hercules Tcp Client after I send data to the PST then as soon as the data is received on the PST the connection closes
    I assume by "keep the connection open" the idea is to send data, wait for a response, then close the connection. Is that correct? If so, update the TcpHandler method. Setup a listener and wait for a response right after sending data. Code is required to process the response, ensure the response data is not an error and, I assume, write the response to the PST.

    The snippet of code below is from the TcpHandler method with a comment to mark where the receive logic should go. You'll probably end up with redundant code blocks and want to refactor the source for code reuse.
        'Send buffered serial data  
        if(sockIO == TX)
          sock.Connect
          pause(100)
          if(sock.Connected)
            sock.Send(@serBuff, strsize(@serBuff))
    
          '''
          ''' Receive Response
          '''
          
          SocketDisconnect
          SockListen
          pause(10)
          sockIO~~ 
    

    Provide details on what you are trying to accomplish if the above is off the mark.
  • oodesoodes Posts: 131
    edited 2013-08-22 05:16
    Mike ,

    Yes I am looking for a Response so the snippet will help for sure. Basically I am sending a Printer Commands , set up fonts , Rasters etc. I send the commands to the Printer by Rs232 via a Max3232 from the Propellor and process it along with other things using the Propellor. So I'm trying to convert from Rs232 to TCP/IP long term and just use TCP/IP which is why I'm interested in the Spinneret. Now i'm just testing (with plenty of help from yourself :thumb: )by trying to send serial data over and back. Send the printer a command and check if i get the right response. As I will be talking to the Printer almost all the time , IS it possible to leave the TCP connection open all the time and never close it?
    Would the Wiznet 5200 be a better option for my long term project of scrapping Rs232 and moving completly to TCP?
    I will just use a direct connection to the Printer with a crossover cable.

    Warm Regards

    Des
  • Mike GMike G Posts: 2,702
    edited 2013-08-22 09:49
    IS it possible to leave the TCP connection open all the time and never close it?
    Possible but not a good strategy.
    Would the Wiznet 5200 be a better option...
    The Spinneret is fine unless the project required more than 4 sockets or more IO pins. The W5200 has 8 hardware sockets where the Spinneret/W5100 has 4.
    ...for my long term project of scrapping Rs232 and moving completly to TCP?
    I'm confused, if the end game is TCP communication then why mess with the added complexity of Serial to TCP. Just send TCP messages to the printer. The TCP client/server stuff works and the code code has been tested. Have you looked at the demos?
  • oodesoodes Posts: 131
    edited 2013-09-02 08:28
    Mike ,

    Are those demos up on the object exchange?

    Regards

    Desmond
  • Mike GMike G Posts: 2,702
    edited 2013-09-02 10:07
    Are those demos up on the object exchange?
    No they are in the Google Repo -> Spinneret Repo. BTW, all the W5200 libraries (parent repo) are compatible with the Spinneret. The only file not compatible is the SPI driver.

    Example:
    TcpSocketClientDemo.spin
    TcpSocketServerDemo.spin
    OBJ
     wiz   : "W5200" 
    
    OBJ
     wiz   : "W5200" 
    

    Other than the object reference, the W5200 has 8 sockets and the Spinneret has 4 (W5100). Some of the W5200 demo might require more than 4 sockets...

    One day I'd like to place the files in the OBEX but the code base changes enough where the OBEX becomes a hassle to keep the latest.
  • oodesoodes Posts: 131
    edited 2013-09-02 12:44
    Mike,

    Thanks again for your help , Much appreciated

    Des
Sign In or Register to comment.