Rei Vilo 695 Posted March 20, 2013 Share Posted March 20, 2013 I was looking for a software serial library but with TX only. Using the Saleae logic analyser, I recorded the hardware TX signals and then worked on obtaining the same signals with the library. By very basic, I mean one single speed 9600 and one single configuration is 8N1. The SerialTX class is derived from Stream and thus benefits from all the print() functions: class SerialTX : public Stream The most critical part is tweaking the delays #define DELAY_START_BIT 90 ///< start bit #define DELAY_SEND_BIT 88 ///< data bits #define DELAY_STOP_BIT 88 ///< stop bit Using it is easy: #include "serialTX.h" SerialTX myTX(P2_0); // Serial TX on pin P2_0 void setup() { myTX.begin(); myTX.print("Hello!"); } void loop() { } Enjoy Updated library release 1.08 SerialTX_108.zip yosh and roadrunner84 2 Quote Link to post Share on other sites
roadrunner84 466 Posted March 21, 2013 Share Posted March 21, 2013 Nice and elegant. read() should return -1 as -1 is defined as the "no characters available" constant. Same for peek() I don't know if it's necessary to override flush(), but it should return a bool according to Stream, Serial on the other hand claims it returns void. This is kind of confusing. I like the initializer list for constructors, but this is more of a preference I guess // Plain implementation SerialTX::SerialTX(uint8_t pin) { _pin = pin; } // Initializer list SerialTX::SerialTX(uint8_t pin) : _pin(pin) { } Which would also allow for _pin to be const uint8_t, which in turn can allow compiler optimisations. I'd put the DELAY_..._BIT defines in the cpp file; the h file should only expose that which is of interest to the user. Rei Vilo 1 Quote Link to post Share on other sites
Rei Vilo 695 Posted March 21, 2013 Author Share Posted March 21, 2013 Thank you for your comments and the correct value -1 for read() and peek(). The initialiser list trick doesn't seem to reduce memory size. I've updated the library accordingly. See new release 1.07 on the initial post. Next goal is a software I2C library... gsutton 1 Quote Link to post Share on other sites
roadrunner84 466 Posted March 21, 2013 Share Posted March 21, 2013 I don't say using initializer lists will reduce memory (though in combination with const uint8_t _pin; it could save you 1 or 2 bytes of ram), but it could reduce memory usage on higher optimisation levels. Quote Link to post Share on other sites
pivden 6 Posted March 21, 2013 Share Posted March 21, 2013 use noInterrupts(); or not use noInterrupts(); ... that is the question ;-) Quote Link to post Share on other sites
Rei Vilo 695 Posted March 21, 2013 Author Share Posted March 21, 2013 Maybe using <template> can save some extra bytes... Quote Link to post Share on other sites
roadrunner84 466 Posted March 22, 2013 Share Posted March 22, 2013 You could use <template> for say an 8E1 or a 7O2 version, but using template for e.g. the pin would not save you at best two bytes of memory, while two instances of SerialTX would double used memory in both ram and flash. I suggest strongly against using template for things that can very well be put in parameters without much added effort. Maybe using the before suggested initializer list and const private member in combination with a declaration like const SerialTX myserial(8); could do the trick, though I doubt that entire objects can be put in flash by the compiler, but you never know... Quote Link to post Share on other sites
Taiko 0 Posted May 26, 2013 Share Posted May 26, 2013 There's a case sensitivity error that triggers a compiler error: In serialTX.cpp it should be #include "serialTX.h" instead of #include "SerialTX.h" Quote Link to post Share on other sites
Rei Vilo 695 Posted May 30, 2013 Author Share Posted May 30, 2013 There's a case sensitivity error that triggers a compiler error: In serialTX.cpp it should be #include "serialTX.h" instead of #include "SerialTX.h" Thank for noticing me. Please find updated library release 1.08 on the leading post. Quote Link to post Share on other sites
B@tto 51 Posted April 2, 2015 Share Posted April 2, 2015 Hi Rei, I just tested your library, I had to tune delays : #define DELAY_START_BIT 95///< start bit #define DELAY_SEND_BIT 93 ///< data bits #define DELAY_STOP_BIT 93 ///< stop bit */ Maybe because digitalWrite() is a little bit faster since you wrote this. But note that my timing are just base on my experiment, I didn't any measure, just experiment different timings. This ones worked. yosh and Rei Vilo 2 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.