'screen /dev/ttyUSB0' with different options such as databit, parity, etc - serial-port

I am trying to use
screen /dev/ttyUSB0
to connect to a old computer(s) through a USB-serial interface.
I have not figured out to put the correct options in my command line to get a non-gibberish feedback from my computer (the text received is all screwed up).
My operating system is CentOS, with GNOME 2.16.0.
I see that there is a program called KPPP which has a "Terminal...", but I haven't figured that one out either. So I am trying to use CLI with 'screen', but I am having trouble setting the correct parameters (obviously, I do not understand how to put these parameters to use with stty). It is not an option installing applications or doing anything with this computer, so I have to use what's already there. 'screen' seems to do the job, but the text received is gibberish as mentioned earlier ("$$#%idj ldj", etc.)
I need these parameters for computer one:
Baud: 9600 Databit: 8 Parity: No Stopbit: 2 Flow control: Hardware.
For computer two I need:
Baud: 9600 Databit: 7 Parity: Even Stopbit: 1 Flow control: Hardware
The baud rate is easy;
screen /dev/ttyUSB0 9600
But what to do with the rest, I do not know. I have found the option for stop bits:
cstopb (use two stop bits)
-cstopb (use one stop bits)
But how do I use it correctly?
screen /dev/ttyUSB0 9600 -cstopb
or
screen /dev/ttyUSB0 9600,-cstopb
How can I connect to the other computer through serial interface with all of the listed parameters?
I have found this manual for stty.
Is databit the same as this option?
cs5 cs6 cs7 cs8
Select character size (see termio(M)).
Parity:
parodd (-parodd)
Select odd (even) parity.
Stopbit:
cstopb (-cstopb)
Use two (one) stop bits per character.
But what about hardware control?
Anyway; this is still not working;
screen /dev/ttyUSB0 9600 cs8 oddp cstop
or
screen /dev/ttyUSB0 9600 cs7 evenp -cstop

I don't think Screen has support for all these different serial port settings. Only the most basic parameters are supported.
You're already in the correct direction by looking at the stty manual, but you have to use stty as a separate tool from Screen:
First you configure your serial port, and then you connect to it using Screen.
To configure your serial port for computer 1:
# stty - change and print terminal line settings
#
# -F /dev/ttyUSB0 Change the settings of /dev/ttyUSB0
# cs8 Use 8 character bits
# -parenb Don't use a parity bit (the '-' means 'disable')
# crtscts Enable RTS/CTS handshaking (hardware flow control)
stty -F /dev/ttyUSB0 cs8 -parenb cstopb crtscts
After you've configured your port, you can start using it trough screen:
# screen - screen manager with VT100/ANSI terminal emulation
#
# /dev/ttyUSB0 Use /dev/ttyUSB0 as terminal
# 9600 Open the serial port using 9600 baud
screen /dev/ttyUSB0 9600
The same applies for your second computer:
# stty - change and print terminal line settings
#
# -F /dev/ttyUSB0 Change the settings of /dev/ttyUSB0
# cs7 Use 7 character bits
# parenb Enable the a parity bit
# -parodd Don't use ODD, but use EVEN parity
# -cstopb Don't use 2 stopbits, but just the regular 1
# crtscts Enable RTS/CTS handshaking (hardware flow control)
stty -F /dev/ttyUSB0 cs7 parenb -parodd -cstopb crtscts
Then you can launch Screen at 9600 baud:
# screen - screen manager with VT100/ANSI terminal emulation
#
# /dev/ttyUSB0 Use /dev/ttyUSB0 as terminal
# 9600 Open the serial port using 9600 baud
screen /dev/ttyUSB0 9600
This should do the trick. You can find much more configuration options in the help of stty:
stty --help

Commas between options are required!
To enable RTS/CTS flow control, use the following:
screen /dev/ttyS0 9600,crtscts
Note: Not all USB-to-RS-232 converters implement the hardware flow control!

Read Linux / UNIX Minicom Serial Communication Program for detailed instructions and use about Minicom.
Minicom is similar to GTKTerm and is the industry standard for serial port communication.

Related

How can I force an Arduino Leonardo to reset with AVRDUDE?

