modifying Serial.read()?

How does the serial port work? When I use the command Serial.read(), what exactly does this function do? Can I modify the code for this function?

I have a hardware device that spits out data at a different format than the Serial.read() can read it. The ideal solution would be to use one of the other pins and read the data bit by bit. I was able to do that by modifying the SerialSoftware library, but this doesn't work for speeds higher than 9600 baud rate, and it also means that the arduino board is constantly engaged in monitoring the pin as there is no buffer that stores the incoming data, unlike the serial pin.

When I use the command Serial.read(), what exactly does this function do?

Serial is a class that has several methods to handle serial communications. You should have a understanding of all the methods and how they need to work together to be successful. For instance one should not perform a Serial.read() until first having performed a Serial.available() to see if indeed there is a serial character in the serial buffer ready to be read.

Here is a good reference:

Lefty

Thank you.

I've read that reference and also this: http://arduino.cc/en/Serial/Read. The link you provided and provides information on how to use the Serial class and related methods (black box), but not what they actually do.

The device I have gives data with MSB first, while (I think) the Serial.read LSB first, so all the bytes are inverted. I made a function to "uninvert" it back, and it works fine, but I also suspect that there is a discrepancy with respect to start/stop bits.

What I'm looking for is the definition of the Serial class and all the methods inside (the actual code). What I want to do is redefine the Serial.read() method.

Mohamed

You will have much better luck modifying NewSoftSerial...
http://arduiniana.org/libraries/newsoftserial/

OMG thanks a lot

This is exactly what I needed. I think my problem was the my code is doing too much and it may have been unable to read and process data too quickly, and maybe that's why I was missing some bytes. Because NewSoftSerial uses interrupts instead, it now works fine!

but I also suspect that there is a discrepancy with respect to start/stop bits.

Most likely not. The start and stop bit are something the internal usart hardware uses or generates and is never read by or written to by one's software. Software only has a role in configuration of the channel as far as how many data bits, how many stop bit, if parity bit is used or not and of course the baud rate to use.

I think the best reference for the serial class is reading the code itself. It's in your Arduino core files named HardwareSerial.h and HardwareSerial.cpp, there will be all the configuration details, and of course you are free to modify them if you wish, as long as you realize that you lose portability of sharing the code with any other arduino user.

As far as having to reverse the data bits in your received data, that is pretty unusual, but I have seen that also with a telemetry system I needed to communicate with decades ago. I too just wrote functions to flip the bits around.

Lefty

I have a hardware device that spits out data at a different format than the Serial.read() can read it.

curious, what hardware?

RFID reader from Wavetrend.

I think the problem initially with the UART was because the reader uses inverse logic. So for every byte that I read, there was a bunch of processing I did. Perhaps that made it miss some bytes. The NewSoftSerial doesn't actually work perfectly, there are some inaccuracies. I posted these problems at another thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294104422/0

Please let me know if you have any suggestions. The only one I can think of right now is to go back to the hardware serial and do the processing after getting all the bytes I'm expecting. I'll try that out and post an update soon.

The device I have gives data with MSB first,

So for every byte that I read, there was a bunch of processing I did

If you have 256 spare RAM bytes use a lookup table, one C instruction and you're done.


Rob