Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func (a *App) Greet(name string) string {
}

func (a *App) GetSerialNames() []string {
ports, _ := serial.GetPortsList()
ports, err := serial.GetPortsList()
if err != nil {
a.sendTowebStr("ERROR listing ports: " + err.Error() + "\n")
return []string{}
}

return ports
}
Expand All @@ -40,25 +44,28 @@ func (a *App) PortOpen(serialName string, baudRate int, length int) {
a.PortClose()
mode := &serial.Mode{
BaudRate: baudRate,
DataBits: 8,
Parity: serial.NoParity,
StopBits: serial.OneStopBit,
}
port, err := serial.Open(serialName, mode)
if err != nil {
runtime.EventsEmit(a.ctx, "printToweb", "ERROR : "+err.Error())
a.sendTowebStr("ERROR opening " + serialName + ": " + err.Error() + "\n")
return
}

a.port = port
a.sendTowebStr(fmt.Sprintf("Connected to %s @ %d baud\n", serialName, baudRate))
buff := make([]byte, length)
go func() { // Goroutine untuk membaca port
for {
n, err := port.Read(buff)
if err != nil {
a.sendTowebStr((err.Error()))
a.sendTowebStr("ERROR reading " + serialName + ": " + err.Error() + "\n")
break
}
if n == 0 {
fmt.Println("\nEOF")
break
continue
}

a.sendToweb(buff[:n])
Expand All @@ -79,7 +86,9 @@ func (a *App) PortClose() {
return
}

a.port.Close()
if err := a.port.Close(); err != nil {
a.sendTowebStr("ERROR closing port: " + err.Error() + "\n")
}
a.port = nil
}
func (a *App) sendToweb(buff []byte) {
Expand Down
36 changes: 25 additions & 11 deletions frontend/src/AppMain.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<script lang="ts" setup>
import { defineComponent, onMounted, onUnmounted, ref, watch } from 'vue';
import { onMounted, onUnmounted, ref } from 'vue';
import { GetSerialNames, IsConnected, PortClose, PortOpen } from '../wailsjs/go/main/App';
import HelloWorld from './components/HelloWorld.vue'

export type PropType = {
openAbout :()=>void
}
const prop = defineProps<PropType>();

const listName = ref<string[]>();
const baudRate = ref<string>();
const listName = ref<string[]>([]);
const baudRate = ref<string>("9600");
const selectedName = ref<string>();
const isPortsConnected = ref<boolean>(false);

const textTerminal = ref<string>("");
let stopPrintTowebListener: (() => void) | null = null;

function sleep(n:number){
return new Promise((r,x)=>{
Expand All @@ -35,10 +35,20 @@ onMounted(() => {
PortClose();
getSerialList();

(window as any).runtime.EventsOn("printToweb", (data:number[])=>{
var textDecoder = new TextDecoder("utf-8");
var str = textDecoder.decode(new Uint8Array(data));
textTerminal.value += str;
stopPrintTowebListener = (window as any).runtime.EventsOn("printToweb", (data:number[] | string)=>{
if (Array.isArray(data)) {
var textDecoder = new TextDecoder("utf-8");
var str = textDecoder.decode(new Uint8Array(data));
textTerminal.value += str;
return;
}

if (typeof data === "string") {
textTerminal.value += data;
return;
}

textTerminal.value += String(data);
});


Expand Down Expand Up @@ -71,6 +81,10 @@ function startConnect(){

onUnmounted(()=>{
PortClose();
if (stopPrintTowebListener) {
stopPrintTowebListener();
stopPrintTowebListener = null;
}
isStillOpen = false;
})

Expand All @@ -92,7 +106,7 @@ onUnmounted(()=>{
<td>

<select v-model="selectedName">
<option v-for="item in listName">{{ item }}</option>
<option v-for="item in listName" :key="item" :value="item">{{ item }}</option>
</select>
</td>
</tr>
Expand All @@ -110,8 +124,8 @@ onUnmounted(()=>{
<td></td>
<td></td>
<td>
<button v-if="!isPortsConnected" :onclick="startConnect">Start Connect</button>
<button class="btnred" v-if="isPortsConnected" :onclick="()=>{ PortClose(); }">Disconnect</button>
<button v-if="!isPortsConnected" @click="startConnect">Start Connect</button>
<button class="btnred" v-if="isPortsConnected" @click="()=>{ PortClose(); }">Disconnect</button>
</td>
</tr>

Expand Down