Forum >APC220 and pyserial
General

APC220 and pyserial

userHead snskreationz 2013-01-20 21:10:26 4791 Views1 Replies
Hi, I have bought an APC220 recently and got it to work with the arduino serial monitor. I have wrote a script in python which works well with USB connection, but does not work at all with the APC 220. Is there any problem that might cause this?

The program uses pygame to read a controller and then pyserial to send the data
the arduino code just uses Serial.parseInt to read the values sent

Thank You in advance


[code]
import sys
import serial
import time
import math
import pygame
PORT = 'COM5'

pygame.init()
time.sleep(2)
j = pygame.joystick.Joystick(0)
j.init()

ser = serial.Serial(PORT, 9600)


time.sleep(2)

try:
while True:
pygame.event.pump()
axis0 = j.get_axis(0)
axis1 = j.get_axis(1)
axis2 = j.get_axis(2)
axis3 = j.get_axis(3)
button1 = j.get_button(0)
button2 = j.get_button(1)
button3 = j.get_button(2)
button4 = j.get_button(3)
button5 = j.get_button(4)
button6 = j.get_button(5)
button7 = j.get_button(6)
button8 = j.get_button(7)
button9 = j.get_button(8)
button10 = j.get_button(9)
axis1 = -axis1*100
axis3 = -axis3*100
axis0 = -axis0*100
axis2 = -axis2*100
xaxis1 = int(math.floor(axis1))
xaxis3 = int(math.floor(axis3))
xaxis0 = int(math.floor(axis0))
xaxis2 = int(math.floor(axis2))

xaxis1 = str(xaxis1)
xaxis3 = str(xaxis3)
xaxis0 = str(xaxis0)
xaxis2 = str(xaxis2)
button1 = str(button1)
button2 = str(button2)
button3 = str(button3)
button4 = str(button4)
button5 = str(button5)
button6 = str(button6)
button7 = str(button7)
button8 = str(button8)
button9 = str(button9)
button10 = str(button10)



print (button1 + "," + button2 + "," + button3 + "," + button4 + "," + button5 + "," + button6 + "," + button7 + "," + button8 + "," + button9 + "," + button10 + "," + xaxis1 + "," + xaxis3 )

ser.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" % ( button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, xaxis1, xaxis3 ))


except KeyboardInterrupt:
j.quit()



ser.close()
print "Bytes Written to port:", written
print "Value written to port: '%s'"%val

if __name__ == '__main__':
args = sys.argv
try:
main(args[1])
except IndexError:
main()
[/code]

2013-01-21 07:37:26 I figured out how to get the APC220 working by translating a few forums.

I had to put ser.setRTS(0)

The problem now is that there is is significant lag when communicating. sometimes it might take a few seconds, sometimes nothing happens for a long time.
Is there any setting in these parameters that could make a difference?

(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, writeTimeout=None, dsrdtr=False, interCharTimeout=None)

I am using baudrate 19200 right now.
From the arduino serial, I always get the communication with minimal delay, so once again, I think it might be a setting in pyserial that is causing this.

Thank You.

userHeadPic snskreationz