0

I am working with a project where I need to read data from my device(i.e ADAM 4520)using serial to USB converter.
1st, I tested with my PC using hyper terminal and with Raspberry Pi 3 using GtkTerm, it is perfectly working.
As both hyper terminal and gtkterm using Ascii and my device ADAM-4520 also understand Ascii, But how to communicate through python code I am not finding away as I am new to python and raspberry pi, please guide me.

Here is my code:

import serial
import time
s=serial.Serial(port='/dev/ttyS0',
                baudrate=9600,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS,timeout=1)
st="#04"
st=''.join(str(ord(c)) for c in st)
x=st.encode('ascii')
while True:
   s.write(x)
   print(x)
   time.sleep(0.2)
   text=s.readline()
   temp=text.decode('ascii')
   #text=text.decode('utf-8')
      #text=text[5:-1]
   print(temp)

Here is my output:

354852
b''
354852
b''
354852

I am receiving nothing. whereas using gtkterm I am getting a response.

 #04
>+261.27+310.83+049.09+206.77+126.81+049.79

#042
>+049.11
M. Rostami
  • 4,323
  • 1
  • 17
  • 36
ram
  • 69
  • 9
  • The following two Q&As might help. Actually there are a couple more similar UART/serial answers I suggested in this forum: (1) https://raspberrypi.stackexchange.com/questions/96184/rpi3-to-arduino-serial-uart-communication-tutorial (2) https://raspberrypi.stackexchange.com/questions/105223/how-can-rpi4b-use-uart-to-read-and-analyze-received-data-at-a-3dr-fpv-air-radio – tlfong01 Jan 27 '20 at 06:00
  • 1
    This is a general programming question and not specific to the Pi. UARTs usually send byte values, which can be interpreted by sender/receiver as characters or numeric values. It is unclear what you are asking or why you are doing the convoluted operations rather than just sending characters (which - at least in Python3), are normally treated as UTF-8, of which ASCII is a simple subset. – Milliways Jan 27 '20 at 06:08
  • @Milliways,as i am clearly written through gtkterm i am able to communicate as my device responding to "#04" command but from python program it is not responding – ram Jan 27 '20 at 06:13
  • 1
    "#04" is a 3 character string. This is still NOT a Pi question. – Milliways Jan 27 '20 at 06:17
  • 1
    @Milliways,But this is a command to read ADAM-4520 device value and i am using raspberry Pi to communicate with ADAM-4520 and which successfully working using gtkterm which is an utility for Raspbian – ram Jan 27 '20 at 06:24
  • @ram, most of my old demo programs are using Rpi3B+ stretch and python 3.5.3. The following are more updated, on Rpi4B buster Thonny python 3.7.3 and playing with multiple on board and USB/UART channels: (1) https://raspberrypi.stackexchange.com/questions/107079/uart-on-raspberry-pi-4 (2) https://penzu.com/public/49c560cf. – tlfong01 Jan 27 '20 at 06:56
  • 1
    @tlfong01,after parallelly connecting hyper terminal and pi with device ,i observe if i am writing "#04 "command by using ser.write(str.encode("#04") then it is also showing in hyper terminal and if in that time i pressed enter through my hyper terminal ,then also my device response and result is coming on both terminal.So my question is (is it possible to add enter command with #04?) – ram Jan 27 '20 at 09:16
  • OK, let us for now forget about the ADAM 4520 and python program. Put them aside, and just use one terminal emulator on each side to check out if one side can send out a "control" character (ETX?), and the other side gets it? Please confirm if I understand your new question correctly. – tlfong01 Jan 27 '20 at 09:24
  • 1
    @tlfong01,Thanks for your valuable time ..i got the solution .it works with commandst="\x23\x30\x34\x0D" then st=st.encode() then s.write(st) then my device respond.Please do me a favor and please upvote the question..i new to python but here all are busy with down voting,which make me feel so bad – ram Jan 27 '20 at 09:41
  • Hi @ram, (1) How nice that you solved your problem. . (2) So I have upvoted your question. (3) You can now give your answer here, and I will upvote your question again. (4) I don't understand your comment ".. here all are busy with down voting, which make me feel so bad .." (5) I am a upvote lover and whenever I see any question or answer gets a downvote, I will immediately gives an upvote, to balance things out, for the harmony of the Universe, or at least our Galaxy. :) – tlfong01 Jan 27 '20 at 10:16
  • (5) Not too long ago another questioner complained that I should not have had down voted him. I told him that he made a wrong guess, and I showed him my up/down vote statistics that my down vote percent over all votes is less than 0.5%. Let me see if I can find and show you more of my old comments/stories to make you so not sad. Actually my down vote story is very long, so perhaps tell you later... – tlfong01 Jan 27 '20 at 10:21
  • I am suggesting you to give your own answer here, because (1) you can get at least one upvote from me, (2) Other newbies can learn from your experience why a serial device (eg, Arduino, flight controller, projector, etc etc.) because for many things, including every device is unique, and has a unique AT command set. The following Q&A is one of a couple of similar serial examples that I encouraged the OP to compile his own answer (he got stuck for a similar question as yours): – tlfong01 Jan 27 '20 at 10:35
  • 1
    his serial Sony/Panasonic projector AT command must start with two s): https://raspberrypi.stackexchange.com/questions/105405/rpi-zero-and-projectors-usb-rs232-serial-cable-interface-connection-problem. READ THE CHAT RECORD. Cheers. – tlfong01 Jan 27 '20 at 10:35
  • @tlfong01,Thanks for your valuable suggestion. – ram Jan 27 '20 at 10:59

1 Answers1

2

After changing the code it worked properly. So I updated here my code as it will definitely help a newbie. My device is ADAM-4520(485-232 converter), ADAM-4015(6-channel RTD module). I am interfacing these two devices with my Raspberry Pi 3 with serial (Rx/Tx)pin.

import serial
import time
import struct
s=serial.Serial(port='/dev/ttyS0',
                baudrate=9600,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS,
                timeout=1)
st="\x23\x30\x34\x0D"
x=st.encode()
while True:
   s.write(x)
   print("write: ",x)
   time.sleep(1)
   text=s.readline()
   temp=text.decode('utf-8')
   print("response: ",temp)
   time.sleep(2)

And here is my output.

write: b'#04\r'
Response: >+261.20+310.60+049.08+206.75+126.77+049.79

So it needs an enter command along with command "#04"(which is given in document of the device, but enter command needed it isn't given on documentation.

M. Rostami
  • 4,323
  • 1
  • 17
  • 36
ram
  • 69
  • 9
  • How nice to read a project on how to connect two Rpi's using the 485-232 converter. I know actually 485 can daisy chain many Rpi's using only two wires. Some time ago I bought a cheapy US$5/3 pieces RS232/RS845 converter and don't know how to start up: (1) AliExpress 5V RS485 to TTL232/RS232 UART module - US$5/pieces, to continue, ... – tlfong01 Jan 27 '20 at 13:09
  • https://fr.aliexpress.com/item/32782374521.html?spm=a2g0o.productlist.0.0.43636d51104Ope&algo_pvid=96bcaa4a-5673-4336-951d-a3fc7b78d572&algo_expid=96bcaa4a-5673-4336-951d-a3fc7b78d572-0&btsid=cafb26d9-3acb-4de6-8c61-8f834c16987f&ws_ab_test=searchweb0_0,searchweb201602_9,searchweb201603_53. Now I have read the user manual and found it not that difficult for Rpi hobbyists to play with: (2) ADAM4520 Isolated RS-232 to RS-422/485 Converter - https://advdownload.advantech.com/productfile/PIS/ADAM-4520/Product%20-%20Datasheet/ADAM-452020180910101957.pdf – tlfong01 Jan 27 '20 at 13:10
  • 1
    (3) ADAM-4520I Robust Isolated RS-232 to RS-422/485 Converter User Manual http://advdownload.advantech.com/productfile/Downloadfile5/1-20F7UM/ADAM-4510I_4520I_Startup_Ed1.pdf. – tlfong01 Jan 27 '20 at 13:10
  • Actually I found AliExpress/Tmall/TaoBao selling ¥33 Linux compatible USB RS232 to RS485 3V3/5V0 adapters. I have not used this before, but I guess it should be as easy as using the USB to TTL adapter: CP2102 3 in 1 5V/3.3V USB To RS485 RS232 TTL Serial Port ¥33 https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.126.2eda6185fGJlS3&id=573932242866&areaId=810200&user_id=3705195256&cat_id=2&is_b=1&rn=b953394ae0f271bf6dd3cdb3474030fa. – tlfong01 Jan 27 '20 at 13:22
  • Now I am thinking of investing ¥33 to upgrade myself from RS232 to RS485, so that my bad Arduino friends will no longer LOL at me and from now on pay more respect to me than I deserve. :) - https://imgur.com/gallery/jFokEJo – tlfong01 Jan 27 '20 at 13:34
  • I also need to wiki and memorize by heart those jargons I don't really quite understand, to over convenience my bad Arduino friends that I know more RS485 than they think. I need to "casually", comment from time to time about the superiority of RS485 over RS232 (Ah yes, RS485 is the real thing, RS232 is rubbish, can't do 10Mb/s, 1,000m, tristate logic, so can deactivate to make linear bus topology, blah, blah, blah, ...). RS-485 - Wikipedia https://en.wikipedia.org/wiki/RS-485. – tlfong01 Jan 27 '20 at 13:55
  • I found RS HK is selling the OP's RS232 to RS485 converter for HK$850, so I think it not worth for a hobbyist like me. So I think I will go for the poor man's ¥33 TaoBao version. Before I place my order, I need to google more to see if it is that hard to do RS485. I cascully googled and found the following article good for newbies to start with: (1) RS485 Programming using Python and USB to RS485 Converter - Rahul Sreedharan 2015apr03 https://www.xanthium.in/cross-platform-rs485-programming-using-python-usb-to-rs485-converter-and-pc. The following rpi.org forum ddiscussion are also usefu: – tlfong01 Jan 27 '20 at 15:01
  • 1
    RS-485 communication Python - rpi.org forum discussions: (2) https://www.raspberrypi.org/forums/viewtopic.php?t=204305 (3) https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=234122 (4) https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=175437 (5) https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=158737 – tlfong01 Jan 27 '20 at 15:03