How to send data to a serial port and see any answer?
On Linux, I want to send a command string (i.e. some data) to a serial port (containing control characters), and listen to the response (which also usually might contain control characters). How can I do this as simplest as possible on Linux? An example is appreciated!
5 Answers 5
All devices on Unix are mapped to a device file, the serial ports would be /dev/ttyS0 /dev/ttyS1 . .
First have a look at the permissions on that file, lets assume you are using /dev/ttyS1 .
You will want read.write access, if this is a shared system then you should consider the security consequences of opening it up for everyone.
A very simple crude method to write to the file, would use the simple echo command.
You can have cat running in one terminal, and echo in a 2nd.
If everything is gibberish, then baud rate, bit settings might need setting before you start sending. stty will do that. !! NOTE stty will use stdin as default file descriptor to affect.
This might be enough for you to script something and log ? Not sure what you are trying to achieve.
For a more interactive, remembers your default settings approach would be to use minicom it is just a program which does everything I’ve mentioned so far. (similar to hyperterminal in Windows, you might be familiar).
An intermediate solution, would use a terminal program like screen which will work on a serial device.
man screen man minicom man stty for more information
Send printk to serial
I am trying to debug a module for the Linux kernel. I heard that it is possible to send the console output to a serial port. I’m running Ubuntu on vmware and want to send printk message to the host. I have managed to set up a serial Connection and can send an echo to the host by typing echo > simething /dev/ttyS1 But I can’t figure out how to send the output on the console to ttyS1. My main problem is that when the module/kernel crashes the last printk-messages are lost and not even displayed, it just buffers.
2 Answers 2
On Guest Linux Kernel
sudo vim /etc/default/grub GRUB_CMDLINE_LINUX="console=ttyS1,115200n8 console=tty0 ignore_loglevel" sudo update-grub
NOTE:- kernel parameter «ignore_loglevel» will print all kernel messages to the console. Useful for debugging.
Now Enable getty On Console ttyS1
For Upstart System
1) Create a file called /etc/init/ttyS1.conf containing the following:
# ttyS0 - getty # # This service maintains a getty on ttyS1 from the point the system is # started until it is shut down again. start on stopped rc RUNLEVEL=[12345] stop on runlevel [!12345] respawn exec /sbin/getty -L 115200 ttyS1 vt102
2) Ask upstart to start the getty
For Systemd Systems
$ sudo systemctl enable serial-getty@ttyS1.service $ sudo systemctl start serial-getty@ttyS1.service $ sudo systemctl daemon-reload
how to write characters to serial port
I have an ancient serial spectrometer which only runs on win9x. I want to convert to a Linux system, but the existing software is proprietary and wont work. I was able to access a console internally on the spectrometer. The console takes keyboard character commands, and sends the output to the serial port. in vb6 I can write to the com port with char values http://www.gtwiki.org/mwiki/?title=VB_Chr_Values using ComPort.Write(Chr(34)) I am interested in using Qt for the interface, how can I send something to the same effect as ComPort.Write(Chr(34)) using qt?
1 Answer 1
- Here an example from Qt official site (C++) http://qt-project.org/wiki/QtSerialPort
- If Visual Basic is your background you may prefer Gambas3 http://gambasdoc.org/help/comp/gb.net/serialport?v3 Gambas have similar syntax of Visual Basic(VB), and support Qt as GUI tool kit. See http://gambasdoc.org/help/comp?v3
- In Linux/BSD, Serial port is more accessible then windows. So you can even write to it from shell/terminal, or use system call from most programming languages. Example in shell with an Android phone as modem, it may help for debugging:
- Reading serial port (need to be root):
sudo su echo -e "AT" > /dev/ttyACM0
It shows OK on reading port window, Also you can sent hexadecimal data (use -n option to avoid sending new line at the end)
echo -e -n "\x41\x54\x0a" > /dev/ttyACM0
echo -e "\x41\x54" > /dev/ttyACM0
How to pass display output to serial port under Linux?
We have 2 computer. Fist computer’s operating system is Linux( any version( not important for me) ) and another operating system is Windows( any version, i don’t care). They are connected via 9 pin serial port. I want to know how can I change Linux’s default display output to serial port instead of it’s own monitor. I want to view all Linux outputs( from first word of boot time ) via hyper terminal in Windows XP. BTW it’s just a research for future uses ! I know there is no easy solution for doing this. Maybe I should compile Linux again ! I don’t know. Please give me some suggestions.
What do you mean «no, it’s not!»? This doesn’t seem programming related to me. It’s about how to configure your operating system and its consoles / serial ports.
I need to change ( or even recompile ) Linux kernel code with new parameters and/or new variables. It’s not superuser related question! I believe that i should edit Linux kernel which is a programming related question I think!
If you having difficult with the in programming your linux kernel modifications then that’s a completely different — but programming related — question. Your current question is about how to configure you linux system to put the primary console on a serial port. As that’s purely a configuration question I still don’t see how it’s programming related.
3 Answers 3
It’s certainly feasible. The easiest way to do this is just to put a «getty» process on the serial terminal, which is sometimes done by editing /etc/inittab (there will be some examples in there) and then you will see a login prompt via the serial terminal and use all (command-line) programs normally. Historically, Unix machines did this as their primary way of working.
Another option is to use the «serial console», which involves passing some boot-time parameters to the kernel; this is only required if you want to see its startup messages on the serial port.
Edit: On newer Linux distributions, it’s not so easy to put «getty» on a serial line, presumably because it’s such an old-fashioned thing to do. Outside of embedded devices, pretty much nobody uses serial ports to log in to the system any more (most modern PCs don’t even HAVE rs232 ports). For example, Fedora and Ubuntu do not use «init», but something else which doesn’t have inherent support for running «getty». There is probably a way of doing it but I don’t know what it is; consult your distributuon’s documentation.