Serial to Ethernet and Back
Mike G
Posts: 2,702
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
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.
2) Spin Socket Setup
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.
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.
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.
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.
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.
Comments
About my previous post: somebody disconnected my spinneret from the router
Even works after changing the client ip into my Wan ip and settings in the router (portforwarding)
Thx for sharing Mike
It is a free comport router/server/emulator.
http://www.eterlogic.com/Products.VSPE.html
Thanks
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
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?
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.
A light weekend? . 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
What WIZNET terminal device are you referring to?
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.
Thanks
Des
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.
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
I having some trouble with the tcpListener. The TCP client works perfect , but when I run the Listener console its gets as far as
If I try to send message from the PST nothing happens. Do i have to initiate the handshake in some other way?
Des
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
Thats the error I'm getting on the console when I run the app. AM i doing something wrong ?
But the UDP version worked fine.....
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?
Enjoy the weekend,
Des
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
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.
Provide details on what you are trying to accomplish if the above is off the mark.
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
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.
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?
Are those demos up on the object exchange?
Regards
Desmond
Example:
TcpSocketClientDemo.spin
TcpSocketServerDemo.spin
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.
Thanks again for your help , Much appreciated
Des