Problems to detect what I receive via serial interface.

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Problems to detect what I receive via serial interface.

Post by BOB63 » Sun Sep 27, 2015 12:05 pm

Hi ,
I'm in trouble with the data received via serial interface .
I wrote a python code to receive some data by serial interface and apparently all work fine , until I try to verify with a simple "if" the string received is what i expect .
The code receive data from serial interface and via PuTTy I've cheched that what I receive is what I expect to receive "connecting\r\n", but then when I try to check if I've received the right info, the program seems don't work properly , and I don't understand where is the problems because the case seems to me quite simple . :roll: :roll:
Any idea ? :?: :?:
Thanks a lot since now .... Bob

Code: Select all

while True:
      if uart.any():
         data_read=uart.readline()
         
         if data_read!="":  
            print(data_read)                                    #  b'connecting\r\n'  is what appear in PuTTY
            uart.write(data_read+"\r\n")                 #  b'connecting\r\n'  is what I send back and receive the sender. 
                                                                         #  Until above line all seems to be ok., but the next line don't works. 
            if data_read=="connecting\r\n":           # <---- THIS[b] IF[/b] SEEMS NOT DETECT WHAT IS INSIDE THE data_read 
               print("CONNECTED")
               send_setting()
               
##       else:   
##                z1=data_read[0]
##                s1=data_read[1]
##                hr_start_z1=data_read[2]
##                min_start_z1=data_read[3]
##                hr_stop_z1=data_read[4]
##                min_stop_z1=data_read[5]
##                lu_z1=data_read[6]
##                ma_z1=data_read[7]
##                me_z1=data_read[8]
##                gi_z1=data_read[9]
##                ve_z1=data_read[10]
##                sa_z1=data_read[11]
##                do_z1=data_read[12]
##                soglia1=data_read[13]
##                z2=data_read[14]
##                s2=data_read[15]
##                hr_start_z2=data_read[16]
##                min_start_z2=data_read[17]
##                hr_stop_z2=data_read[18]
##                min_stop_z2=data_read[19]
##                lu_z2=data_read[20]
##                ma_z2=data_read[21]
##                me_z2=data_read[22]
##                gi_z2=data_read[23]
##                ve_z2=data_read[24]
##                sa_z2=data_read[25]
##                do_z2=data_read[26]
##                soglia2=data_read[27]
      
      time_stamp=rtc.datetime()
      hr_curr=time_stamp[4]
      min_curr=time_stamp[5]
      day_curr=time_stamp[3]
Last edited by BOB63 on Sat Dec 12, 2015 11:18 am, edited 1 time in total.
Thanks. Roberto

BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Re: Problems to detect what I receive via serial interface.

Post by BOB63 » Sun Sep 27, 2015 3:15 pm

... At the end I've understood why was not working. :oops:
The data received from serial are not the "character" but the decimal code of ascii characters so if I send the string "connected" il receive
this sequence inside the array data_received: 99,111,110,110,101,99,116,101,100
When I ask to print the data_received , the ascii codes are converted to character , but if I try to compare data_read with a string , It can works !! ;)

This is how I've fixed the problem :

Code: Select all

while True:
     [b] data_read_chr=""[/b]
      if uart.any():
         data_read=uart.readline()
         print(data_read) 
         if data_read!="":  
            uart.write(data_read+"\r\n")
            for letter in data_read:
                [b]data_read_chr=data_read_chr+ chr(letter)[/b]
            print(data_read_chr)
            if data_read_chr=="connected":  
               print("CONNECTED")
               send_setting()
Thanks. Roberto

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problems to detect what I receive via serial interface.

Post by dhylands » Sun Sep 27, 2015 3:50 pm

You could also use b'connected'

using the b prefix on a string creates a byte string rather than a unicode string.

BOB63
Posts: 58
Joined: Sat Jul 25, 2015 8:24 pm
Location: Monza , Italy

Re: Problems to detect what I receive via serial interface.

Post by BOB63 » Sun Sep 27, 2015 4:48 pm

AH !! Another new thing learned ! :D
Thank you so much !
Thanks. Roberto

Post Reply