"Has the implementation of functions within the Serial API, specifically navigator.serial and SerialPort, not been completed

I’ve been attempting to establish a connection with the serial port through a web page, and it seems that the supported method for this is using the Serial API.

var Serial = {};

(function() {
  'use strict';
  /*
   * Initiate the Serial object
   */
  Serial.debug = true;
  
//  Serial.device;
  
  Serial.log = function(message) {
    if(Serial.debug)
    {
      let pre;
      if(!document.querySelector('pre'))
      {
        pre = document.createElement('pre');
        document.body.appendChild(pre);
      }
      pre = document.querySelector('pre');
      pre.innerHTML += `\n${message}`;
    }
  }
  
  Serial.request = function() {
  
  const requestOptions = {
  // Filtering devices with the Arduino USB vendor ID.
  filters: [{ vendorId: 0x0403 }],
};

// Prompt user to select an Arduino device.
            console.log(navigator);
        console.log(navigator.serial);
const port =  navigator.serial.requestPort(requestOptions);

// Open port and start reading data.
 port.open({ baudrate: 19200 });
const reader = port.in.getReader();
while (true) {
  const {done, data} =  reader.read();
  if (done) break;
  console.log(data);
}
  
  

  }
  
  Serial.port = {
     
     device:{},
    
     connect:function()
     {
      let loop = () => {
        this.device.transferIn(5, 64).then(result => {
        Serial.log(result);
          loop();
        }, error => {
          WebUSB.log(error);
        });
      }
      console.log(this.device);
      return this.device.open( {baudrate: 19200 })
        .then(() => this.device.selectConfiguration(1))
        .then(() => this.device.claimInterface(1))
        .then(() => this.device.controlTransferOut({requestType: 'class', recipient: 'interface', request: 0x22, value: 0x01, index: 0x02}))
        .then(() => {loop})
        .then(
          result => {
          Serial.log('successfull');
          }
        )
        .catch(
          error => {
          Serial.log(error);
          }
        );
    },
    
    send:function()
    {
      let d = new Date();
      let h = d.getHours();
      let m = d.getMinutes();
      let s = d.getSeconds();
      if(h < 10){h = `0${h}`};
      if(m < 10){m = `0${m}`};
      if(s < 10){s = `0${s}`};
      let data = `show time ${h}${s % 2 == 1 ? ':' : ' '}${m}${s % 2 == 0 ? ':' : ' '}${s}`;
      console.log(data);
      let textEncoder = new TextEncoder();
      WebUSB.port.device.transferOut(4, textEncoder.encode(data));
    },
     
     disconnect:function()
     {
      console.log(Serial.port.device)
      Serial.port.device.close()
      .then(
        result => {
        Serial.log('closed');
          document.querySelectorAll('button')[1].parentNode.removeChild(document.querySelectorAll('button')[1]);
        }
      )
      .catch(
        error => {
        Serial.log(error);
        }
      );;
     }
   }

})();

/*
 *
 */
window.addEventListener('DOMContentLoaded', connect => {
  let button = document.createElement('button');
  button.innerHTML = 'Connect a USB Device';
  button.addEventListener('click', Serial.request);
  document.body.appendChild(button);
}, true);

However, I have encountered an issue where the call to navigator.serial.requestPort() is failing as it appears that navigator.serial is undefined. Despite having ensured that the serial port is connected to my system, I am facing difficulties. Could it be that certain functions within navigator.serial and SerialPort in the Serial API are not fully implemented? For reference, I am using Chrome version 74.0.3729.131 on Ubuntu 16.04.

Answer №1

Reposting @Benjamin's response to provide clarity. Serial port connections are considered an "experimental" feature in Chrome as of early 2020. To enable this feature, you need to follow these steps:

chrome://flags/#enable-experimental-web-platform-features

Simply copy and paste the above line into the address bar of your Chrome browser and activate it.

Answer №2

Unfortunately, the Serial API is still undergoing implementation in Chrome. I noticed that you have come across this particular issue in the Chromium bug tracker related to its development. Any updates regarding the progress of implementation will be shared there. As of now, it remains in the "started" phase and has not yet been marked as "fixed".

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discover the default text highlighting color of a browser with the use of JavaScript or Dart programming

Did you know that you can change the browser's default text highlight (selection) background color using CSS? For example: ::selection { background: #ffb7b7; } Find out more about the browser/OS specific default background color when selecting tex ...

Labeling with the index number of arrays

