arduino stuffs
Diffstat (limited to 'car_driver/godot/serial.gd')
| -rw-r--r-- | car_driver/godot/serial.gd | 46 |
1 files changed, 14 insertions, 32 deletions
diff --git a/car_driver/godot/serial.gd b/car_driver/godot/serial.gd index dfb3a0f..674d4c2 100644 --- a/car_driver/godot/serial.gd +++ b/car_driver/godot/serial.gd @@ -1,48 +1,30 @@ extends Node -onready var Serial = preload("res://bin/GDsercomm.gdns").new() +const Serial = preload("res://bin/GDsercomm.gdns") +onready var serial = Serial.new() const baud_rate := 9600 const endline := "\n" -signal recieved(text) -#a readline function, just add the current Port (based on sercomm) -#and it will return a line, since sercomm always use a timeout, it should not lag +#@param text the text to send +func write(text: String) -> void: #"please only use ascii" + if serial.write(text) != 0: #asshole used unicode + print("serial broke, reloading(%s)" % text) + create_serial() -func readline(port): - if !port.has_method("read"): #to avoid problems - return "NOT A PORT" - var cho = "" - var chango = "" - while cho != endline: - cho = port.read() - if typeof(cho) == TYPE_STRING: - if cho != endline: - chango += cho - else: - chango = "FAILED" - break - return chango - - -func _physics_process(_delta: float): - var text = "" - for _i in range(Serial.get_available()): - text += str(Serial.read()) - if text: - emit_signal("recieved", text) - - -func send(text: String) -> void: #"please only use ascii" - Serial.write(text) +func create_serial(): + if serial: + serial.close() + serial = Serial.new() + serial.call_deferred("open", get_ports()[-1], baud_rate, 1000) func _ready(): prints("connecting to", get_ports()[-1]) - Serial.call_deferred("open", get_ports()[-1], baud_rate, 1000) + create_serial() func get_ports() -> Array: - return Serial.list_ports() + return serial.list_ports() |