I want to compile and transfer an Arduino program by myself on a Leonardo board.
Everything works great with the Arduino official IDE. I have enabled verbose mode for compiling and bytecode transfer.
I can see each command line.
I want to understand each line.
Everything is good except last step: transfer with AVRDUDE. If I type exactly the same command, I get an error:
.avrdude: butterfly_recv(): programmer is not responding
This error is not present if I upload code with the Arduino IDE.
I can see a difference - the Arduino IDE displays this line before the AVRDUDE call:
Forcing reset using 1200 bps open/close on port /dev/cu.usbmodem1431
How can I make this reset by command line?
I had the same issue on macOS, and I came up with the following Bash script:
# Find the Arduino port
ARDUINO_UPLOAD_PORT="$(find /dev/cu.usbmodem* | head -n 1)"
# Reset the Arduino
stty -f "${ARDUINO_UPLOAD_PORT}" 1200
# Wait for it...
while :; do
sleep 0.5
[ -c "${ARDUINO_UPLOAD_PORT}" ] && break
done
# ...upload!
avrdude "${OPTIONS[#]}"
The while loop is the trick! It's going to proceed as soon as the Arduino port is back online.
This is part of a Makefile I wrote for the project Sesame.
For uploading from Windows, I made a bat file wrapper for AVRDUDE.
It identifies the Leonardo COM port with WMI, resets this COM port to 1200 baud with the MODE command, identifies the bootloader COM port and invokes AVRDUDE.
The firmware is supposed to be placed in firmware.hex, but it can be changed to be supplied from the command line.
Code is in a GitHub repository, Arduino Leonardo Uploader
Or below:
#echo off
setlocal
for /f "tokens=1* delims==" %%I in ('wmic path win32_pnpentity get caption /format:list ^| find "SparkFun Pro Micro"') do (
call :resetCOM "%%~J"
)
:continue
:: wmic /format:list strips trailing spaces (at least for path win32_pnpentity)
for /f "tokens=1* delims==" %%I in ('wmic path win32_pnpentity get caption /format:list ^| find "Arduino Leonardo bootloader"') do (
call :setCOM "%%~J"
)
:: end main batch
goto :EOF
:resetCOM <WMIC_output_line>
:: sets _COM#=line
setlocal
set "str=%~1"
set "num=%str:*(COM=%"
set "num=%num:)=%"
set port=COM%num%
echo %port%
mode %port%: BAUD=1200 parity=N data=8 stop=1
goto :continue
:setCOM <WMIC_output_line>
:: sets _COM#=line
setlocal
set "str=%~1"
set "num=%str:*(COM=%"
set "num=%num:)=%"
set port=COM%num%
echo %port%
goto :flash
:flash
avrdude -v -C./avrdude.conf -patmega32u4 -cavr109 -P%port% -b57600 -D -V -Uflash:w:./firmware.hex:i
I had the same problem. I've tried opening and closing the ACM0 port with a Python script at 1200 baud, as someone already mentioned. It didn't work for me. Then I have received half-advice to try toggling RTS/DTS and that will make autoreset. So in the end I found the solution (at least for me) on Linux Mint 18.2 (Sonya):
#! /usr/bin/python
import sys
import serial
com = serial.Serial(sys.argv[1], 1200)
com.dtr=False
com.close()
python ./reset.py "/dev/ttyACM0"
dmesg shows me:
[21850.047120] cdc_acm 1-1:1.0: ttyACM0: USB ACM device
[22093.700327] usb 1-1: USB disconnect, device number 53
[22094.034133] usb 1-1: new full-speed USB device number 54 using xhci_hcd
[22094.175377] usb 1-1: New USB device found, idVendor=2341, idProduct=0036
[22094.175381] usb 1-1: New USB device strings: Mfr=2, Product=1, SerialNumber=0
[22094.175384] usb 1-1: Product: Arduino Leonardo
[22094.175387] usb 1-1: Manufacturer: Arduino LLC
[22094.175964] cdc_acm 1-1:1.0: ttyACM0: USB ACM device
On Windows, in a command prompt, the same solution, slightly different batch file:
It determines the bootloader COM port as well. Note that just only the Leonardo to be flashed should be connected!
#echo off
echo Upgrade procedure starting.
if %1.==. goto error
set hexfile=%1
set comportA=NONE
set comportB=NONE
if not exist %hexfile% goto error
for /f "usebackq" %%B in (`wmic path Win32_SerialPort Where "Caption LIKE '%%Leonardo%%'" Get DeviceID ^| FIND "COM"`) do set comportA=%%B
if %comportA%==NONE goto nodevice
echo COM port for Arduino device is detected as %comportA%.
echo Reset Arduino into bootloader
mode %comportA%: baud=12 > nul
timeout 2 > nul
for /f "usebackq" %%B in (`wmic path Win32_SerialPort Where "Caption LIKE '%%Leonardo%%'" Get DeviceID ^| FIND "COM"`) do set comportB=%%B
if %comportB%==NONE goto nobldevice
echo COM port for Arduino bootloader device is detected as %comportB%.
echo.
echo Starting AVR Downloader/UploaDEr.....
avrdude -pm32u4 -cavr109 -D -P%comportB% -b57600 -Uflash:w:%hexfile%
goto upgradedone
:nodevice
echo No matching module found, you should connect the module you want to upgrade.
goto end
:nobldevice
echo Reset into bootloader failed, please try again...
goto end
:error
Echo Missing parameter or file, you should provide the full filename of an existing .hex file you want to use.
goto end
:upgradedone
echo.
echo Upgrade done!
:end
I use this for Nano-every:
I think the essential elements are:
configure appropriate com port at 1200 baud with DTR = ON
Turn DTR off.
Flash device with correct version of avrdude and avrdude.config. My version came with the installation of my Nano-every device.
For other boards, it might be different. Lines 1, 2 reset the port and I think this is independent of the board, but change the port number!
c:\windows\system32\mode.com com6: baud=1200 dtr=on
c:\windows\system32\mode.com com6: dtr=off
C:\Users\Ludo\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude -CC:\Users\Ludo\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf -v -patmega4809 -cjtag2updi -PCOM6 -b115200 -e -D -Uflash:w:C:\Users\Ludo\AppData\Local\Temp\arduino_build_385557/sketch_dec11a.ino.hex:i -Ufuse2:w:0x01:m -Ufuse5:w:0xC9:m -Ufuse8:w:0x00:m {upload.extra_files}
Well, you pretty much wrote the answer yourself.
You need to open a serial connection at 1200 baud to the Arduino and then close the connection. The Arduino will then boot into SAM-BA, and reset itself, and is now ready for a new program.
I had the same problem with arduino leonardo.
I want to share my solution for people wanting to solve this problem using Qt.
Here is solution ready to be copy and paste, also taking care of timeout:
#include <QtCore/QString>
#include <QtCore/QDebug>
#include <QtCore/QThread>
#include <QtCore/QElapsedTimer>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
bool forceResetComPort(const QString& portName)
{
QSerialPort serial;
serial.setPortName(portName);
serial.setBaudRate(1200);
qDebug() << "Forcing reset using 1200bps open/close on port ") << portName;
if(!serial.open(QIODevice::ReadWrite))
return false;
// This seems optional
serial.setDataTerminalReady(false);
qDebug() << "Waiting for the new upload port...";
QElapsedTimer timeoutWatcher;
qint64 timeoutMs = 10000;
bool isPortPresent = true;
const auto fetchIsPortPresent = [&]() -> bool
{
const auto ports = QSerialPortInfo::availablePorts();
for(const auto& info : ports)
{
if(info.portName() == serial.portName())
return true;
}
return false;
};
timeoutWatcher.start();
// Wait for port to disconnect
while(isPortPresent && !timeoutWatcher.hasExpired(timeoutMs))
{
isPortPresent = fetchIsPortPresent();
if(isPortPresent)
QThread::msleep(1);
}
serial.close();
// Wait for port to reconnect
while(!isPortPresent && !timeoutWatcher.hasExpired(timeoutMs))
{
isPortPresent = fetchIsPortPresent();
if(!isPortPresent)
QThread::msleep(1);
}
return !timeoutWatcher.hasExpired(timeoutMs);
}
It is all about timing. I really needed to wait for the serial port to disconnect to close serial.
Closing too fast didn't have any effect.
Closing too slow, the usb serial was reconnecting but with a different port name. (For example /dev/ttyACM0 to /dev/ttyACM1).
It's important to wait for the port to reconnect in my case because I call avrdude in QProcess after.
Here is dmesg:
[ 8566.623621] cdc_acm 1-8:1.0: failed to set dtr/rts
[ 8566.979697] usb 1-8: new full-speed USB device number 21 using xhci_hcd
[ 8567.133193] usb 1-8: New USB device found, idVendor=2341, idProduct=0036, bcdDevice= 0.01
[ 8567.133197] usb 1-8: New USB device strings: Mfr=2, Product=1, SerialNumber=0
[ 8567.133199] usb 1-8: Product: Arduino Leonardo
[ 8567.133200] usb 1-8: Manufacturer: Arduino LLC
[ 8567.134820] cdc_acm 1-8:1.0: ttyACM0: USB ACM device