var arr = []; $('.inp').click(function(){ function arrcall() { $.each(arr, function(key, value) { alert('Array Key is: ' + key + ' \n Array Key value is: ' + value); }); } ...

Baconjs exclusively retrieves the final debounce value

Below is a code snippet that showcases my current implementation: let watcher; const streamWatcher = bacon.fromBinder(sink => { watcher = chokidar.watch(root, { ignored: /(^|[\/\\])\../ }); watcher.on('all&a ...

Validating dates using JQuery within a specific range of dates

Users can input a date within a specified range in an input field. To enable this feature, I created a new method using the jQuery validator object: $.validator.addMethod("dateRange", function(value, element, from, to){ try { var date = new D ...

The Camera component in React Native is not currently supporting foreground service

Trying to capture images in a foreground service, such as taking pictures while the user is using another app. Everything works fine within the app, but when the app is closed with the foreground service active, the camera stops working and shows this erro ...

Adjust the default color for the icon in the v-text-field type=date using Vuetify

I need to filter records between two dates, displaying data retrieved from the backend. Instead of following the traditional method outlined in Vuetify's date pickers documentation or using the CodePen example: Vuetify v-text-field Date Picker on Cod ...

The Vue.js component experiencing issues with the Foundation 6 modal functionality

I'm facing an issue with my vue.js single file component. I have a button that is supposed to open a modal with a video from zurb foundation. However, every time I click the button, the page reloads without showing any errors in the console or network ...

The Dropzone feature fails to function properly when displayed on a modal window that is being shown via

After attempting to integrate Dropzone and JavaScript into a Bootstrap Modal called through AJAX, I encountered issues. Due to the high number of records, I avoided placing the Modal in a foreach loop and instead used AJAX to call a controller method upon ...

Incorporating SCSS directly in React component along with material-ui styling

I'm having trouble incorporating styles into my React component. I'd prefer to use SCSS instead of either using the withStyle function or importing a plain CSS file into a JS file. For instance, I would like to import my SCSS file into ButtonCom ...

Save a newly uploaded image to Github utilizing NodeJS and the Github API

Struggling to upload an image to my Github repo using NodeJS and Github API has been a challenge for me. I have managed to create the SHA, Commit Tree, and all necessary steps, but the final hurdle is passing the image to the API and saving it as an actual ...

I am perplexed as to why my useEffect function is executing twice within my NextJs client

I am currently working on a NextJs program and using a function called useAsync on my website. However, I have noticed that this function runs twice every time it is called. I suspect that the useEffect calling the useAsyncInternal might be causing this be ...

The correct method for removing NPM packages (Npm Uninstall versus Updating package.json)

I am facing the challenge of removing multiple libraries from my project. I am concerned about potentially causing issues by removing a library that is a dependency for another library. What is the safest approach to remove these libraries? I have two op ...

Having trouble with jQuery UI draggable when using jQueryUI version 1.12.1?

Currently, I am diving into the world of jQuery UI. However, I am facing an issue with dragging the boxes that I have created using a combination of HTML and CSS. My setup includes HTML5 and CSS3 alongside jQuery version 1.12.1. Any suggestions or help wou ...

Data structures of advanced complexity within HTML data attributes

I am delighted by the existence of the html5 data attribute. It's great to be able to input a plain string into html attributes and retrieve them using jquery. However, wouldn't it be wonderful to have more than just a basic string? Is there a ...

Manipulating HTML content with Javascript Array

Is there a way to retain Javascript context from an array? Any suggestions for improving this code? SAMPLE: <html> <script type="text/javascript"> var rand = Math.floor(Math.random() * 6) + 1; var i = Math.floor(Math.random() * 6) + 1; var ...

Enhanced Bootstrap Carousel with White Background Transitions

Having trouble articulating my issue, so here's a video that should clarify: https://example.com/video I'm using a bootstrap carousel template for a full-width slider. Despite my efforts to remove the white-space transitions, they persist. Am I ...

I am looking to improve my form submission process by using JavaScript to only submit the form itself, rather than the

I have integrated JS and HTML code. The issue I am facing is that upon submission, the entire page is being submitted instead of just my form. function submit_by_id() { var name = document.getElementById("name").value; var email = document.getElementByI ...

Using Jquery to iterate through a dynamic list of elements

Currently, I am exploring the idea of creating a forEach loop that can handle an unspecified number of elements. The goal is to randomly select one element, perform XYZ actions on it, make it visible, and then eliminate it from consideration. This process ...

Trigger an alert using the Ajax response

Is there a way to display the value of imageX outside of the $.ajax() function? var imageX; $.ajax({ type: 'GET', url: 'php/my1.php', dataType: 'json', async: 'false', success: function(response) ...

Find items where a certain value matches any of the values in another array and return them

I'm seeking assistance with a specific task. I have an object structured as shown below: SOMEKEY: { "key1": val1, "key2": val2 } Additionally, I have an array that contains string values like this: [stringA, stringB, stringC ...