ASP. NET uses JS to get serial data

Source: Internet
Author: User
Tags time and date

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
  1. <object classid="clsid:648a5600-2c6e-101b-82b6-000000000014" id="MSComm1" codebase= "MSCOMM32. OCX "
  2. type= "application/x-oleobject" style= "left:54px; Top:14px ">
  3. <param name="Commport" value="4"><!--set and return the communication port number. -
  4. <param name="dtrenable" value="1">
  5. <param name="handshaking" value="0">
  6. <param name="inbuffersize" value="1024x768">
  7. <param name="Inputlen" value="0">
  8. <param name="Nulldiscard" value="0">
  9. <param name="outbuffersize" value="> "
  10. <param name="Parityreplace" value="?" >
  11. <param name="RThreshold" value="1">
  12. <param name="rtsenable" value="1">
  13. <param name="Sthreshold" value="2">
  14. <param name="eofenable" value="0">
  15. <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. -
  16. <param name="databits" value="8">
  17. <param name="stopbits" value="1">
  18. <param name="baudrate" value="38400">
  19. <param name="Settings" value="38400,n,8,1">
  20. </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
  1. function Openport ()
  2. {
  3. var cmd_send = "";
  4. var result = $ (' txtreceive '). Value;
  5. var results = result.split (', ');
  6. For (var i=0;i<results.length;i++)
  7. {
  8. Cmd_send + = String.fromCharCode (eval (results[i));
  9. }
  10. if (mscomm1.portopen==false)
  11. {
  12. Mscomm1.portopen=true;
  13. Mscomm1.output=cmd_send; //Send command
  14. }
  15. Else
  16. {
  17. Window.alert ("already started receiving data!");
  18. }
  19. }
  20. function Closeport ()
  21. {
  22. if (mscomm1.portopen==true)
  23. {
  24. Mscomm1.portopen=false;
  25. }
  26. Else
  27. {
  28. Window.alert ("serial port is OFF!");
  29. }
  30. }
  31. function SendMessage ()
  32. {
  33. var cmd_send = "";
  34. var result = $ (' txtreceive '). Value;
  35. var results = result.split (', ');
  36. For (var i=0;i<results.length;i++)
  37. {
  38. Cmd_send + = String.fromCharCode (eval (results[i));
  39. }
  40. if (mscomm1.portopen==false)
  41. {
  42. Window.alert ("serial port is OFF!!");
  43. }
  44. Else
  45. {
  46. Mscomm1.output=cmd_send; //Send command
  47. }
  48. }

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
  1. Public static void Read ()
  2. {
  3. SP = new SerialPort ("COM4", 38400, Parity.none, 8, Stopbits.one);
  4. Sp. Close ();
  5. Sp. Readbuffersize = 512;
  6. Sp. Receivedbytesthreshold = 1;
  7. Sp. Open ();
  8. While (true)
  9. {
  10. Try
  11. {
  12. DataBuffer + = sp. Readexisting ();
  13. }
  14. catch (timeoutexception) {}
  15. }
  16. }

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
  1. function Gpsgetdatatimer () {
  2. Referece = "Data"; //Get GPS data
  3. $.post ("getgpsdatacomm.aspx",
  4. {Ref:encodeuri (referece)
  5. },
  6. function (data) {
  7. alert (data);
  8. });
  9. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.