how to transfer binary data through serial port on javascript

4
http://strokescribe.com/en/serial-port-javascript-binary-data- transfer.html How to transfer binary data through serial port on Javascript An example of how to transfer binary data from javascript using byte arrays. The javascript code sample (see below) uses StrokeReader serial port ActiveX and will work with any RS242 and RS485 ports, including USB serial adapters. To run the sample code, copy it into .hta file. Some notes on this code The SetPort() function sets the data exchange parameters. 9600-n- 8-1 are the most popular settings for reading data from barcode scanners and weighting machines. Please make sure you have set the correct serial port number . The port.attachEvent() call assigns the event handler function for serial port events . The CommEventHandler function processes the serial port event notifications and displays incoming data in the textarea field. Additionally, it displays the current state of CTS/DSR serial port lines. DataMode=2 switches the ActiveX to JS/BINARY reception mode and make data arrays passed to CommEventHandler compatible with javascript (JS does not accept "classic" byte SAFEARRAYs). This mode is available in StrokeReader starting from v1.3. Please carefully check all data you are sending in SendData() . The Send() call can accept any values, but they will be trimmed to bytes. If your array looks like (0x100, 0x2FF, 0x12345), bytes (0x00, 0xFF, 0x45) will be sent to the serial port.

Upload: balabooks

Post on 28-Dec-2015

83 views

Category:

Documents


1 download

DESCRIPTION

transfer data

TRANSCRIPT

Page 1: How to Transfer Binary Data Through Serial Port on Javascript

http://strokescribe.com/en/serial-port-javascript-binary-data-transfer.html

How to transfer binary data through serial port on Javascript

An example of how to transfer binary data from javascript using byte arrays. The javascript code sample (see below) uses StrokeReader serial port ActiveX and will work with any RS242 and RS485 ports, including USB serial adapters.To run the sample code, copy it into .hta file.

Some notes on this code

The SetPort() function sets the data exchange parameters. 9600-n-8-1 are the most popular settings for reading data from barcode scanners and weighting machines. Please make sure you have set the correct serial port number.

The port.attachEvent() call assigns the event handler function for serial port events.The CommEventHandler function processes the serial port event notifications and displays incoming data in the textarea field. Additionally, it displays the current state of CTS/DSR serial port lines.

DataMode=2 switches the ActiveX to JS/BINARY reception mode and make data arrays passed to CommEventHandler compatible with javascript (JS does not accept "classic" byte SAFEARRAYs). This mode is available in StrokeReader starting from v1.3.

Please carefully check all data you are sending in SendData(). The Send() call can accept any values, but they will be trimmed to bytes. If your array looks like (0x100, 0x2FF, 0x12345), bytes (0x00, 0xFF, 0x45) will be sent to the serial port.

<html><head><script type="text/javascript"> var port; //A reference to serial port ActiveX object //Serial port initialization procedure, called from <body> onload= function SetPort() { port = document.getElementById("comport"); //See <object> in <body>

//CommEventHandler will handle serial port events port.attachEvent('CommEvent', CommEventHandler);

port.Port=8; //!Please check the serial port number!

Page 2: How to Transfer Binary Data Through Serial Port on Javascript

http://strokescribe.com/en/serial-port-javascript-binary-data-transfer.html

port.BaudRate=9600; port.DataBits=8; port.Parity=0; //NOPARITY port.StopBits=0; //one stop bit

port.DataMode=2; //BINARY data, passed in JS-compatible byte array

port.Connected=1; //Connects to the serial port

if(port.Error>0) //Display errors alert(port.ErrorDescription); }//End of SetPort

//This will be called if serial data received or CTS/DSR line states are changed function CommEventHandler(Evt, Data) { if(Evt==3) //=EVT_CONNECT { connected.checked=true; } if(Evt==0) //=EVT_DISCONNECT { connected.checked=false; } if(Evt==2) //EVT_SERIALEVENT { if(Data&0x0008) //"CTS is changed" bit is set in event flags { cts.checked=port.CTS; //Mirror the CTS line state in checkbox } if(Data&0x0010) //Checking DSR flag { dsr.checked=port.DSR; } } if(Evt==1) //=EVT_DATA { var a = new VBArray(Data).toArray(); for (i = 0; i<a.length; i++) //Prints each received byte into textarea in hexadecimal form output.value=output.value+ " " + a[i].toString(16); } }//End of CommEventHandler

//Sends some binary data to serial port. Called by <button> in <body> function SendData() { //Replace this array with your data s=new Array(0x00, 0x20, 0x80, 0xC0, 0xFF); port.Send(s); }//Changes the state of RTS line. Called by checking/unchecking of the "rts" checkbox, see <body>function RTS_click(){

Page 3: How to Transfer Binary Data Through Serial Port on Javascript

http://strokescribe.com/en/serial-port-javascript-binary-data-transfer.html

port.RTS = rts.checked;}//Changes the state of DSR line.function DTR_click(){ port.DTR = dtr.checked;}</script></head>

<body onload="SetPort()"> <object classid="CLSID:7E42B8C5-73BE-4806-8904-FF4080A696EC" id="comport"></object>

<button onclick="javascript:SendData()">Send</button> <input type="checkbox" name="rts" onClick="RTS_click()">RTS</input> <input type="checkbox" name="dtr" onClick="DTR_click()">DTR</input> <br> <input type="checkbox" disabled="disabled" name="connected">Connected</input> <input type="checkbox" disabled="disabled" name="cts">CTS</input> <input type="checkbox" disabled="disabled" name="dsr">DSR</input> <br> <textarea name="output" rows=10 cols=50></textarea></body></html>

How the application looks like: