MqxM
Deno3y ago
102 replies
Mqx

Use Deno.run() to communicate with Arduino

Hey I'm trying to communicate via USB serial with my Arduino. My goal is to have the result I get via USB serial available as a variable in Deno. I tried to do the whole thing with Deno.run().

This is my Arduino main file:
#define POTENTIOMETER_PIN_A A0

void setup() {
    pinMode(POTENTIOMETER_PIN_A, INPUT);
    Serial.begin(115200);
}

void loop() {
    int potentiometerA = analogRead(POTENTIOMETER_PIN_A);
    
    String data = map(potentiometerA, 0, 1023, 0, 100) + String('\n');

    Serial.write(data.c_str());
    
    delay(100);
}


With this simple PowerShell script I can read the data from USB serial:
$port= new-Object System.IO.Ports.SerialPort COM4,115200,None,one
$port.Open()
do {
    $line = $port.ReadLine()
    Write-Host $line
}
while ($port.IsOpen)


How can I run the whole thing in Deno now and get the output?

I have tried the following which obviously does not work:
Deno.run({
    cmd: [
        'powershell',
        '$port= new-Object System.IO.Ports.SerialPort COM4,115200,None,one',
        '$port.Open()',
        'do { $line = $port.ReadLine(); Write-Host $line }',
        'while ($port.IsOpen)'
    ]
})


Can anyone help me with this? Or is there perhaps a better solution for that?
Thanks!
Was this page helpful?