read(), readline(), readlines() & xreadlines() func in Python

While doing a regression analysis, I had to read & parse through a large .dat file. I called readline() func but was surprised to see that I was actually iterating through every single character in the first line!

with open(filePath, 'r') as f:     
for line in f.readline():
print line

Then realization dawned upon me!

* read(size) >> size is an optional numeric argument and this func returns a quantity of data equal to size. If size if omitted, then it reads the entire file and returns it

size − This is the number of bytes to be read from the file.

* readline() >> reads a single line from file with newline at the end

* readlines() >> returns a list containing all the lines in the file

* xreadlines() >> Returns a generator to loop over every single line in the file

Advertisements

Read and Write From Serial Port With Raspberry Pi

n this tutorial we will see how to use the serial port on Raspberry Pi. We will use the serial port available on Raspberry with a RS232/TTL 3-5,5V adapter and a USB-serial adapter. By default the Raspberry Pi’s serial port is configured to be used for console input/output. This can help to fix problems during boot, or to log in to the Pi if the video and network are not available.

To be able to use the serial port to connect and talk to other devices (e.g. a modem a printer.. ), the serial port console login needs to be disabled.

Here we use Raspberry Pi 2, and we connect a RS232/TTL 3-5,5V adapter to pins 4 (5V), 6 (GND) ,8 (TX),10 (RX) of Raspberry, obviously connect tx with rx and vice versa.

Emmeshop tutorial Raspberry serial connection

To search for available serial ports we use the command

dmesg | grep tty

The output is something like this

pi@raspberrypi ~ $ dmesg | grep tty
  [    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  [    0.001774] console [tty1] enabled
  [    0.749509] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
  [    1.268971] console [ttyAMA0] enabled
  pi@raspberrypi ~ $

Last line indicates that the console is enabled on the serial port ttyAMA0, so we disable it

Run the configuration command and follow the instructions below

sudo raspi-config

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Emmeshop tutorial Raspberry serial

Reboot and try with

dmesg | grep tty

output now is

pi@raspberrypi ~ $ dmesg | grep tty
  [    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  [    0.001769] console [tty1] enabled
  [    0.749438] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
  pi@raspberrypi ~ $

Now we can use the serial ttyAMA0. We connect an adapter usb / serial, then we will try to establish a communication between the two serial ports; obviously in a practical application to every serial we could connect a device, for example a modem, a printer a RFID reader etc.

Emmeshop tutorial Raspberry two serial port communication

After connecting the second serial port we launch the command to find the name that Raspberry gives him

dmesg | grep tty

The output is something like this

pi@raspberrypi ~ $ dmesg | grep tty
  [    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x93f9c7f9 smsc95xx.macaddr=B8:27:EB:F9:C7:F9 bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  [    0.001769] console [tty1] enabled
  [    0.749438] dev:f1: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev3
  [  971.919417] usb 1-1.2: pl2303 converter now attached to ttyUSB0
  pi@raspberrypi ~ $

Ok, now we create two files, one who writes something on the ttyAMA0 port and the other that reads on the ttyUSB0 port.

serial_write.py

    #!/usr/bin/env python
          
      
           import time
           import serial
          
      
           ser = serial.Serial(
              
               port='/dev/ttyAMA0',
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
           counter=0
          
      
           while 1:
               ser.write('Write counter: %d \n'%(counter))
               time.sleep(1)
               counter += 1

serial_read.py

    #!/usr/bin/env python
          
      
           import time
           import serial
          
      
           ser = serial.Serial(
              
               port='/dev/ttyUSB0',
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
           counter=0
          
      
           while 1:
               x=ser.readline()
               print x

If we run both files, serial_read.py will read what serial_write.py writes

Emmeshop tutorial Raspberry serial write and read

This is just a small example but it can serve as a starting point to send a print to an old printer or read data from a router or a gps.

AWS Cannot download a root CA for IoT

Reader Query: 

When I’m trying to create a new certificate – I can see the page with 4 downloading links (keys and certificates).
Unfortunately, I can download all, except for root CA, because when I’m pushing the last – it throws me to AWS Documentation.

Solution:

The AWS documentation that the link is redirecting you to contains the Root CA certificates. The Root CA certificates are available under the “Server Authentication” section:
https://docs.aws.amazon.com/iot/latest/developerguide/managing-device-certs.html#server-authentication

Based on the signing key, the following certificates are available from the documentation page:

-> RSA 2048 bit key: VeriSign Class 3 Public Primary G5 root CA certificate (https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem)
-> RSA 2048 bit key: Amazon Root CA 1 (https://www.amazontrust.com/repository/AmazonRootCA1.pem)
-> RSA 4096 bit key: Amazon Root CA 2 (https://www.amazontrust.com/repository/AmazonRootCA2.pem)
-> ECC 256 bit key: Amazon Root CA 3 (https://www.amazontrust.com/repository/AmazonRootCA3.pem)
-> ECC 384 bit key: Amazon Root CA 4 (https://www.amazontrust.com/repository/AmazonRootCA4.pem)

If you are creating a new thing using the web console and downloading the credentials (device certificate, device public and private key, etc) then “Amazon Root CA 1” can be used as the root CA certificate.
While creating a new thing from the console,  please do not forget to click on the “Activate” button prior to proceeding to the “Attach a Policy” section.