Go to Post I saw 2274, a rookie team that had won the regional, gathered around their mentor. He was holding their trophy and saying, "This is what it is all about. Teamwork and cooperation. You guys did great." - Po-ser [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 Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 08-02-2012, 14:11
basicxman basicxman is offline
Emily Horsman
FRC #2200 (MMRambotics)
Team Role: Programmer
 
Join Date: Oct 2007
Rookie Year: 2007
Location: Burlington, Ontario
Posts: 971
basicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant future
Send a message via AIM to basicxman Send a message via MSN to basicxman Send a message via Yahoo to basicxman
Usage of serial port on cRio

We're having a lot of difficulty using the serial port on the cRio to interface with devices.
  • Console out switch is off.
  • Tried with an RS232 compass sensor
  • Tried with an Arduino spitting out a single byte in a loop
  • Confirmed correct serial data on both devices with a logic analyzer and Portmon

When running some very simple code (I sure hope I'm just doing something wrong) - the cRio will read some data (slowly) and then crash.

Code:
virtual void TeleopPeriodic() {
  int n = com.GetBytesReceived();
  if (n > 0) {
      char *buffer;
      printf("Reading\n");
      com.Read(buffer, n);
      printf("Done reading\n");

      for (int i = 0; i < n; i++) {
          printf("%d 0x%X\n", n, buffer[i]);
      }
}
Screenshot attached.
Attached Thumbnails
Click image for larger version

Name:	machine_check_error.png
Views:	29
Size:	55.1 KB
ID:	11800  
Reply With Quote
  #2   Spotlight this post!  
Unread 08-02-2012, 14:38
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
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: Usage of serial port on cRio

I don't have Wind River in front of me at the moment so I can't verify the com class especially the Read method, but my instinct tells me that you declare buffer as a "char *" which means there is no buffer associated with it. buffer is an uninitialized pointer. If the Read method is going to put bytes into buffer, it is going to trash random memory locations.

I could be wrong
__________________
Reply With Quote
  #3   Spotlight this post!  
Unread 08-02-2012, 14:45
basicxman basicxman is offline
Emily Horsman
FRC #2200 (MMRambotics)
Team Role: Programmer
 
Join Date: Oct 2007
Rookie Year: 2007
Location: Burlington, Ontario
Posts: 971
basicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant future
Send a message via AIM to basicxman Send a message via MSN to basicxman Send a message via Yahoo to basicxman
Re: Usage of serial port on cRio

Quote:
Originally Posted by mikets View Post
I don't have Wind River in front of me at the moment so I can't verify the com class especially the Read method, but my instinct tells me that you declare buffer as a "char *" which means there is no buffer associated with it. buffer is an uninitialized pointer. If the Read method is going to put bytes into buffer, it is going to trash random memory locations.

I could be wrong
Perhaps. But the data being printed is correct, however it's slow and crashes afterwards.
Reply With Quote
  #4   Spotlight this post!  
Unread 08-02-2012, 14:51
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: Usage of serial port on cRio

Quote:
Originally Posted by mikets View Post
I don't have Wind River in front of me at the moment so I can't verify the com class especially the Read method, but my instinct tells me that you declare buffer as a "char *" which means there is no buffer associated with it. buffer is an uninitialized pointer. If the Read method is going to put bytes into buffer, it is going to trash random memory locations.

I could be wrong

Nope, you are 100% correct. They have declared/reserved no storage. Change char *buffer to char buffer[X] where X is big enough to hold the data. It should work then but it is a bit of a kludge, you probably want a bigger buffer declared as a global or allocate the memory so it is not on the stack (as all local variables are).

HTH
__________________
Fast, cheap or working - pick any two!

Last edited by wireties : 08-02-2012 at 15:06.
Reply With Quote
  #5   Spotlight this post!  
Unread 08-02-2012, 14:54
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
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: Usage of serial port on cRio

Of course it looks correct (at least before it crashed). Imagine the buffer pointer is pointing to some random memory, so the Read method will start copying the bytes to the random location. Your print loop will also print the bytes from this random location. But what is this random location anyway? What if the random location contains the return address of your function (which is very likely)? So at the end of your function when you are returning to the caller, since the return address is trashed, you returned to la la land, thus crashed.
__________________

Last edited by mikets : 08-02-2012 at 14:57.
Reply With Quote
  #6   Spotlight this post!  
Unread 08-02-2012, 16:34
basicxman basicxman is offline
Emily Horsman
FRC #2200 (MMRambotics)
Team Role: Programmer
 
Join Date: Oct 2007
Rookie Year: 2007
Location: Burlington, Ontario
Posts: 971
basicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant future
Send a message via AIM to basicxman Send a message via MSN to basicxman Send a message via Yahoo to basicxman
Re: Usage of serial port on cRio

Aha, this worked. Feeling quite silly now but thanks!
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 03:00.

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