Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO.Ports;
  4. using System.Threading;
  5. public class Sending : MonoBehaviour{
  6. //public static SerialPort sp = new SerialPort("COM4",9600,Parity.
  7. public SerialPort sp = new SerialPort("----------);
  8. public string message2;
  9. float timePassed = 0.0f;
  10. //Use this for initialization
  11. void Start()
  12. {
  13. OpenConnection();
  14. }
  15.  
  16. //Update is called once per frame
  17. void Update(){
  18. //timePassed+= Time.deltaTime;
  19. //if(timePassed)=0.2f){
  20. //print("BytesToRead" +sp.BytesToRead);
  21. message2 = sp.ReadLine ();
  22. print (message2);
  23. ///timePassed = 0.0f;
  24. //}
  25. }
  26.  
  27. public void OpenConnection()
  28. {
  29. if(sp != null)
  30. {
  31. if (sp.IsOpen) {
  32. sp.Close ();
  33. print ("Closing port, because it was already open!");
  34. } else {
  35. sp.Open ();//open the connection
  36. sp.ReadTimeout = 16;
  37. print ("Port Opened!");
  38. //message = "Port Opened";
  39. }
  40. }
  41. else
  42. {
  43. if (sp.IsOpen)
  44. {
  45. print ("Port is already open");
  46. }
  47. else
  48. {
  49. print ("Port == null");
  50. }
  51. }
  52. }
  53. void OnApplicationQuit()
  54. {
  55. sp.Close();
  56. }
  57. public void sendStop(){
  58. sp.Write ("s");
  59. }
  60. public void sendForward(){
  61. sp.Write("f");
  62. }
  63. public void sendBack(){
  64. sp.Write("b");
  65. }
  66. public void sendRight(){
  67. sp.Write("r");
  68. }
  69. public void sendLeft(){
  70. sp.Write("l");
  71. }
  72. }
  73.  
  74. using UnityEngine;
  75. using System.Collections;
  76. public class CallForward: MonoBehaviour {
  77.  
  78. // Use this for initialization
  79. void Start () {
  80.  
  81. }
  82.  
  83. // Update is called once per frame
  84. void Update () {
  85.  
  86. }
  87.  
  88. void OnMouseDown(){
  89. print ("Clicked");
  90. Sending.sendForward();
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement