LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   reading/writing to serial port doesn't work (https://www.linuxquestions.org/questions/linux-hardware-18/reading-writing-to-serial-port-doesnt-work-4175634073/)

Irbis 07-15-2018 06:17 AM

reading/writing to serial port doesn't work
 
Hi. I use Ubuntu 16.04. In the first terminal I wait for data:
Code:

cat </dev/ttyS0
Then in the second terminal I try to send data to the serial port:
Code:

echo -e '123' > /dev/ttyS0
but data is not visible in the first terminal. I have added myself to the group dialout for /dev/ttyS0.

Additional info:
dmesg | grep ttyS
Code:

[    0.684920] 00:06: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
stty -F /dev/ttyS0 -a
Code:

speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O;
min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc

ifconfig -a
Code:

enp0s25  Link encap:Ethernet  HWaddr d0:50:99:5e:47:a7 
          inet addr:192.168.0.164  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: 2a02:a31a:a043:6d80:8f91:68c2:9a1d:1b86/64 Scope:Global
          inet6 addr: 2a02:a31a:a043:6d80:81a4:6233:ff1f:6b9a/64 Scope:Global
          inet6 addr: fe80::1942:8c1c:9235:bbe7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9231 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6295 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:9029350 (9.0 MB)  TX bytes:809689 (809.6 KB)
          Interrupt:20 Memory:f7300000-f7320000

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:966 errors:0 dropped:0 overruns:0 frame:0
          TX packets:966 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:90014 (90.0 KB)  TX bytes:90014 (90.0 KB)

Why reading/writing to the serial port doesn't work ?

michaelk 07-15-2018 06:41 AM

Welcome to LinuxQuestions.

You need a physical loopback plug attached to the serial port connector to see data. Here is a pin out of the plug wiring.

http://www.bb-elec.com/Learning-Cent...-DB9-DB25.aspx

It is also easier to use a terminal program like minicom to test the serial port. With a loopback plug the characters are displayed as soon as they are typed on the keyboard.

Irbis 07-15-2018 09:32 AM

Is it possible to buy such connector ? My PC has a female com port so I need a male connector.

michaelk 07-15-2018 09:50 AM

Since the IBM standard uses a male connector I think it would be difficult to find a male loopback to purchase. You probably need to purchase a female connect and a gender changer.

It depends on what you are actually trying to accomplish. If you disable hardware control you can jumper pins 2-3 to see characters as they are typed.

Irbis 07-15-2018 12:20 PM

Gender changer is easy to get, I have also found the following connector. Is it what I need ?

What do you mean by "disable hardware control"? Could you explain it ?

pan64 07-15-2018 12:50 PM

probably this helps: http://tldp.org/HOWTO/Serial-HOWTO-9.html
I think that is the right connector, but you can find an interesting comment there (probably a damaged one only)

michaelk 07-15-2018 01:09 PM

Yes, however one of the comments shows this particular model using a strange wiring layout out which may be ok.

The RS-232 uses several wires to control data flow over the data pins. Since suggested just using a jumper to connect the RX to TX pins you need to disable hardware flow control since those pins are unused and you will be able to transmit data.

https://en.wikipedia.org/wiki/RS-232

Irbis 07-19-2018 03:42 PM

I have performed another test: I have connected to my pc usb to serial adapter which is connected with mentioned above loopback connector. Instead of ttyS0 I use ttyUSB0. It works but when I send some message:
Code:

echo -e '123' > /dev/ttyUSB0
on reading terminal
Code:

cat </dev/ttyUSBO
message is displayed multiple times and it looks like I get white spaces in a loop (infinitely). What is going on ?

michaelk 07-19-2018 03:48 PM

Yes it should work in theory.

How are you reading/writing to the serial port. Have you given permission to your regular user to read/write to the USB adapter which is typically /dev/ttyUSB0?

Irbis 07-19-2018 03:53 PM

Please see my answer update. I have forgotten to use ttyUSB0 instead of ttyS0

michaelk 07-19-2018 03:59 PM

using echo and cat in the terminal is not very reliable for testing serial ports in my opinion. As suggested earlier use minicom. You need to configure the serial port but it will show characters as they are typed.

Irbis 07-19-2018 04:26 PM

minicom with echo works fine.


All times are GMT -5. The time now is 05:29 AM.