How to send boot files over uart

I have a beaglebone black board. A host with 64 bit ubuntu14.04
I wanted to transfer uImage file over uart to beaglebone.
So I stopped at u-boot and type
U-Boot# loadb
## Ready for binary (kermit) download to 0x80200000 at 115200 bps...
Now it is waiting for the file. What I have to do in order to send the uImage from pc to board.
After the loadb command prints "Ready for binary download", exit from the terminal(minicom, putty etc). Note down the serial device (eg: /dev/ttyUSB0). Install kermit or its variants (eg: gkermit and ckermit are available in Ubuntu).
Assuming that /dev/ttyUSB0 is your serial device, baudrate is 115200, and no flow control is used, provide the following parameters to kermit
$kermit
kermit> set port /dev/ttyUSB0
kermit> set speed 115200
kermit> set carrier-watch off
kermit> set flow-control none
Now issue the command send , to send the file over serial line:
kermit> send filename
After the file transfer is successful, exit from kermit (use exit command), and re-open minicom. Now you can issue further commands.
NOTE : You can explicitly specify a load address to loadb. If not specified, U-boot takes load address from environment variables.
NOTE-2 : Some terminal programs have built-in facility to send files over serial line using protocols like xmodem or kermit.

Unable to program NodeMCU

This question is related to this, Cannot program ESP8266, but different settings.
I have a NodeMCU devkit v1, its comercial name is NodeMCU v2. I tried different setting to connect the devkit.
First the devkit has a usb-serial bridge (CP2102) and voltage adapter from 5v (USB) to 3.3v. So I connect the devkit with the machine via USB. The driver is installed correctly, and I can see the com, it is COM3.
After turning on the NodeMCU I can see a Wifi AI-THINKER-adfe21.
Then I tried with the nodemcu-flasher - WINDOWS.
Without any button or pin to ground. I press Flash in the flasher and stay Waiting MAC and the log says Begin Find ESP8266 as the question I told in the begin and the ESP's led (blue led) blinks.
Holding FLASH, press RESET, release FLASH. Still the same like 1.
Wire D3 (GPIO0) to GND. Same result
With ESPtool - LINUX - Trying these commands
python2 esptool.py --port /dev/ttyUSB0 read_mac
python2 esptool.py --port /dev/ttyUSB0 write_flash 0x00000 firmware.bin
Without any button or pin to ground. The output for the commands was Connecting... A fatal error occurred: Failed to connect to ESP8266
Holding FLASH, press RESET, release FLASH. Same
Wire D3 (GPIO0) to GND. Same result.
Via Arduino IDE, I installed the ESP8266 board and selected NodeMCU 1.0. The result to try upload some code -> warning: espcomm_sync failed error: espcomm_open failed
I tryed to connect the NodeMCU without the USB. Using an Arduino UNO, connecting like following... And I'm getting the same errors.
UNO | NodeMCU
5v -> Vin
GND -> GND
Rx -> Tx
Tx -> Rx
The last test I made, it is with Putty or Arduino SerialMonitor connect to the COM3 or /dev/ttyUSB0 and turning on the NodeMCU. I should see some characters or garbage if it in different baud rate. But I got nothing, the terminal is blank. I restart the NodeMCU, put it in Flash mode. No response.
Are there some solution, tip or trick to make it programmable?
PS: I know the NodeMCU is in Flash mode because its wifi disappear.
EDIT:
I discovered something.
I just connected the NodeMcu with a mobile charger and connect the D0 to Serial ground and D3 to Serial Rx. On the putty with 115200 Baudrate, I'm able to get the following code on reset.
node : sta(mac address) + softAP(mac address)
add if0
add if1
dhcp server start:(ip:192.168.4.1, mask:255.255.255.0,gw:192.168.4.1)
bcn 100
I have been working with both the node-mcu flasher and the esptool.
First in the esptool yo should use more parameter in the call. Use something like this
python esptool.py -p SERIAL_PORT_NAME --baud 9600 write_flash --flash_size=8m 0 firmware-combined.bin
Be careful with the baudrate, i always use 9600.
If you prefer using the windows program you should only connect the ESP8266 through usb and dont connect any other pin. If it still gives you problems you should check that u have installed the com ports.
Good Luck
if you are using esptool (either in Linux or windows), you should include -fm and -fs inside the esptool command. There will be no response if you did not include those 2 parameter even though it show successfully upload. you can refer the detail in this youtube tutorial or its description for flashing firmware in nodemcu V2 or v1.0.
esptool.py --port [serial-port-of-ESP8266] write_flash -fm [mode] -fs [size] 0x00000 [nodemcu-firmware].bin
Tutorial on how to flash firmware using esptool(windows):
https://www.youtube.com/watch?v=MHrm7axsImI
cheers!
Maybe you can try this setting.
$sudo esptool.py -p /dev/ttyUSB0 --baud 115200 write_flash -fs 16m -fm qio -ff 0x00000 firmware.bin

