iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Serial interface - black magic?

Thu Sep 12, 2013 4:19 pm

Hello all,
I'm writing a script to read serial data from a medical scale on my RPi but encountered weird problems.

I'm using serial to ttl converter with MAX3232 chip and as far as i can tell it works as it is supposed to. When I connect RPi to serial port in my old laptop it receives all it is sent its way. Using the same code the laptop receives data from the scale without issue .
The problem is, when I connect RPi to the scale, it receives nothing. I'm able to monitor all passing data and it is sent everytime from the scale (on Rx), but on Pi's end it doesn't show.
I use two different cables (M/M - Pi + scale, M/F - Pi + laptop, laptop + scale) but i checked them and they are both straight without crossing Tx and Rx so this part should be fine, not to mention swapping Tx/Rx cables on pins multiple times just to be sure.. :)

In my mind it comes to weird converter behaviour or some Pi quirk I don't know about and it's maddening.
It works except when it doesn't.. So I'm hoping for some kindly insight and suggestions since I'm not expert on RS232 and I have no more ideas. I'll try using serial to usb converter next, but this goes around the issue.
Thanks..

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13223
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Serial interface - black magic?

Thu Sep 12, 2013 5:20 pm

Did you set the same baudrate on the PI that the scale is using?
Did you realize that in order to use the PI's serial port with your own serial software you need to turn off the kernal's use of the serial port, otherwise what you send to the PI will be received as if it were commands for the standard input of the command interpreter.

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Thu Sep 12, 2013 7:43 pm

Thanks for response.

yes, the baudrate is surely correct.. i'm using same code on the laptop and pi to test it.
and yes.. I edited both /boot/cmdline.txt and /etc/inittab as in various guides.

scale -> laptop - works
laptop -> Pi - works
scale -> Pi - doesnt work

Same code, same settings, in two cases it works and in the case I need it doesnt..
The worst thing is it should have been dead simple as plug it and code it.. Now I have almost done application and the most basic thing refuses to obey logic </rant>

I think I should mention I'm using Python and pyserial.

Thanks again

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Serial interface - black magic?

Thu Sep 12, 2013 7:52 pm

mahjongg wrote:Did you realize that in order to use the PI's serial port with your own serial software you need to turn off the kernal's use of the serial port, otherwise what you send to the PI will be received as if it were commands for the standard input of the command interpreter.
I recently wrote something to help you do exactly that :) http://www.raspberrypi.org/phpBB3/viewt ... 05#p418205

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Serial interface - black magic?

Thu Sep 12, 2013 7:57 pm

iond wrote:Same code, same settings, in two cases it works and in the case I need it doesnt..
The worst thing is it should have been dead simple as plug it and code it.. Now I have almost done application and the most basic thing refuses to obey logic </rant>

I think I should mention I'm using Python and pyserial.
Yeah, looks quite frustrating indeed! But serial comms are usually so simple I bet it's one of those things where you'll kick yourself when you discover what the problem is ;-)
Are you using just software flow control everywhere? Have you tried "debugging" using a terminal emulator such as picocom, instead of using pySerial?

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Thu Sep 12, 2013 8:09 pm

No flow control anywhere.. Scale doesnt support it.
No I didn't try terminal emulator, but since the same code works when the laptop is on the receiving end of data from the scale I don't think the software is at fault. I still hope it is something simple, but I can't simply explain that one case works and other almost identical doesn't.
Literally the only thing different is cable and end device. (and both cables are straight)

technoducky
Posts: 62
Joined: Sat Jan 26, 2013 7:29 pm
Contact: Website

Re: Serial interface - black magic?

Fri Sep 13, 2013 12:55 pm

iond wrote:No flow control anywhere.. Scale doesnt support it.
No I didn't try terminal emulator, but since the same code works when the laptop is on the receiving end of data from the scale I don't think the software is at fault
Never assume anything.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13223
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Serial interface - black magic?

Fri Sep 13, 2013 5:03 pm

If you have an oscilloscope, measure if there is a valid serial signal on the PI's RxD pin.

You are probably failing an oscilloscope, so a RED led with a 1K series resistor has to make a "poor mans substitute".

Connect the LEDs Cathode to GND, its anode to a 4K7 resistor, and use the other side as the resistor as a "probe". 4K7 will allow only a very small current through the LED, (but will not load the RxD signal too much) you can check by "probing" the 3V3 line, in dark you should just see the LED light up a bit.

If you probe the RxD pin, and the scale sends some data you should be able to see the LED flicker, if the baudrate used is reasonable low, like 2400Bps. But even much higher flickering can often be seen by the fact that the LED becomes dimmer when receiving data. Without data transfer the signal should be high (3V3) and should go low during transfers.

