Uncategorized

RS232 to RS485 Converter programming notes

Asked 
Active today
Viewed 26 times
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 way 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. where as using gtkterm i am getting response.

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

#042
>+049.11
  • The following two Q&As might help. Actually there are a couple more similar UART/serial answers I suggested in this forum: (1) raspberrypi.stackexchange.com/questions/96184/… (2) raspberrypi.stackexchange.com/questions/105223/… – tlfong01 7 hours ago
  • 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 7 hours ago
  • @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 6 hours ago
  • 1
    “#04” is a 3 character string. This is still NOT a Pi question. – Milliways 6 hours ago
  • 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 6 hours ago
  • @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) raspberrypi.stackexchange.com/questions/107079/… (2) penzu.com/public/49c560cf. – tlfong01 6 hours ago
  • 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 3 hours ago
  • 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 3 hours ago
  • 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 3 hours ago
  • 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 2 hours ago
  • (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 2 hours ago
  • 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 2 hours ago
  • his serial Sony/Panasonic projector AT command must start with two <CR>s): raspberrypi.stackexchange.com/questions/105405/…. READ THE CHAT RECORD. Cheers. – tlfong01 2 hours ago
  • @tlfong01,Thanks for your valuable suggestion. – ram 2 hours ago
Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?
1

After changing the code it worked properly. So i updated here my code as it will definitely help newbie. My device is ADAM-4520(485-232 converter),ADAM-4015(6-channel RTD module). I am interfacing this two device 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 need a enter command along with command “#04″(which is given in document of device ,but enter command needed it isn’t given on documentation.

.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: