Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Usage of serial port on cRio (http://www.chiefdelphi.com/forums/showthread.php?t=102275)

basicxman 08-02-2012 14:11

Usage of serial port on cRio
 
1 Attachment(s)
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.

mikets 08-02-2012 14:38

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 ;)

basicxman 08-02-2012 14:45

Re: Usage of serial port on cRio
 
Quote:

Originally Posted by mikets (Post 1122446)
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.

wireties 08-02-2012 14:51

Re: Usage of serial port on cRio
 
Quote:

Originally Posted by mikets (Post 1122446)
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

mikets 08-02-2012 14:54

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.

basicxman 08-02-2012 16:34

Re: Usage of serial port on cRio
 
Aha, this worked. Feeling quite silly now but thanks!


All times are GMT -5. The time now is 13:32.

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