How to get the ESP8266 to work in update mode?

I have been trying to get my WiFi module, ESP8266 to work in the "update mode" where you connect GPIO_0 to ground.
The board is functional when GPIO_0 is disconnected from ground and I am able to use AT commands to talk to it via the Arduino serial monitor.
Following is what it prints upon connecting:
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x40100000, len 1396, room 16
tail 4
chksum 0x89
load 0x3ffe8000, len 776, room 4
tail 4
chksum 0xe8
load 0x3ffe8308, len 540, room 4
tail 8
chksum 0xc0
csum 0xc0
2nd boot version : 1.4(b1)
SPI Speed : 40MHz
SPI Mode : DIO
SPI Flash Size & Map: 8Mbit(512KB+512KB)
jump to run user1 # 1000
rrlÌÿ
Ai-Thinker Technology Co.,Ltd.
ready
However, I cannot do most things as the firmware is out of date.
As a result, I am trying to update it (with no success)
When I set it to update mode and use the python tool "esptool.py", the script fails to connect to the ESP board.
Following is the command:
python esptool.py -p /dev/ttyUSB0 write_flash 0x00000 "/home/aditya/Desktop/v0.9.2.2 AT Firmware.bin"
Following is the output:
Connecting...
Traceback (most recent call last):
File "esptool.py", line 532, in <module>
esp.connect()
File "esptool.py", line 159, in connect
raise Exception('Failed to connect')
Exception: Failed to connect
I have even run the Windows counterpart of this tool "ESP Flasher" with no luck
The one who helps will be given a cookie (unless you value privacy)
This tool (esptool.py) works with a ROM bootloader, which is only started if GPIO_0 is tied to the GND during hardware reset. Bootloader also times out pretty fast, so just connecting GPIO_0 to the ground is not enough.
For me, following works:
Connect TXD/RXD/GND/VCC to the PC and CH_PD to VCC;
Make sure I can talk to the firmware via terminal (picocom/minicom/etc);
Connect GPIO_0 to the GND;
Connect RST to GND;
Release RST;
Run esptool.py
If still no dice, swap 5 and 6 above, i.e. first run esptool.py, then (as quickly as possible) release/disconnect RST. You will only have a second or two before esptool.py times out.
Try making the ground of all devices connected: if you have a level shifter inbetween, make sure the GND is tied to the GND of Arduino and also to the RS232 GND.

how to redirect the output of serial console (e.g. /dev/ttyS0) to a buffer or file

Is it possible to pipe serial console output to a file or a buffer or some virtual or pseudo device (in /dev)?
The Kernel command line has in startup at this point "console=null,115200".
(Normally it has "console=ttyS0,115200" - my requirement is: if "console=null,115200", should the output go to some other place than ttyS0, e.g. a virtual or pseudo device or to a file/buffer)
Maybe somebody know if there is good solution available?
Thanks a lot in advance!
There are two ways that I am aware of :-
First way :-
get ttylog from sourceforge :-
http://sourceforge.net/projects/ttylog/files/latest/download
Fire the below command:-
nohup ttylog -b 115200 -d /dev/ttyS0 > log.txt
this will then show you the PID of the process that is running, you now need to disown that PID so it doesn't get killed when you log out. Note that 115200 is the serial port speed/baud rate you configured grub for on the box you are monitoring.
Second way :-
Setup a serial console from system under test to some other linux/windows box. In case of linux install minicom and set minicom to listen on the serial port define in grub of system under test. Save that as dfl. You are good to go for more info :-
https://www.kernel.org/doc/Documentation/serial-console.txt

Resources