klricks
Posts: 7209
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Serial interface - black magic?

Sat Sep 14, 2013 3:51 am

Did you power the Max3232 with 3V3? It's very important that the Max3232 is powered by 3V3 and not 5V.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Sat Sep 14, 2013 7:57 am

Thanks for the tip.
I'll try measuring the signal levels as soon as I have the scale again.. I'll get back with the results.

fdufnews
Posts: 289
Joined: Fri Oct 07, 2011 5:37 pm

Re: Serial interface - black magic?

Sat Sep 14, 2013 12:37 pm

No GND missing between the equipments?

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Sat Sep 14, 2013 3:23 pm

OK.. so.. interesting thing.. when connected to laptop both Tx and Rx of Pi have expected 3.3 - 3.2V. But when connected to scale the Rx suddenly goes to 0V and Tx keeps 3.3V.. I guess that could mean something.. :)

+ another weird thing is the voltages on the serial cable
between scale and Pi the Rx is -3.8V and Tx 1.7V
between scale and laptop the Rx is -5.4V and Tx -5.5V

+between laptop and pi it's Rx -3.1V and Tx -4.35V

Edit: correction after double check

mistofeles
Posts: 62
Joined: Thu Sep 27, 2012 7:31 am

Re: Serial interface - black magic?

Mon Sep 16, 2013 9:10 pm

I get exactly the same problem.
I have checked and rechecked all and everything and I'm positive the problem lies somewhere in the way Raspi handles GPIO.

OKAY, Now I got some drops of information !
(I have worked with RS232 for 30 years on the low and high levels.)
Raspi is connected to our lan and I take two terminals there to test this.
I have a USB-RS232 device in Raspi connected to RS232 of GPIO of the same Raspi with a MAX3232 card.
All the parts are checked and rechecked. The system is Arch Linux.

1. I returned the original /boot/cmdline.txt and rebooted. There stands 115200 for speed.
2. I set the GPIO port speed by 'stty -F /dev/ttyAMA0 speed 115200 speed'
3. I set the USB-port speed by 'stty -F /dev/ttyUSB0 speed 115200 speed'
4. I open the terminals:
picocom /dev/ttyAMA0 -b 115200
picocom /dev/ttyUSB0 -b 115200
The connection works as long as I just write random characters on ttyUSB0 terminal.
When I hit Enter, the system vomits non-readable characters.
The speed of ttyAMA0 drops back to 38400 by itself !
What I think:
- As long as I only write characters, /dev/ttyAMA0 takes them as characters on a command line. Pressing Enter sends the line to the kernel which doesn't understand it. The character encoding has not worked and the kernel gets garbage. The kernel sends a response, which is again encoded wrong and I don't understand, what the kernel is saying.

NEXT TEST
I changed the speed in /boot/cmdline.txt to 38400 and rebooted.

TERMINAL 1:
root@raspi:~# stty -F /dev/ttyAMA0 speed
38400
root@raspi:~# picocom /dev/ttyAMA0 -b 38400

TERMINAL 2:
root@raspi:~# picocom /dev/ttyUSB0 -b 38400

Wait some 1-5 sec and hit enter on ttyUSB0.
You get a login prompt and you get in. So for now on ttyAMA0 works as a console port

I managed to get a result that the ports, connectors, cables, MAX3232 and PL2303X are working and the signal goes both ways.

There seems to be some mechanism, which wants to use speed 38400 and don't handle 115200 (nor any other speed). This mechanism or something else keeps the ttyAMA0 port for console.

Sorry to leave it now, but I have been without a sleep for too many hours. The battle will continue...
Hundreds of web sites tell you that you can change the port from console port to normal RS232 if you edit /boot/cmdline.txt and /etc/inittab.
- The problem is that in Arch Linux there is no /etc/inittab.
-----------------------------------------------------
35 years with computers and still here !

d_older
Posts: 126
Joined: Mon Jun 25, 2012 5:04 pm
Location: East Yorkshire, UK

Re: Serial interface - black magic?

Mon Sep 16, 2013 10:37 pm

Hi mistofeles,

looking at the Arch linux wiki it appears that it uses systemd and that serial consoles are controlled as a service see:-

https://wiki.archlinux.org/index.php/Wo ... stemd_only

It mentions that the default serial terminal is 38400 8N1 - the value you are getting

It appears that you may have to remove some links to stop the getty grabbing the serial port.

Sorry I can't be anymore help - I'm a debian user - but just learnt the beauty of "stty -F /dev/ttyAMA0 9600 raw" when working with a serial GPS. (after the commandline / inittab disabling)

Dave

mistofeles
Posts: 62
Joined: Thu Sep 27, 2012 7:31 am

SOLUTION: Serial interface - black magic?

Tue Sep 17, 2013 7:11 am

