-
Notifications
You must be signed in to change notification settings - Fork 2
/
serial_port_test.gd
114 lines (82 loc) · 2.61 KB
/
serial_port_test.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
extends Control
var serial := SerialPort.new()
var port := ""
var baudrate := 9600
var is_hex := false
func send_input_content():
if serial.is_open():
if is_hex:
# Get hex code list
var hex_str_arr:PackedStringArray = %InputArea.text.split(" ", false)
var hex_buf: PackedByteArray
for hex in hex_str_arr:
if hex.is_valid_hex_number():
# NOTE: May be cause an error, because the hex string may large than 2 bytes.
hex_buf.append(hex.hex_to_int())
serial.write_raw(hex_buf)
else:
serial.write_str(%InputArea.text)
func update_serial():
port = %SerialList.get_item_text(%SerialList.selected)
serial.port = port
baudrate = %BaudRate.get_item_text(%BaudRate.selected).to_int()
serial.baudrate = baudrate
func _on_error(where, what):
print_debug("Got error when %s: %s" % [where, what])
# Called when the node enters the scene tree for the first time.
func _ready():
serial.got_error.connect(_on_error)
var ports_info := SerialPort.list_ports()
for info in ports_info:
%SerialList.add_item(info)
if ports_info.size():
%SerialList.select(0)
%BaudRate.add_item("4800")
%BaudRate.add_item("9600")
%BaudRate.add_item("115200")
%BaudRate.select(1)
update_serial()
serial.data_received.connect(_on_data_received)
serial.start_monitoring(20000)
pass # Replace with function body.
func _exit_tree():
serial.stop_monitoring()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
# if serial.available() > 0:
# var rec := serial.read_raw(serial.available())
# _on_data_received(rec)
# pass
pass
func _on_data_received(data: PackedByteArray):
# await RenderingServer.frame_post_draw
%Content.text += data.get_string_from_ascii()
%Content.scroll_vertical = %Content.get_line_count()
if %Content.get_line_count() > 50:
%Content.text = ""
print("Received[%d]: %s" % [data.size(), data.get_string_from_ascii()])
if serial.is_open():
serial.write_raw(data)
func _on_send_pressed():
send_input_content()
func _on_hex_toggled(button_pressed):
is_hex = button_pressed
func _on_input_area_text_submitted(new_text):
send_input_content()
func _on_serial_list_item_selected(index):
port = %SerialList.get_item_text(index)
func _on_open_close_toggled(button_pressed):
if button_pressed:
serial.baudrate = baudrate
serial.open(port)
if serial.is_open():
button_pressed = false
%OpenClose.text = "Close"
else:
serial.close()
if not serial.is_open():
button_pressed = true
%OpenClose.text = "Open"
func _on_baud_rate_item_selected(index):
baudrate = %BaudRate.get_item_text(index).to_int()
pass # Replace with function body.