jeffcraighead

Hi all, I've just started working with the Arduino and have not been able to find a way to set the Data, Parity and Stop bits for the serial libraries. I've got a motor controller that needs 7-E-1, not 8-N-1.

Thanks!

drspectro

I wrote a library for this, I put off posting it because I wanted to add some more interesting stuff for error checking. Its short so I am putting the code in line. I didnt find this elsewhere, but its pretty simple so it may be on the site already. Note if you dont want to use the library, you can simply lift the setserial sub.FYI - I have worked out a couple of tricks to check for parity errors if you get into that. The best one requires editing wiring_serial.c, but I found a kluge that can be done from a program.

jeffcraighead

The #endif at the bottom of SerialExtension.h is missing the associated if, and if I remove it there are a few other errors because it looks like something is missing or redefining something. What is the #if line at the top of the file?

I'm getting this error now:

In file included from /Applications/arduino-0015/hardware/cores/arduino/WProgram.h:4,

drspectro

Also, you can test as follows.1) initialize the serial port as usual2) set the baud/parity/stop bits3) loop and send a string repeatedly, (delay a second each time makes it a little easier)

4) start your sketch, then EXIT the Arduino GUI. Start Hyperterm you should receive your test string. You should have to set hyperterm to match your serial settings. Make sure you select the right com port in hyperterm.

SerialExtension.cpp: In function 'void SetSerial(long int, char, int, int)':SerialExtension.cpp:19: error: 'UBRRH' was not declared in this scopeSerialExtension.cpp:20: error: 'UBRRL' was not declared in this scopeSerialExtension.cpp:23: error: 'UCSRB' was not declared in this scopeSerialExtension.cpp:23: error: 'RXEN' was not declared in this scopeSerialExtension.cpp:24: error: 'TXEN' was not declared in this scopeSerialExtension.cpp:27: error: 'RXCIE' was not declared in this scopeE:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp: In function 'void SetSerial(long int, char, int, int)':

E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:19: error: 'UBRRH' was not declared in this scope

E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:20: error: 'UBRRL' was not declared in this scope

E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:23: error: 'UCSRB' was not declared in this scope

E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:23: error: 'RXEN' was not declared in this scope

E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:24: error: 'TXEN' was not declared in this scope

E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:27: error: 'RXCIE' was not declared in this scope

I get the same results in versions 15 & 16 of the Arduino environment.

drspectro

I still want to get the library fixed because it covers all the parameters at once. I would hope that would be easier in the long run..... Also dont be afraid to go straight to the AVR datasheet for stuff like this. It is usually pretty clear which registers to mess with and you can look at the wiring code to figure out the syntax.

I have a device that communicates on 1200-8-N-0. All of that is possible minus the 0 stop bits. Anyway to get the arduino to do this?

Are you really sure about the 0 stop bit requirement? I say that because asynchronous communication hardware is built around the requirement for the stop bit and start bit to determine the ending of one character and the starting of the next, it just doesn't work without the stop bit. I've worked with the RS-232 asynchronous comm links for many decades and I've never heard of a zero stop bit ability. Now there are synchronous communications links that use no start or stop bits but rather either an independent or impeded clock to keep the sender and receiver in step for character transmission.

So might you be working with a serial synchronus communicating device?

Note: the AVR processor can be programmed to use synchronous communications, however with the standard Arduino serial software commands and a new clock wire would have to be wired between the Arduino and the external device.

Lefty

REPthe603

I'm trying to read data that is coming in with 2 stop bits. Now I see that the Atmel datasheet says that on page 198, BIT3 of USBSn sets the stop bits, but only the transmitter cares. From my rough knowledge of how asynchronous communications work, is that because the receiver sees a single stop bit and doesn't care what happens after that until the next start (low) bit? If not, how would I go about changing the receiver settings for stop bits? Thanks!

is that because the receiver sees a single stop bit and doesn't care what happens after that until the next start (low) bit? If not, how would I go about changing the receiver settings for stop bits? Thanks!

That's exactly right. Remember that after a single character with a proper stop bit detected, there may be no further characters for several seconds or even mins depending on the application.

Once a receiver detects that a expected stop bit is proper, it is willing to wait forever for only one further event, a start bit. That's why it's called asynchronous communications because the receiving end has no advance information on when and if the next character may be sent.

Interesting the purpose for more then a single stop bits (1&1/2 stop bits was an option in older times as well as 2 stop bits) dates back to the old mechanical teletype machines that used asyn communications. It allowed a little more time for receiving machines to "catch up" in case the transmitter was sending slightly faster then the agreed on baud rate.

The half bit in the old 5 data bit standards also allowed synchronisation to a continuous stream of data. For example a standard way of keeping the channel open (free from other signals) was to transmit a continuous string of "RYRYRYRYRYRY....". These characters were chosen because they were at each end of the mechanical rotation limit of the print head. This was transmitted over short wave. Now if you tuned into this continuous stream you could start anywhere in it. If you had a stop bit time of exactly one or two bits you could tap into this stream and miss read it mistaking start bits and stop bits for data bits. The half period stop bit prevented this from happening.