GroveIPiSerial read write?

Hi,

How can I read and write strings with GroveIPiSerial ports?

I tried to ad a new function definition in grovepi.py which reads with read_i2c_block function.

But i don’t know how to adress the RX pin p0, in your scripts pin 0 is used for the analog port 0.

To use the GrovePi serial port’s, attach a sensor of your choice to the RPISER port. Now you can directly read and write data to your sensor.

You can use the Pyserial package to interact with the sensor. Here is an example https://github.com/DexterInd/ArduBerry/blob/master/Software/Python/pySer.py .

You can modify the example to read and write data to your sensor.

-Karan

Thank’s for the answer but the PiSerial port is already in use by a sensor, i would need to write and read strings on the Grove serial port.

There is no direct support for sensors on the GrovePi serial port. You’ll have to customize the firmware and the software to make use of the serial port.

Can you give some more information about what sensor are you already using with the RPISER port and what other sensor are you trying to use.

-Karan

Thank you for your response but i will use one of the usb ports of the Raspberry to get a second serial port because i have not enough experience in C++ and time to customize the firmware.

But i have a different problem i often get IOError when i try to grovepi.digitalWrite or grovepi.digitalRead, is there a way to prevent that from happening.

I tried to swich digital pin 4 with your Grove_-_2-Coil_Latching_Relay.py script.
Sometimes it prints that the pin is switched on or off without changing the the state of the pin, looks like your exception handling doesn’t work correct.
I have to check the pin after i switched it to be sure it really switched.

#!/usr/bin/env python

import time
import grovepi

ON = 'on’
OFF = ‘off’

relay = 4

def readPin(args):
while True:
pinStatus = grovepi.digitalRead(args)
if pinStatus == 0 or pinStatus == 1:
#print pinStatus
return pinStatus
break
else:
#print 'IOError’
time.sleep(0.3)

def writePin(pin, args):
if args == ‘off’:
while True:
grovepi.digitalWrite(pin,0)
time.sleep(0.3)
y = readPin(pin)
#print y
if y == 1:
print "IOError"
time.sleep(0.3)
elif y == 0:
print ‘pin:’, pin, args
break
elif args == ‘on’:
while True:
grovepi.digitalWrite(pin,1)
time.sleep(0.3)
y = readPin(pin)
#print y
if y == 0:
print "IOError"
time.sleep(0.3)
elif y == 1:
print ‘pin:’, pin, args
break

a = raw_input(‘Type on or off to switch relay:n’)
if a == ON:
writePin(relay, ON)
elif a == OFF:
writePin(relay, OFF)

The IOError is thrown when there is a problem communicating with the GrovePi or if the last command was not sent properly. You can easily use the exception handling to take care of that like in this example https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_button.py .