A Google Chrome Extension that allows client-side JavaScript to read/write Serial and Parallel ports.
Control Arduinos and send Parallel Port triggers for electrophysiological devices and other legacy hardware from a webpage. No server required!
jsPsychHardware was built as a companion to the fantastic jsPsych library for creating and running behavioral experiments in a browser. However, you don't need to include it on your page, here's how to get up and running
You will need to install this small native C++ program to relay messages from the extension to the hardware.
Windows x64 and x86 only, Linux support planned
You can find and install the latest version of this extension here
Let jsPsychHardware know that your page will need it by setting a custom "jspsych" attribute on your <html>
element. This is done automatically if you have already included jsPsych through a <script>
tag in your page.
<!-- Set this attribute -->
<html jspsych="present">
...
<!-- Rest of your webpage goes here -->
</html>
You can now use jsPsychHardware by clicking the jsPsych icon on the top right of your Chrome screen!
You can connect to a COM port by selecting one from the options. The icon will turn green upon successful connection. Make sure no other processes (like an Arduino Serial monitor) are using the COM port you are trying to connect to!
You will need to provide jsPsychHardware with the hardware address of your Parallel port. It is a 4-digit hex number you can find by consulting your system's settings as described here, then click the "on" button next to the input box to confirm the address
jsPsychHardware uses Custom Events to communicate with your client-side JS. You will thus send commands by dispatching jspsych
type Events on the document
element
// Creating a message to display a byte with value 255 on the parallel port
var myEvent = new CustomEvent('jspsych', {
detail:{
target : 'parallel',
action : 'trigger',
payload: 255
}
});
//send the message
document.dispatchEvent(myEvent);
jsPsychHardware's events have a detail
object with 3 fields describing the desired operation:
Field name | Usage | Possible values |
---|---|---|
target | indicates the target hardware to operate | serial parallel |
action | What operation to conduct | trigger send read |
payload | action-dependent information needed to complete the command | `{String |
You can write to the serial port using event detail with target serial
, action send
, and a String for payload
//you can 'send' Strings to the serial port
document.dispatchEvent(new CustomEvent('jspsych',{
detail:{
target: 'serial',
action: 'send',
payload: 'Hello Arduino' //String only, other types will fail
}
}));
payload
must be a valid integer representing the maximal number of bytes to read from the port. jsPsychHardware will emit a jspsych-hardware-message
event when the hardware responds. The event objects' detail
member will contain the data.
If no data is available on the port when the read command is sent, the event will contain no data. Make sure your device has written data before attempting to read
//create a listener that will receive the result of all read operations
document.addEventListener('jspsych-hardware-message', function(evt){
var receivedObject = evt.detail;
var value = receivedObject.result //the char array read from the port
if(value){
//do something with the received data...
}
else{
//there was nothing to read when this request was sent...
}
});
//Now that we are ready to receive the data, send the read request
document.dispatchEvent(new CustomEvent('jspsych', {
detail:{
target: 'serial',
action: 'read',
payload: 1 //must be integer, here reads single byte if available
}
}));
Currently, only writing is supported for LPT ports. Byte value appears on the port's pins for 5ms then goes back to zero (used to send triggers to BioSemi EEG acquisition box).
payload must be integer between 0 and 255.
//send a byte value to indicate an interesting event (like a visual stimulus appearing during a psychological experiment)
document.dispatchEvent(new CustomEvent('jspsych', {
detail:{
target:'parallel',
action:'trigger',
payload: 64 //only valid byte accepted (0-255)
}
}));
Contributions and suggestions for future hardware support are more than welcome! raise an issue on this GitHub :)
- Daniel Rivas - (https://github.com/rivasd)
- Hat tip to Josh de Leeuw for the jsPsych library
- Montreal's Centre for Research on Brain Language and Music for their support.