Python Forum
serial communication write(comand) and read(result)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
serial communication write(comand) and read(result)
#1
Hi

maybe someone could have a look at my code or suggest a better one (or another place to look for)


I need to do serial communication with a device: write to device and listen to what it is saying

I should write a command (send some bytes). I finish my command sendind byte 0XFF (chr(255)) 3 times in a row
Then the device sends me a reply that i have to read. This reply also finishes after 3 times 0XFF

I wrote this

import serial

ser = serial.Serial(

    port='/dev/ttyAMA0',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)
ser.reset_output_buffer()

print('Serial connected')

def nxRead(value):
    #first i need to send a 'command' (for example 'sendme X.value'
    command = 'sendme X.value'
    ser.write(command.encode('latin-1'))
    ser.write(chr(255).encode('latin-1')) # 0xFF
    ser.write(chr(255).encode('latin-1')) # 0xFF
    ser.write(chr(255).encode('latin-1')) # 0xFF

    #by now the device should have understood my request and will send me a reply byte by byte, finishing with 0XFF 0XFF 0XFF 

    reply = [] 
    count =0
    t3FF = False # the termination has not stopped

    while t3FF == False:
       r = ser.read().decode('latin-1)
       c = ord(r)
       reply.append(c)
         
           if c == 0xff:
               count = count + 1
               if count == 3:
                   t3FF = True
                   return
           else:
               count = 0
    return s
It's not working, why?
Reply
#2
Be more specific. What isn't working right? Any errors or exceptions? Is it doing the wrong thing? Is it doing nothing at all? Do you know if the device is receiving your messages? I don't know this package but I know a lot of I/O libraries perform buffering, does the Serial object have a flush() method?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,466 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 625 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,099 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  How do I read and write a binary file in Python? blackears 6 6,669 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 829 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,622 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,039 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Read JSON via API and write to SQL database TecInfo 5 2,218 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  Write and read back data Aggie64 6 1,897 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,850 Jul-17-2021, 04:19 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

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