Python Forum
Raspberry PI writing to RS485 driver, DIR pin not holding state
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raspberry PI writing to RS485 driver, DIR pin not holding state
#1
Photo 
Hello. I am writing data to my modbus DRIVER and learning about modbus in general. I am using logic analyzer to check my signals.

I have noticed a very strange behaviour of my DIR pin. Before I send data to RS485 driver, I toggle DIR pin HIGH and keep it HIGH until the data transfer is finished.
The code:
# \x02 - trigger scanning
# \x03 after scanning is done
import serial
import RPi.GPIO as GPIO
import time
sendSerial = serial.Serial ("/dev/serial0", 9600)
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(16, GPIO.OUT) # Set pin 10 to be an input pie to be pulled low (off)

GPIO.output(16,1)
sendSerial.write (str.encode("hello"))
GPIO.output(16,0)

print("done")


sendSerial.close()
And the signal analzyer shows:
https://ibb.co/xsJR28b

As you can see from the image above, the DIR pin does not want for the serial write to finish

I have tried to add :
sendSerial.flush() after the writing but it looks even more strange:
https://ibb.co/qy0CDXv

Now its holding DIR pin for way too long.

Can someone help me understand this strange behaviour and how to fix it?
Reply
#2
Serial I/O is buffered. You usually don't want to wait for the write to complete. I was looking at the PySerial documentation and it looks like you could loop until out_waiting is zero if waiting until done is important.
Reply
#3
(Oct-29-2020, 06:46 PM)deanhystad Wrote: Serial I/O is buffered. You usually don't want to wait for the write to complete. I was looking at the PySerial documentation and it looks like you could loop until out_waiting is zero if waiting until done is important.

Its not that I want to wait. The rs485 driver requires me to hold the DIR pin high untill all bytes are transmittet.But as you can see from the examples of code I have posted above , the program wont let me do it. How can i build a proper modbus structure then?

From what I undestood, the serial.flush() function should do the job but it does not
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code a Max7219/7221 7 segment driver Aggie64 11 3,171 Dec-01-2022, 02:24 AM
Last Post: Larz60+
  help with code for USB-RS485 korenron 3 3,670 Nov-17-2022, 09:04 AM
Last Post: wiseweezer
  Help reading data from serial RS485 korenron 8 14,012 Nov-14-2021, 06:49 AM
Last Post: korenron
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,214 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Stepper motor/Easy Driver/HELP Harney 1 1,893 Jul-31-2021, 08:05 AM
Last Post: Harney
  Writing device driver to stop electric supply to any I/O port sumandas89 0 1,801 May-02-2019, 10:22 AM
Last Post: sumandas89
  State graph Kaluss 1 2,242 Mar-18-2019, 05:29 PM
Last Post: nilamo
  how to upload a file to my gmail driver after login ?? evilcode1 5 4,166 Nov-06-2018, 07:33 AM
Last Post: evilcode1
  Raspberry + RS485 shield | pySerial Coada 6 8,132 Apr-13-2017, 07:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020