Serial comm review
Rsadeika
Posts: 3,837
This is a rehash of serial I/O. The program below works sort of as expected, the problem is, in the while loop I am expecting to have a char "A" displayed on the external terminal screen, every second, instead I get eight "A"s displayed at one time, maybe every eight seconds, a buffer issue? I think this was discussed a couple years ago, but I do not think that there was a satisfactory resolution. Since there has been some minimal discussion in another thread, I thought maybe it could get expanded here.
Ray
Ray
/** * @file PPred0.c * This is the main PPred0 program start point. */ #include <propeller.h> #include <stdlib.h> #include <stdio.h> #include <driver.h> extern _Driver _SimpleSerialDriver; extern _Driver _FullDuplexSerialDriver; _Driver *_driverlist[] = { &_FullDuplexSerialDriver, NULL }; /** * Main program function. */ int main(void) { /* Baud Rx Tx */ FILE *term = fopen("FDS:115200, 27, 26", "r+"); waitcnt(CLKFREQ + CNT); printf("Start of program\n"); fprintf(term,"This is on the terminal screen.\n"); while(1) { fputs("A",term); sleep(1); } fclose(term); return 0; }
Comments
Dave has shown how to make output unbuffered (although you'll have to change his example to use "term" instead of "stdout" for your program). The other option is to add an explicit fflush(term); after fputs("A",term);. This will force output to be printed to the terminal.
Ray
Read more about _IOCOOKED here: http://propgcc.googlecode.com/hg/doc/Library.html#cooked
I'll also note that we've recently added an _IONONBLOCK flag that will allow for non-blocking reads (which return -1 immediately if no data is available); you'll see it in the document linked to above because that's in the current source tree, but _IONONBLOCK hasn't made it into a release yet.
Great! Thanks.
These baby steps are leading up to a program(s) that will work with an XBee attached to the console terminal unit, and one attached to a remote unit.
Ray
Also, the comment /* If no keypress on console terminal */ is misleading; testing "if (stdin == NULL)" does not check to see if there is a keypress, it checks to see if stdin is initialized (which it always will be, so there's no need for that test).
Currently there is no easy way to try to get a key without pausing, so the program as structured will probably not do what you want. In the next release there will be an _IONONBLOCK flag that you can set on a FILE to cause reads to return immediately if no data is available.
I also included a Spin version of the use of Sensirion, and XBee modules, this works as expected, so maybe someone can try doing a spin2cpp conversion to see if they can get better results than I did. Feedback would be appreciated, just to keep my sanity in check LOL.
Ray