extends Node

var serial: GdSerial

var latestData

func _ready():
	# Create serial instance
	serial = GdSerial.new()
	
	# List available ports
	print("Available ports:")
	var ports = serial.list_ports()
	for i in range(ports.size()):
		var port_info = ports[i]
		print("- ", port_info["port_name"], " (", port_info["port_type"], ")")
	
	# Configure and open port
	serial.set_port("/dev/cu.usbmodem101")  # Adjust for your system
	serial.set_baud_rate(9600)
	serial.set_timeout(1000)
	
	
	
	


func _process(delta): 
	serial.readline();
	if serial.open():
		print("Port opened successfully!")
				# Send command
		serial.writeline("Hello Arduino!")
	
		
		# Wait and read response
		await get_tree().create_timer(0.1).timeout
		if serial.bytes_available() > 0:
			var response = serial.readline()
			print("Response: ", response)
			latestData = response
		
		serial.close()
	#else:
		#print("Failed to open port")
	#print(latestData)
	
	
