443,660 Members | 1,100 Online Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

# serial port digit-by-digit

 P: 2 I was writing a program calculating angles. There is a horizontal and a vertical angle and when I want the serial port to read both, it can give the horizontal digit by digit and the vertical all at once. For example it gives something like: h2 h2.60 h2.6029 and it will occasionally give the vertical coordinates, but it works fine for just the verticals. I have tried both readline() and readlines() and they give similar results. I haven't seen anything like this discussed on the Internet so far Here is part of my code: Expand|Select|Wrap|Line Numbers #Calculate horizontal angle     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)     T=str(theta)     #Calculate vertical angle, accounting for possibility of singularity     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)     phi=atan2(airplane.estimate.z,c)     P=str(phi)+"\n"     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300     #Send to serial port       ser.write("h")     ser.write(T)     ser.write("v")     ser.write(P)     ser.readline()     print ser.readline()     x=x+.01 It works the best with timeout=0, because the VPython works with timeout>0, but no coordinates show --Michael May 31 '07 #1
3 Replies

 Expert 5K+ P: 6,596 I was writing a program calculating angles. There is a horizontal and a vertical angle and when I want the serial port to read both, it can give the horizontal digit by digit and the vertical all at once. For example it gives something like: h2 h2.60 h2.6029 and it will occasionally give the vertical coordinates, but it works fine for just the verticals. I have tried both readline() and readlines() and they give similar results. I haven't seen anything like this discussed on the Internet so far Here is part of my code: Expand|Select|Wrap|Line Numbers #Calculate horizontal angle     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)     T=str(theta)     #Calculate vertical angle, accounting for possibility of singularity     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)     phi=atan2(airplane.estimate.z,c)     P=str(phi)+"\n"     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300     #Send to serial port       ser.write("h")     ser.write(T)     ser.write("v")     ser.write(P)     ser.readline()     print ser.readline()     x=x+.01 It works the best with timeout=0, because the VPython works with timeout>0, but no coordinates show --Michael Hi Michael. The key appears to be: Expand|Select|Wrap|Line Numbers     P=str(phi)+"\n"   the \n added to P. You missed that on T May 31 '07 #2

 P: 2 Hi Michael. The key appears to be: Expand|Select|Wrap|Line Numbers     P=str(phi)+"\n"   the \n added to P. You missed that on T I had tried it on T, but it was completely ignoring the horizontal component May 31 '07 #3

 Expert 5K+ P: 6,596 I was writing a program calculating angles. There is a horizontal and a vertical angle and when I want the serial port to read both, it can give the horizontal digit by digit and the vertical all at once. For example it gives something like: h2 h2.60 h2.6029 and it will occasionally give the vertical coordinates, but it works fine for just the verticals. I have tried both readline() and readlines() and they give similar results. I haven't seen anything like this discussed on the Internet so far Here is part of my code: Expand|Select|Wrap|Line Numbers #Calculate horizontal angle     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)     T=str(theta)     #Calculate vertical angle, accounting for possibility of singularity     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)     phi=atan2(airplane.estimate.z,c)     P=str(phi)+"\n"     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300     #Send to serial port       ser.write("h")     ser.write(T)     ser.write("v")     ser.write(P)     ser.readline()     print ser.readline()     x=x+.01 It works the best with timeout=0, because the VPython works with timeout>0, but no coordinates show --Michael Have you tried readline() after each write() with a newline: Expand|Select|Wrap|Line Numbers #   #Calculate horizontal angle     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)     T=str(theta)+"\n"     #Calculate vertical angle, accounting for possibility of singularity     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)     phi=atan2(airplane.estimate.z,c)     P=str(phi)+"\n"     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300     #Send to serial port       ser.write("h")     ser.write(T)     print ser.readline()     ser.write("v")     ser.write(P)     print ser.readline()     x=x+.01 You may also want/need to insert a delay between the write() and readline() to allow the characters to accumulate in the buffer. Jun 1 '07 #4 