By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
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
  1. #Calculate horizontal angle
  2.     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)
  3.     T=str(theta)
  4.     #Calculate vertical angle, accounting for possibility of singularity
  5.     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)
  6.     phi=atan2(airplane.estimate.z,c)
  7.     P=str(phi)+"\n"
  8.     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300
  9.     #Send to serial port
  10.  
  11.     ser.write("h")
  12.     ser.write(T)
  13.     ser.write("v")
  14.     ser.write(P)
  15.     ser.readline()
  16.     print ser.readline()
  17.     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
Share this Question
Share on Google+
3 Replies


bartonc
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
  1. #Calculate horizontal angle
  2.     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)
  3.     T=str(theta)
  4.     #Calculate vertical angle, accounting for possibility of singularity
  5.     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)
  6.     phi=atan2(airplane.estimate.z,c)
  7.     P=str(phi)+"\n"
  8.     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300
  9.     #Send to serial port
  10.  
  11.     ser.write("h")
  12.     ser.write(T)
  13.     ser.write("v")
  14.     ser.write(P)
  15.     ser.readline()
  16.     print ser.readline()
  17.     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
  1.     P=str(phi)+"\n"
  2.  
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
  1.     P=str(phi)+"\n"
  2.  
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

bartonc
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
  1. #Calculate horizontal angle
  2.     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)
  3.     T=str(theta)
  4.     #Calculate vertical angle, accounting for possibility of singularity
  5.     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)
  6.     phi=atan2(airplane.estimate.z,c)
  7.     P=str(phi)+"\n"
  8.     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300
  9.     #Send to serial port
  10.  
  11.     ser.write("h")
  12.     ser.write(T)
  13.     ser.write("v")
  14.     ser.write(P)
  15.     ser.readline()
  16.     print ser.readline()
  17.     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
  1. #
  2.  
  3. #Calculate horizontal angle
  4.     theta=atan2(airplane.estimate.y,airplane.estimate.x+.01)
  5.     T=str(theta)+"\n"
  6.     #Calculate vertical angle, accounting for possibility of singularity
  7.     c=sqrt((airplane.estimate.y)**2+(airplane.estimate.x)**2+.01)
  8.     phi=atan2(airplane.estimate.z,c)
  9.     P=str(phi)+"\n"
  10.     antenna.axis=vector(cos(theta),sin(theta),sin(phi))*300
  11.     #Send to serial port
  12.  
  13.     ser.write("h")
  14.     ser.write(T)
  15.     print ser.readline()
  16.     ser.write("v")
  17.     ser.write(P)
  18.     print ser.readline()
  19.     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

Post your reply

Sign in to post your reply or Sign up for a free account.