Using JS to get serial data
The JavaScript language is usually a web programming language, which is more suitable for some application designs in the foreground. There are significant restrictions on the operation of local devices and data. Due to the needs of the project, I need to get the serial data of this machine and display it on the web side. We have a lot of methods in the implementation of the function, in the actual operation of the process I test two ways to obtain data. The first method is to use the ActiveX control, JS call MSCOMM32.dll serial control to control the serial port. The second method is to control the serial port using the C # language control, and then use Js+ajax to interact with C # to obtain the serial port data.
First of all, the characteristics of the two methods: Method one uses the Microsoft ActiveX control MSCOMM32.dll, the control can directly operate the serial port, and JS can also be controlled by invoking the interface. However, ActiveX controls are currently only supported for IE browsers, and I'm using chrome. Method Two at present should be more popular, C # used to do background control personal feel is still very good, and Ajax in front of the background data interaction role should also be obvious. The only disadvantage may be a slight delay in data synchronization, after all, Ajax is a standard for asynchronous transmissions.
First, I tried the method one. Implement an HTM file and a JS file. The code for the serial call is simple, and the main work in the HTM file is to add a call to the MSCOMM32.dll control.
[HTML]View Plaincopy
- <object classid="clsid:648a5600-2c6e-101b-82b6-000000000014" id="MSComm1" codebase= "MSCOMM32. OCX "
- type= "application/x-oleobject" style= "left:54px; Top:14px ">
- <param name="Commport" value="4"><!--set and return the communication port number. -
- <param name="dtrenable" value="1">
- <param name="handshaking" value="0">
- <param name="inbuffersize" value="1024x768">
- <param name="Inputlen" value="0">
- <param name="Nulldiscard" value="0">
- <param name="outbuffersize" value="> "
- <param name="Parityreplace" value="?" >
- <param name="RThreshold" value="1">
- <param name="rtsenable" value="1">
- <param name="Sthreshold" value="2">
- <param name="eofenable" value="0">
- <param name="Inputmode" value="0"><!--cominputmodetext 0 (default) via Input property to retrieve the data in text mode. Cominputmodebinary 1 uses the Input property to check the retrieval of data in binary mode. -
- <param name="databits" value="8">
- <param name="stopbits" value="1">
- <param name="baudrate" value="38400">
- <param name="Settings" value="38400,n,8,1">
- </Object>
The above code mainly set the serial number, baud rate, send and receive buffers and so on (the specific use of the Internet has a lot of instructions).
In the JS file is the main implementation of the open and close the serial port, and send and receive data operations.
[JavaScript]View Plaincopy
- function Openport ()
- {
- var cmd_send = "";
- var result = $ (' txtreceive '). Value;
- var results = result.split (', ');
- For (var i=0;i<results.length;i++)
- {
- Cmd_send + = String.fromCharCode (eval (results[i));
- }
- if (mscomm1.portopen==false)
- {
- Mscomm1.portopen=true;
- Mscomm1.output=cmd_send; //Send command
- }
- Else
- {
- Window.alert ("already started receiving data!");
- }
- }
- function Closeport ()
- {
- if (mscomm1.portopen==true)
- {
- Mscomm1.portopen=false;
- }
- Else
- {
- Window.alert ("serial port is OFF!");
- }
- }
- function SendMessage ()
- {
- var cmd_send = "";
- var result = $ (' txtreceive '). Value;
- var results = result.split (', ');
- For (var i=0;i<results.length;i++)
- {
- Cmd_send + = String.fromCharCode (eval (results[i));
- }
- if (mscomm1.portopen==false)
- {
- Window.alert ("serial port is OFF!!");
- }
- Else
- {
- Mscomm1.output=cmd_send; //Send command
- }
- }
The effect after implementation is this:
We see the text box above that makes me send the instruction, and the following text box is the data I received.
Then, I use method two. Use C # to open the serial port and use jquery's functions to communicate with C # to get current data (including current latitude and longitude, time and date). The code for C # is as follows:
[CSharp]View Plaincopy
- Public static void Read ()
- {
- SP = new SerialPort ("COM4", 38400, Parity.none, 8, Stopbits.one);
- Sp. Close ();
- Sp. Readbuffersize = 512;
- Sp. Receivedbytesthreshold = 1;
- Sp. Open ();
- While (true)
- {
- Try
- {
- DataBuffer + = sp. Readexisting ();
- }
- catch (timeoutexception) {}
- }
- }
Where SP is the serial port class SerialPort. After opening the serial port, call the Readexisting function to read the serial data.
The code to get the current data in JS is as follows:
[JavaScript]View Plaincopy
- function Gpsgetdatatimer () {
- Referece = "Data"; //Get GPS data
- $.post ("getgpsdatacomm.aspx",
- {Ref:encodeuri (referece)
- },
- function (data) {
- alert (data);
- });
- }
I make a request to the getgpsdatacomm.aspx file at a certain frequency, then get the current data and display the string. Effects such as:
Next I plan to display the string graphically in the foreground, so that the data of the serial port can be observed more intuitively.
ASP. NET uses JS to get serial data