Serial I/O with Propeller C code
BTL24
Posts: 54
What is the simplest way to send Serial commands with the Prop C system?
I have been attempting to get my Propeller (Activity Board) to transmit and receive simple command / status to/from my Motor Mind B (DC motor controller) at 2400 BAUD... both Parallax products and am having mixed results. Primarily because I don't know which simple serial command to use. I have looked through tutorials and example code.... but nothing is available to provide simple example or reference... other than the libraries.. which sometimes lack the practical example.
So, I tried "fdserial" structure, and then just "serial" structure. The half duplex "serial" function seems to be able to get the Motor Mind to run. When I try and read back status, I get weird results.
I would like to use something very simple like the SX command SEROUT or SERIN, where the pin, baud and value are included in the command. Also, it allows for multiple bytes to be sent with one command string.
Anything in the libraries that would do something similar to the SX or BASIC stamp commands?
Thanks,
Brian (BTL24)
I have been attempting to get my Propeller (Activity Board) to transmit and receive simple command / status to/from my Motor Mind B (DC motor controller) at 2400 BAUD... both Parallax products and am having mixed results. Primarily because I don't know which simple serial command to use. I have looked through tutorials and example code.... but nothing is available to provide simple example or reference... other than the libraries.. which sometimes lack the practical example.
So, I tried "fdserial" structure, and then just "serial" structure. The half duplex "serial" function seems to be able to get the Motor Mind to run. When I try and read back status, I get weird results.
I would like to use something very simple like the SX command SEROUT or SERIN, where the pin, baud and value are included in the command. Also, it allows for multiple bytes to be sent with one command string.
Anything in the libraries that would do something similar to the SX or BASIC stamp commands?
Thanks,
Brian (BTL24)
Comments
Try this: http://learn.parallax.com/propeller-c-start-simple
Thanks, but I have been through that site with fine toothed comb...a few times. Can't find anything on serial interface.
The libraries have quite a group of "text" routines with serial commands. Couldnt find anything that appeared to be a solution to my problem. I do wish they had examples to follow.
I just want to send and receive 2 or 3 bytes at a time from serial interface with sensors, motor controllers....the fundamentals. I just cant figure it out with the prop...I can't find a reference manual geared toward both the prop and C. Lots of places to look, but confusing when you want a simple reference manual with the most basic commands.
Sorry for vent. I spent way too much time searching for answers today. In comparison, I could have finished this task in 30 minutes with SX or Stamp. But, I want to branch out to prop and C....so much of this is my learning curve.
Thanks for the link anyways...
Regards,
BTL24 (Brian)
The C serial interface uses the standard I/O approach, which is great for user input and output, but it's often an overkill for talking to other devices. Using the standard C serial I/O to talk to device requires working around the buffering that is done on the input and output. I think your best bet is to use the FullDuplexSerial object and convert it from Spin/PASM to C using spin2cpp. I create a test program that uses the converted FullDuplexSerial object, and attached it below. It prints "Hello World" to the console, and the reads characters and echos them back. It uses pins 31 and 30 for serial RX and TX at a baud rate of 115200. You can change these to the pins and baudrate that you need to use.
Dave
Dave,
Thanks for your response and I better understand that prop C may be a tad more burdensome when working with serial I/O. So you think I should go back to full duplex.. Hmmmm. I am having some luck with the half duplex mode using "serial_txchar". It is the "serial_rxchar" that I am getting nonsense answers back. So it could be I have set up the commands wrong... or the Motor Mind B is malfunctioning. I think it is my ignorance with Prop C serial I/O.
If you are interested....I have attached a sample of code with several serial commands to the Motor Mind B (a DC motor controller). As you can see, the code gets cumbersome with all the "serial_txchar" commands.... rather than one command that sends a string of HEX values out to the port, all at once. This is especially worrisome when using the "serial_rxchar" in multiple calls, as I don't know the timing of the port and the sending unit (MMB) may have already finished its transmission before the prop sees it.
I have also tried moving groups of code into functions... but then I had to open a serial port in each function.... that adds a time delay since the function has to keep opening the port every time it is called. It would be great if I could "publicly" open the serial port called MMB and then use it within the function code. I tried using the PUBLIC parameter... with no success (again, due to my inexperience with prop C).
If you move "serial *MMB;" out of the main function, you can access it from any function.
A SERIN equivalent will require using fdserial instead of serial.
The semantics of serin(...) are not the same as SERIN for pbasic, because i'm too lazy to implement setjmp at the moment.
Wow! Thanks for all the quick feedback and the examples. I love examples.
So many ways to slice it... but it looks like fdserial is going to be the final answer... Thanks.
For starters though, I took Jazzed's recommendation and moved "serial MMB" outside of the Main function (still using half duplex) .... and VOILA... it worked. No more build errors and the functions now are very responsive. At least the transmit portion of my problem using functions has a temporary solution. I don't have to keep opening new serial ports in each function. I learned something here.. thanks.
Now I will need to read more on fdserial and study Jazzed examples and C version of SERIN and SEROUT.
NOW if you are wondering what I am using this for... check out my posts on DIYChristmas.org (Christmas lighting forum). I am building a new animated prop... a Candy Cane Factory.
Here is the link....
http://www.diychristmas.org/vb1/showthread.php?1138-Candy-Cane-Factory-Lightman-Version
Thanks again,
BTL24 (Brian)
I have done the serial setup, per the instructions, but I do not see any C samples, all samples are SPIN based.
It should work as long as it's a serial port device and you have the right baudrate and signal sense.
A fully specifiable 7 bit or 8 bit library is probably months away considering my schedule.
If anyone has any requests as I get started on this, let me know. The interface will likely be very similar to my SPI interface (unless of course someone chimes in with a better idea).
And, as always, this code is public on Github so anyone is free to help!
Look for the beginnings of this code to start showing up in commit logs either tonight or within the next few days. I do not expect anything to be testable, let alone operational, for at least a week.
--Edit--
Perhaps this will go a lot faster with jazzed's post for 7-bit serial. Just need to convert to assembly and add some options now
No complaints!
I'm exited to try it out (and pick thru the brains).
Let me know how many man-hours I owe you, I will gladly repay with CNC machine time.
-EP
Eric, are you looking for simplex, half duplex, or full duplex?
David
-EP
Did you try to modify the program to make input work?
libfdserial.c (functional test harness of 8 bit full duplex) ~ libserial7.c (Though 7 bit is based on half duplex "serial7.h")
fdserial.c is 8 bit ~ serial7.c (says 7 bit full duplex) is about 30% of the code of fdserial.c (not sure why)
serial.h ~ serial7.h
serial_rxtx.c ~ serial7_rxtx.c
fdserial.h (8 bit) has no 7 bit companion.
Just trying to compare files at this point and find any issues.
The fact that the output from the prop to "Realterm" is fine in 7 bit gives me much hope it will be a small code issue on the input side.
Nearly there!
-EP
The zip I posted doesn't have any actual fdserial code in it.
Try this one ... no cog required like before. I verified it works with putty in 7 bit mode.
same line.
I have a gut feeling that this must be something I'm doing wrong on the compiler end.
Here is my compile debug if it is of any value:
How many bytes get loaded?
I tested with putty using N17 (71N) and everything worked fine. Didn't realize you need E72 ....
Guess I'll make a E72 version when I'm not dead tired.
I really just need the prop to have full serial flexibility like the BS2 serin/out commands.
I didn't want to design around the BS2sx/px or BS2p for obvious reasons, but if I have to for a while, I guess that is a no-brainer.
Take a break, Jazzed !
Thanks so much for getting propeller 7 bit I/O this far!
-EP
Code is still only in the nightly branch - I'll merge with the main 2.0 release branch tomorrow night after I finish up the documentation. Again, the example project should provide all the info you need, but let me know if you have questions.
David, you are speaking of PropWare?