Thank you ! !
Your information of the link was the key !

SYSTEM: Linux version 3.6.11-10-ARCH+ (nobody@panda4) (gcc version 4.7.2 (GCC) )
DEVICE: RaspBerry Pi 256MB.

IF YOU WANT TO USE RS232 in GPIO
Change the name of a file:
mv /etc/systemd/system/getty.target.wants/getty@ttyAMA0.service /etc/systemd/system/getty.target.wants/Xgetty@ttyAMA0.serviceX

Reboot


After this the /dev/ttyAMA0 is not a console port. You can make it back to console by changing the name back.

You see that I used 'X' in the beginning and end of the name.
You can use some random name of yourself.
-----------------------------------------------------
35 years with computers and still here !

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Tue Sep 17, 2013 7:41 am

Hi.. glad you solved your problem, but just to reiterate and keep it on problem with which this thread starts..

measured on Pi/adapter
Pi to laptop both Tx and Rx of Pi have expected 3.3 - 3.2V
Pi to scale the Rx suddenly goes to 0V and Tx keeps 3.3V

measured directly on connected serial cable
between scale and Pi the Rx is -3.8V and Tx 1.7V !!!!!!!
between scale and laptop the Rx is -5.4V and Tx -5.5V
between laptop and pi it's Rx -3.1V and Tx -4.35V

The unexpected voltage seems it might be causing these issues, although I dont see why does it suddenly should have different voltage.

User avatar
joan
Posts: 14998
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Serial interface - black magic?

Tue Sep 17, 2013 8:20 am

I'd get a new meter and/or stop connecting such voltages to the Pi. You may have damaged the Pi if you input voltages outside the range 0-3.3V for any extended periods.

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Tue Sep 17, 2013 9:03 am

joan wrote:I'd get a new meter and/or stop connecting such voltages to the Pi. You may have damaged the Pi if you input voltages outside the range 0-3.3V for any extended periods.
Well.. I do nothing of the kind.. The serial to ttl adapter connected to Pi's GPIO converts it to 0-3.3V - as the first part of measurments clearly says..

User avatar
joan
Posts: 14998
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Serial interface - black magic?

Tue Sep 17, 2013 9:44 am

In that case I don't understand
measured directly on connected serial cable
between scale and Pi the Rx is -3.8V and Tx 1.7V !!!!!!!
between scale and laptop the Rx is -5.4V and Tx -5.5V
between laptop and pi it's Rx -3.1V and Tx -4.35V

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13223
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Serial interface - black magic?

Tue Sep 17, 2013 9:49 am

Sounds like you are trying to put RS232 levels into the PI, instead of (3V3) TTL levels!

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Tue Sep 17, 2013 12:00 pm

mahjongg wrote:Sounds like you are trying to put RS232 levels into the PI, instead of (3V3) TTL levels!
thats RS232 voltage levels on serial cable.. i got one cable with shaved off insulation to be able to measure it.
Voltages on Pi/MAX3232 adapter are 0 to 3.3V
Last edited by iond on Tue Sep 17, 2013 12:04 pm, edited 1 time in total.

iond
Posts: 12
Joined: Thu Sep 12, 2013 3:08 pm

Re: Serial interface - black magic?

Tue Sep 17, 2013 12:02 pm

joan wrote:In that case I don't understand
measured directly on connected serial cable
between scale and Pi the Rx is -3.8V and Tx 1.7V !!!!!!!
between scale and laptop the Rx is -5.4V and Tx -5.5V
between laptop and pi it's Rx -3.1V and Tx -4.35V
Its on serial cable.. not Pi/adapter which is after conversion to TTL levels and is correctly between 0 and 3.3V

mistofeles
Posts: 62
Joined: Thu Sep 27, 2012 7:31 am

Re: Serial interface - black magic?

Tue Sep 17, 2013 12:15 pm

First test:
What happends if you connect Rs to Tx in the GPIO connector of RPI ?
I mean, leave everything else out and use a DIP shortcut.
Use 'picocom /dev/ttyAMA0 -b 1200'
( Check that local echo if OFF )
Send some characters. They should come straight back to the terminal.
If they do, the problem is in the MAX3232 card or between the MAX and external device.
If something comes back, check my previous note about etty@ttyAMA0.service.
If nothing comes back, you have a real problem.

Another test:
connect the TTL Rx to TTL Tx in the MAX3232 card. You can use a short piece of copper here. Let the card use the 3.3V and GND of RPI. You must also connect the GND to the GND of the RS232 connector
Now connect the card to your laptop with your cables.
Use some terminal program in your laptop.
Send some characters. They should come straight back.
If something comes back, check Rx-Rx and Tx-Tx connections between MAX and GPIO.
If nothing comes back, check the terminal program settings and the cable.
If something comes back, check the cable.

