DenoDDeno
Powered by
MqxM
Deno•4y 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()
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);
}
#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)
$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)'
    ]
})
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!
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Migrating `Deno.run` to `Deno.command` with `readlines`
Ed, Edd n EddyEEd, Edd n Eddy / help
3y ago
Problems with Deno run
ashAash / help
3y ago
How to run mocha with deno
BhauminBBhaumin / help
2y ago
Use ESLint with Deno
MqxMMqx / help
2y ago