|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Usage of serial port on cRio
We're having a lot of difficulty using the serial port on the cRio to interface with devices.
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]);
}
}
|
|
#2
|
||||
|
||||
|
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 ![]() |
|
#3
|
|||
|
|||
|
Re: Usage of serial port on cRio
Quote:
|
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
Re: Usage of serial port on cRio
Aha, this worked. Feeling quite silly now but thanks!
|
|
#6
|
||||
|
||||
|
Re: Usage of serial port on cRio
Quote:
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 Last edited by wireties : 08-02-2012 at 15:06. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|