You said "Pi to scale the Rx suddenly goes to 0V and Tx keeps 3.3V"
Add a 1kOhm resistor in series in this Rx line between GPIO and MAX card.
When the Rx goes to 0V, you can measure the voltage on both sides of the resistor against GND. Now you know which draws the line down, GPIO or MAX.
The same thing can be done with the Tx line.
The signals do not care about this 1kOhm, the inputs are so high impedance that it does not matter. In truth I'm using these resistors in cases, when I'm not sure.

In any case I'd recommend a simple logic analyser to check where the signal is going where. Just connect red and green LED side-by-side anode to cathode and cathode to anode and connect this in series with a 1 kOhm resistance. The other end you can solder to the GND with a cord.
You can use this to check both TTL and RS232 lines.

When you are testing RS232 with this 'analyzer', use as low speed as possible. For example
"picocom /dev/ttyAMA0 -b 110".
The slow speed makes it easier to see when the bits are running.

I'm using a D9 shortcut adapter to test RS232. Take a female D9 and connect the pins like this: 2-3, 6-7-8.
Those lines 6,7 and 8 are used for handshaking when you connect it to your laptop. This adapter works if the device has a XON-XOFF handshaking. If the device has DTS-DTR handshaking, you got to invent something else. In case of slow interaction the adapter should work still, because the handshaking is mostly there to tell that the buffer is full.

Some laboratory devices need similar shortcuts in their connectors. The device does not say or listen anything, if there is no handshaking. RTFM of the device
-----------------------------------------------------
35 years with computers and still here !

mistofeles
Posts: 62
Joined: Thu Sep 27, 2012 7:31 am

Re: Serial interface - black magic?

Tue Sep 17, 2013 8:34 pm

Possible explanation / solution.

Even though you said the scale don't use (software) flow control, there might be hardware handshaking.

You have told that you have checked that the cables are straight. Have you also checked the handshakes ?
I mean that you have to check all the connections between all the pins in the cables.
There must be some shortcuts in the F/M cable, because you told that Laptop and Raspi can talk to each other using this F/M cable. This is not possible with a cable with no handshake shortcuts between pins.
I think the F/M cable has just three wires: Rx, Tx, GND

Laptop <-> Scale, OK, M/F cable, Handshake shortcuts in the ends of the cable.
Laptop <-> Raspi, OK, M/F cable, Handshake shortcuts in the ends of the cable. Raspi don't need handshake lines. It relies on XON/XOFF handshake.
Raspi < -> Scale, NOT-OK, M/M cable, No handshake shortcuts in the ends of the cable. Scale waits for DTR and/or RTS High state, which never comes. After a certain time the scale gets bored and drops her signals.

Some "standard" says that in M/M cable the Rx/Tx lines are crossed and in F/M cable they are straight. This is because DCE is historically female and DTE is male (PC is DTE, modem is DCE) DTE-DTE needs a crossed cable.

Build handshake shortcuts in the scale end of the cable to draw DTR and RTS high. Here you need a pin, which is High. Possibilities are DTS=pin6, CTS=pin8..I would recommend DTS=pin6.
You need a 3 wire cable:
- connect pins 2,3 and 5 of the other end to 2,3 and 5 of the other end
- solder pins 4, 6 and 7 together in the scale end of the cable (you can make this in the other end too ;)

Pontificating
I have written a large page about the weirdos of RS232 but unfortunately it is in local language we use here in Finland:
http://users.utu.fi/ptmusta/rs232.shtml
All my knowledge about RS232 collected to one sentence:
- "Never trust to any cable somebody else has built. There is no such thing as a standard in the world of RS232"
Another piece of collected wisdom:
- " The only standard in computers is the continuing alteration."
Last edited by mistofeles on Tue Sep 17, 2013 9:32 pm, edited 2 times in total.
-----------------------------------------------------
35 years with computers and still here !

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5732
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Serial interface - black magic?

Tue Sep 17, 2013 9:05 pm

@mistofeles et al.
FWIW one of my oldest webpages is about "RS232 Issues". It was originally posted on my workplaces "intranet" (Unix based) a year or two before I had internet access and, eventually an external website. It can be found at http://www.cpmspectrepi.webspace.virgin ... /rs232.htm and, whilst virtually all of the examples for "common computers" are for ancient hardware of which a few items I still have in my "workshop" (and some still work after >20 years), the interconnection methods still apply (as discussed above). Fortunately when I (briefly) tried out a USB to RS232 adapter on the Pi I was able to make use of some of my ancient "test dongles" etc. (http://www.cpmspectrepi.webspace.virgin ... apter.html) :)
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Return to “Interfacing (DSI, CSI, I2C, etc.)”