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
14
  • 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 6:08
  • 1
    "#04" is a 3 character string. This is still NOT a Pi question.
    – Milliways
    Jan 27 '20 at 6: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 6:24
  • 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 9:16
  • 1
    @tlfong01,Thanks for your valuable time ..i got the solution .it works with command` st="\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 9:41
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.

8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.