Bug in SoftwareSerial ... solution

I was working with the Software Serial Lib to create a new library for a Serial LCD display. I was having a problem with getting an x-y positioning routine to work right reliably. To make a long story short, if you call print(x) and the value of x is zero (0) nothing prints.

The workhorse routine printNumber() is shown below. The call to printByte('0') is the problem. It should be print('0') . I believe printByte() is a HW Serial routine. I suspect the '0' was being sent back up the serial line to the Arduino IDE which blissfully ignored it. When I changed the code from printByte('0') to print('0') it would now print the '0' when invoked as print(x) where 'x' has a value of zero.

Further proof of the pudding is the recompiled demo sketch is 1200-1300 bytes smaller! I suspect that number corresponds to the HWSerial object size that is no longer being loaded.

I have written three libraries and corrected a fourth (the Software serial). My reason for creating the first was as a basis for the LCD module. But there are any number of modules one might interface to an Arduino which have one way serial communictions and it seemed logical to create libraries for each direction so you don't waste a precious second pin.

1 - SWSerialXmit - a transmitt serial only library (simple surgery on the original SoftwareSerial)
2 - SWSerialRcv - a receive serial only library (same)

3 - SWSerialLCDPHA - a transmit only software serial library specifically for an LCD display
running under a Peter Anderson controller chip.

4 - SoftwareSerial - the Arduino 08 delivered library corrected as mentioned above.

These four libs can be found in a zipfile on my website http://www.wulfden.org/Arduino/ArduinoSWSerLibs.zip.

The short demo sketch I wrote to show off the new lib with a 4x20 display on one of my K107 serial LCD controller boards (http://www.wulfden.org/k107/) with an Anderson chip is available at http://www.wulfden.org/Arduino/LCDPHA_Demo.zip.

cheers ... BBR


// Private Methods /////////////////////////////////////////////////////////////

void SoftwareSerial::printNumber(unsigned long n, uint8_t base)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;

if (n == 0) {
printByte('0');
return;
}

while (n > 0) {
buf[i++] = n % base;
n /= base;
}

for (; i > 0; i--)
print((char) (buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
}

Whoops, that was silly.

Thanks for catching the error; I've fixed it in the Arduino source, it will be included in Arduino 0009.

Whoops, that was silly.

Thanks for catching the error; I've fixed it in the Arduino source, it will be included in Arduino 0009.

You are welcome. I am an Arduino newbie, so could you verify for me my suspicion that the 1300 byte shrinkage was from the HWSerial not being loaded?

I assume that HWSerial is an always available structure that does not need to be invoked as I did with the SoftwareSerial,

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

which is why the printByte() didn't generate a compile error?

cheers ... BBR

Yes, the shrinkage was almost certainly caused by the hardware serial code no longer being linked it. The call to printByte() won't actually work without a call to Serial.begin() or beginSerial(), but it will compile and link.

Ok. Probably a dumb noob question but where did you find the srouce for SoftwareSerial? Are the sources for other libs available too?

The source code for the core code and libraries comes with the Arduino software. It's in lib/targets/arduino and lib/targets/libraries.

Thanks! I found the source and patched it.

I just ran into this problem. Switched to software serial so I could do usb logging on a VDIP1 and use software serial for the LCD.

Thanks for a solution. I'll apply it right now.