Go to Post Mentoring is HARD. - Akash Rastogi [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 21-01-2012, 15:26
duffany1 duffany1 is offline
Registered User
AKA: Brandon
FRC #0195 (The Cyberknights)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2006
Location: Southington, CT
Posts: 17
duffany1 is an unknown quantity at this point
Thumbs up TCP/UDP Listening in C++

How do I use C++ to read data from a TCP port/socket/etc.?

To clarify, we have the LabVIEW dashboard VI sending a bunch of TCP data to port 1500. How do I access (with the C++ on the cRIO) the data that is getting sent to this port?

I have seen some stuff about "winsock" and so forth but I am getting absolutely nowhere as to how to implement it. I mean, even the include statement for "winsock2.h" isn't working ... Does anyone have any insight on this matter? I'm fairly well-versed in C++ but I'm completely new to TCP.

Last edited by duffany1 : 21-01-2012 at 23:14.
Reply With Quote
  #2   Spotlight this post!  
Unread 21-01-2012, 22:28
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,572
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: TCP Listening in C++

Quote:
Originally Posted by duffany1 View Post
To clarify, we have the LabVIEW dashboard VI sending a bunch of TCP data to port 1500. How do I access (with the C++ on the cRIO) the data that is getting sent to this port?
I can't help you with the TCP part, but based on the 2012-01-20 team update, you might want to choose a different port.
Reply With Quote
  #3   Spotlight this post!  
Unread 21-01-2012, 23:13
duffany1 duffany1 is offline
Registered User
AKA: Brandon
FRC #0195 (The Cyberknights)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2006
Location: Southington, CT
Posts: 17
duffany1 is an unknown quantity at this point
Re: TCP Listening in C++

Quote:
Originally Posted by Joe Ross View Post
I can't help you with the TCP part, but based on the 2012-01-20 team update, you might want to choose a different port.
Thanks for that. I think that I will be using UDP instead. UDP 1140 seems like the right choice.

EDIT: Not all my problems are solved. I am still having the same problem figuring out how to listen to a UDP socket.

Last edited by duffany1 : 22-01-2012 at 18:19.
Reply With Quote
  #4   Spotlight this post!  
Unread 26-01-2012, 00:23
Tommy.Brown's Avatar
Tommy.Brown Tommy.Brown is offline
Lead Programmer
FRC #1477 (Texas Torque)
Team Role: Programmer
 
Join Date: Jul 2011
Rookie Year: 2011
Location: The Woodlands
Posts: 27
Tommy.Brown is an unknown quantity at this point
Re: TCP Listening in C++

TCP and UDP code will be horrible to write in C++, especially since LabView is the officially supported language for the cRIO, because NI makes both of them, and C++/Java just happen to work, especially since I don't think WPILib has anything built in for TCP or UDP. Try using the serial port, WPILib has a class SerialPort.cpp, you would just have to modify the Labview code to send the data via serial port (to do this you will need a null modem cable, not a normal serial cable). Writing serial code for the robot in C++ is extremely easy, and the data can send really fast (not comparatively of course, but you can set the baud rate higher if needed), I think I was able to write the serial code in 3 hours.

TCP and UDP will be much harder, especially since I am not even sure if the cRIO has the capability to do that through C++.
__________________
FRC Team 1477 - Texas Torque
The Woodlands, Texas
Reply With Quote
  #5   Spotlight this post!  
Unread 26-01-2012, 02:31
mmavipc mmavipc is offline
Registered User
FRC #1622
 
Join Date: Jan 2012
Location: California
Posts: 6
mmavipc is an unknown quantity at this point
Re: TCP Listening in C++

Quote:
Originally Posted by Tommy.Brown View Post
TCP and UDP code will be horrible to write in C++, especially since LabView is the officially supported language for the cRIO, because NI makes both of them, and C++/Java just happen to work, especially since I don't think WPILib has anything built in for TCP or UDP. Try using the serial port, WPILib has a class SerialPort.cpp, you would just have to modify the Labview code to send the data via serial port (to do this you will need a null modem cable, not a normal serial cable). Writing serial code for the robot in C++ is extremely easy, and the data can send really fast (not comparatively of course, but you can set the baud rate higher if needed), I think I was able to write the serial code in 3 hours.

TCP and UDP will be much harder, especially since I am not even sure if the cRIO has the capability to do that through C++.
VxWorks supplies sockets, see the code for PCVideoServer
Reply With Quote
  #6   Spotlight this post!  
Unread 26-01-2012, 19:55
Hjelstrom's Avatar
Hjelstrom Hjelstrom is offline
Mentor
FRC #0987 (High Rollers)
Team Role: Mentor
 
Join Date: Mar 2008
Rookie Year: 2005
Location: Las Vegas
Posts: 147
Hjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond repute
Re: TCP Listening in C++

One problem is that you're trying to use "winsock". That is the windows/microsoft flavor of sockets programming. The concepts will be the same on the cRio but the specific function names and #include files will be different.
Reply With Quote
  #7   Spotlight this post!  
Unread 26-01-2012, 20:00
Hjelstrom's Avatar
Hjelstrom Hjelstrom is offline
Mentor
FRC #0987 (High Rollers)
Team Role: Mentor
 
Join Date: Mar 2008
Rookie Year: 2005
Location: Las Vegas
Posts: 147
Hjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond reputeHjelstrom has a reputation beyond repute
Re: TCP Listening in C++

Here is some information on VxWorks sockets programming:

http://www-kryo.desy.de/documents/vx...c-sockets.html
Reply With Quote
  #8   Spotlight this post!  
Unread 27-01-2012, 07:33
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,170
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Re: TCP Listening in C++

Just google for UDP socket programming examples - it is pretty straightforward. The network programming API in vxWorks is straight from BSD 4.4 and is very standard stuff. On the DS side, it should work fine with the the LabView UDP infrastructure.

Aligning your data will be more trouble that the actual socket code. For example, is the PowerPC side big or little-endian? The PC is little-endian, the PowerPC might be big endian thus you would have to byte-swap your data (there are functions in LabView to do this).

Check here for one example (http://www.linuxhowtos.org/C_C++/socket.htm). About 2/3rds of the way down a client server model using UDP is shown. This is for Linux but uses the same API as VxWorks. The winsock API (if you use C/C++ on your DS) is a wee bit different setting things up than VxWorks or Linux (one xtra call I think - WSAStartup?) but after that is identical to the examples.

HTH

Last edited by wireties : 27-01-2012 at 09:49.
Reply With Quote
  #9   Spotlight this post!  
Unread 15-09-2012, 03:28
jacob9706 jacob9706 is offline
Registered User
AKA: Jacob Ebey
FRC #3574 (High Tekerz)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2010
Location: Seattle
Posts: 101
jacob9706 is on a distinguished road
Re: TCP/UDP Listening in C++

Quote:
Originally Posted by duffany1 View Post
How do I use C++ to read data from a TCP port/socket/etc.?

To clarify, we have the LabVIEW dashboard VI sending a bunch of TCP data to port 1500. How do I access (with the C++ on the cRIO) the data that is getting sent to this port?

I have seen some stuff about "winsock" and so forth but I am getting absolutely nowhere as to how to implement it. I mean, even the include statement for "winsock2.h" isn't working ... Does anyone have any insight on this matter? I'm fairly well-versed in C++ but I'm completely new to TCP.
Look at the source code located here. We sent messages from an on board computer last year for vision processing to our c-rio

https://github.com/jacob9706/HighTekerz2012
__________________
/*
* Team 3574 Alumni
*
* 2011 - Highest Seeded Rookie
* 2011 - Rookie All-Star
* 2012 - Engineering Inspiration
* 2012 - Olympic Deans List Winner
* 2013 - Engineering Inspiration
* 2013 - Judges Award (For unique circular robot and the way the team works together.)
*/
Reply With Quote
  #10   Spotlight this post!  
Unread 25-11-2012, 21:01
mythbusterma mythbusterma is offline
Registered User
no team
 
Join Date: Nov 2012
Rookie Year: 2009
Location: ----
Posts: 10
mythbusterma is an unknown quantity at this point
Re: TCP Listening in C++

Forgive me, but doesn't the WPILib have something in it that allows you to craft what it calls "Serial Packets/Blocks" something like that? They let you put a custom payload into a packet and send it to the driver's station. I could've sworn they did.
Reply With Quote
  #11   Spotlight this post!  
Unread 03-12-2012, 19:28
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,170
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Re: TCP Listening in C++

Quote:
Originally Posted by mythbusterma View Post
Forgive me, but doesn't the WPILib have something in it that allows you to craft what it calls "Serial Packets/Blocks" something like that? They let you put a custom payload into a packet and send it to the driver's station. I could've sworn they did.
I think you are correct but the purpose to to create "bundles" that can LabView can process on the DS.
__________________
Fast, cheap or working - pick any two!
Reply With Quote
  #12   Spotlight this post!  
Unread 28-12-2012, 11:53
taichichuan's Avatar
taichichuan taichichuan is offline
Software Mentor
AKA: Mike Anderson
FRC #0116 (Epsilon Delta)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Herndon, VA
Posts: 333
taichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud of
Send a message via AIM to taichichuan
Re: TCP Listening in C++ -- BUG in WPILib

We used UDP on the 'bot last season for communicating from Arduinos to the cRIO. The one thing that we found is that the WPILib has issues with unread UDP packets. Evidently, the WPILib is always monitoring and hooking UDP traffic (it may do something similar with TCP traffic) so that rather than dropping a packet on the floor as it should, WPILib tries to buffer everything. I can see why they want to do this (in case things start to fall behind, you don't want important control packets dropped), but it's against what UDP was built to do. Nonetheless, if you don't service the UDP packets (say, it's a periodic transmission from your RPM sensor), they eventually cause the whole robot to lock up when the internal WPILib buffers overflow. This is clearly a bug in WPILib. However, you can get around it by always reading the UDP socket and tossing the packet away in your teleop disabled routine.

Good Luck,

Mike
Reply With Quote
  #13   Spotlight this post!  
Unread 08-01-2014, 22:26
yash101 yash101 is offline
Curiosity | I have too much of it!
AKA: null
no team
 
Join Date: Oct 2012
Rookie Year: 2012
Location: devnull
Posts: 1,191
yash101 is an unknown quantity at this point
Re: TCP Listening in C++

What do you guys think of this? It seems a little deep-rooted, but interesting. I think we can use some things from this.

I am trying to use net sockets to communicate to an application written in C++ on one computer to the cRIO, coded in Java!

Google is the guru with endless knowledge
Reply With Quote
  #14   Spotlight this post!  
Unread 10-01-2014, 06:22
mikets's Avatar
mikets mikets is online now
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: TCP Listening in C++

We have written a netconsole replacement called netterm. It is written in C++ using winsock to communicate with the cRIO via UDP but the netterm program is written with general purpose in mind so it has options to communicate via either UDP or TCP and can specify different ports. It even supports ANSI escape sequences for color fonts. It is purposefully written in such a way that the TCP/UDP communication is a reusable library module with a server/client pair. The server side is a multi-threaded listener that can handle multiple communication streams. If you are interested in how this work, the NetTerm source code can be found here.
http://proj.titanrobotics.net/hg/Frc...bde9d6/NetTerm
The network library can be found here.
http://proj.titanrobotics.net/hg/Frc...3bde9d6/winlib
__________________
Reply With Quote
  #15   Spotlight this post!  
Unread 10-01-2014, 18:22
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,065
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: TCP Listening in C++

WPILib has a built-in mechanism called 'NetworkTables' that you can use to transfer data to/from the robot via TCP to external programs, in particular the SmartDashboard. You can access NetworkTables from C++, Java, LabVIEW, and Python.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 05:27.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi