Why Characters Received in Serial Connection Only After Pressing Enter

Why characters received in serial connection only after pressing enter?

but I see the characters in the board console, only after pressing "enter" in teraterm

The behavior you describe is characteristic of canonical read (aka reading lines).

The behavior you seem to want is called non-canonical read (aka raw read or binary read).

  1. why are the characters received in Linux driver only when pressing enter key ?

No, the Linux serial port driver is receiving every character as it is appears on the wire.

Each character is buffered (typically in DMA-able memory), and then forwarded to a line discipline handler, which is also buffering the received data.

A canonical read() syscall by the userland program is blocked until the line discipline handler detects a line termination character.


  1. Is there some way to receive the characters without pressing the enter key ?

Yes, before issuing the cat command, configure the serial port to non-canonical mode:

stty -F /dev/tty05 raw

or more likely the correct device node is

stty -F /dev/ttyO5 raw

Or use the termios interface to configure the serial port to non-canonical mode in a userspace program on the board. Sample code is here.

Documentation on how to properly program a serial port are
Serial Programming Guide for POSIX Operating Systems and Setting Terminal Modes Properly.

How to send characters in PuTTY serial communication only when pressing enter?

The settings you need are "Local echo" and "Line editing" under the "Terminal" category on the left.

To get the characters to display on the screen as you enter them, set "Local echo" to "Force on".

To get the terminal to not send the command until you press Enter, set "Local line editing" to "Force on".

PuTTY Line discipline options

Explanation:

From the PuTTY User Manual (Found by clicking on the "Help" button in PuTTY):

4.3.8 ‘Local echo’

With local echo disabled, characters you type into the PuTTY window are not echoed in the window by PuTTY. They are simply sent to the server. (The server might choose to echo them back to you; this can't be controlled from the PuTTY control panel.)

Some types of session need local echo, and many do not. In its default mode, PuTTY will automatically attempt to deduce whether or not local echo is appropriate for the session you are working in. If you find it has made the wrong decision, you can use this configuration option to override its choice: you can force local echo to be turned on, or force it to be turned off, instead of relying on the automatic detection.

4.3.9 ‘Local line editing’
Normally, every character you type into the PuTTY window is sent immediately to the server the moment you type it.

If you enable local line editing, this changes. PuTTY will let you edit a whole line at a time locally, and the line will only be sent to the server when you press Return. If you make a mistake, you can use the Backspace key to correct it before you press Return, and the server will never see the mistake.

Since it is hard to edit a line locally without being able to see it, local line editing is mostly used in conjunction with local echo (section 4.3.8). This makes it ideal for use in raw mode or when connecting to MUDs or talkers. (Although some more advanced MUDs do occasionally turn local line editing on and turn local echo off, in order to accept a password from the user.)

Some types of session need local line editing, and many do not. In its default mode, PuTTY will automatically attempt to deduce whether or not local line editing is appropriate for the session you are working in. If you find it has made the wrong decision, you can use this configuration option to override its choice: you can force local line editing to be turned on, or force it to be turned off, instead of relying on the automatic detection.

Putty sometimes makes wrong choices when "Auto" is enabled for these options because it tries to detect the connection configuration. Applied to serial line, this is a bit trickier to do.

Received message differs from sent message over serial connection

The kind of gibberish you're getting indicates three possible causes:

-A mismatch of settings, most likely different baud rates

-Noise on the bus: frequently originating from lacking a common ground reference. This happens when you have not connected only connect the TX and RX signals but leave the GND floating (on desktop computers you'll likely have a common ground even if you don't connect the GND signals because of the safety ground on your house's wiring but on laptops or other battery-powered devices this is a problem and you have to use a third cable to connect GND on both sides of the bus).

-You're trying to communicate using different logic levels. This happens if one side of the bus is working on TTL levels (5V) and the other on 3.3V or when you have a real RS232 level (differential) combined with any of the others. Not to mention if you try to mix RS485 with RS232 or TTL, which, of course, won't work either.

How to avoid pressing Enter with getchar() for reading a single character only?

On a linux system, you can modify terminal behaviour using the stty command. By default, the terminal will buffer all information until Enter is pressed, before even sending it to the C program.

A quick, dirty, and not-particularly-portable example to change the behaviour from within the program itself:

#include<stdio.h>
#include<stdlib.h>

int main(void){
int c;
/* use system call to make terminal send all keystrokes directly to stdin */
system ("/bin/stty raw");
while((c=getchar())!= '.') {
/* type a period to break out of the loop, since CTRL-D won't work raw */
putchar(c);
}
/* use system call to set terminal behaviour to more normal behaviour */
system ("/bin/stty cooked");
return 0;
}

Please note that this isn't really optimal, since it just sort of assumes that stty cooked is the behaviour you want when the program exits, rather than checking what the original terminal settings were. Also, since all special processing is skipped in raw mode, many key sequences (such as CTRL-C or CTRL-D) won't actually work as you expect them to without explicitly processing them in the program.

You can man stty for more control over the terminal behaviour, depending exactly on what you want to achieve.

Sending ENTER key through serial port

The microsoft version of enter or new line is \r\n which is 0x0d 0x0a in hex.

  • \r is the carriage return

    In a shell or a printer this would put the cursor back to the beginning of the line.

  • \n is the line feed

    Puts the cursor one line below, in some shells this also puts the cursor to the beginning of the next line. a printer would simply scroll the paper a bit.

So much for the history lesson. Current windows systems still use these characters to indicate a line ending. Dos generated this code when pressing enter.

The key code is a bit different. Beginning with the esc key being the 1. Enter is 28.

Source: linux hlkeycodes from www.comptechdoc.org



Related Topics



Leave a reply



Submit