JS Serial communication call MSCOMM32 Control link Electronic scale (full version implementation plan)

Source: Internet
Author: User

Hardware Environment : RS232 to USB Serial cable *

Electronic scale * * (I use g&g e600y-c type known as heavy instrument)

Electronic scale original RS232 data line * *

Computer * *

Software Environment : RS232 to USB serial cable drive (this can be downloaded from the serial line manufacturer's website, I use Unitek production)

Windows7 (32-bit)

MSCOMM32 control (This can be found on the web)

Implementation Steps :1, installing the MSCOMM32 control

2, install RS232 to USB serial cable drive

3, Link hardware (note here: Need to first link the original serial cable, and then use RS232 to USB serial cable link original serial line and computer)

4, Start coding

Coding Details:

1, initialize the MSCOMM32 control with JS

$ (function () {
Initactivex ();//Initialize control methods
})
function Initactivex () {
try {
msComm1 = new ActiveXObject ("mscommlib.mscomm.1");//Initialize MSComm control
}catch (err) {
Console.log (err);//initialization failed, print error
}
if ((typeof (MSCOMM1) = = "undefined") | | (MSCOMM1 = = null)) {//Uninitialized success
alert ("MSCOMM1 is null");
return false;
}else{//initialization succeeded
Configport ();//Configure Port information
Searchandopenport ();//auto-search port and open ports
return false;
}
}
2, configure serial port information
function Configport () {//Configure Port Method
mscomm1.settings = ' 9600,n,8,1 ';//9600: Baud rate N: Parity bit 8: Data bit 1: Check bit   
mscomm1.outbuffercount = 0;//empty send buffer
mscomm1.inbuffercount = 0;//empty Receive buffer
mscomm1.rthreshold=14;//This parameter is important, this parameter is configured to match the hardware device This parameter indicates how many bits of data are received by the hardware device to trigger the OnComm event   
}
3, automatically search the computer serial port and open the serial port (if the computer connected two hardware, reference ideas can be)
automatic search Computer serial port idea : The general computer only up to 16 serial ports, in turn, traverse all the serial port, and send messages to the serial port (I use the device: requires the weighing instrument to send a data,
Send the decimal "27112" Can), if received data, then that the serial port is we need to look for the serial port.
var com = 0;//port variable
var res = ";//Receive Hardware
function Searchandopenport () {
if (Mscomm1.portopen = = True) {
Togggleport ();//Close or open port
}
if (com > 16) {//General PC only 16 ports
com = 0;
alert ("Not connected to the weighing instrument!")
return;
}
if (res! = ") {//RES is the data that receives the weighing meter
mscomm1.commport = com-1;//Set serial port for the search to the serial port
Togggleport ();
return;
}
mscomm1.commport = com;//set up the serial port
var B = togggleport ();//Open the serial port and return to the open result
if (b) {
Send (' 27,112 ');//transmit information to the serial port
}
COM + = 1;
setTimeout (' Searchandopenport () ', +);// interval 500ms waits for data to be received and implemented loops, if not interval, is affected by the order of code execution and will not receive data
}
4. Open/close serial port
Description:
Mscomm1.portopen = True; indicates that the serial port is turned off if it equals false
var Statu = Mscomm1.portopen Indicates the status of the current switch on the serial port; statu = = True indicates current on; Statu = = False indicates the current shutdown
function Togggleport () {
if (Mscomm1.portopen = = False) {
try {
Mscomm1.portopen = true;//Open serial port
bindevent ();//Open serial port and then bind serial transceiver Events
return true;
}catch (ex) {
Console.log (ex);
return false;
}
}else{
try {
Mscomm1.portopen = false;//Turn off the serial port
return true;
}catch (ex) {
alert (ex.message);
return false;
}
}
}
5, receive/Send serial message event bindings
var bindevent = function () {
function Mscomm1::oncomm () {
mscomm1_event ();//The processing method after triggering the sending and receiving event
}
}
6, Serial transceiver Events
function Mscomm1_event () {
//console.log (mscomm1.commevent);//This is the flag that is returned after triggering the send and Receive Event 1: indicates the sending;
//2: Accept
//Other identification line information (wire change specific information needs to refer to MSComm API)
if (mscomm1.commevent = = 1) {
alert ("Send ok! ");
}else if (mscomm1.commevent = = 2) {
receive ();//Receiver serial information method
}
}
7, Receive serial information method
function Receive () {
var inputvalue = mscomm1.input;//Receive serial information
///Console.log ("Input:::" + inputvalue);
if (Inputvalue.indexof ("G") >0) {//If the return data contains G
res = $.trim (Inputvalue.split ("G") [0]);//Get weight and assign value to res
}
}
8. Send a message to the serial port
Note: The serial port can not accept the decimal data, and JS does not have a char type of data, here we use the String.fromCharCode () method to convert the decimal data to char type
function Send (str) {
var cmd_send = ';
try{
var results = str.split (', ');
For (Var i=0;i<results.length;i++) {
Cmd_send + = String.fromCharCode (Results[i]);//decimal to char
}
mscomm1.output=cmd_send;//Send messages to the serial port
}catch (ex) {
Console.log (ex.message);
}
}
9, click to get the weighing instrument data method
function Getweight () {
if (com = = ' 0 ') {
alert ("Not connected to the weighing instrument!")
}else{
send (' 27,112 ');//sends a request to the serial port
setTimeout ("Auto_weight_once ()", "()", "//// 200ms after receiving data, here set 200ms and then get it is necessary, this is the execution order problem
}
}
function Auto_weight_once () {
$ ("#weight"). Val (res);//Fill the retrieved data to the page
}
The method of continuous output data of the weighing instrument
function Auto_weight () {
//if (!continue_auto_weight) {//This is the end of the continuous output flag, as to when the end, depending on the project
//return;
//}
$ ("#weight"). Val (res);
send (' 27,112 ');
setTimeout ("Auto_weight ()", +);
}
Summary: About Goole How to connect the weighing instrument, there is time to write the post.
finally: If it does not solve the problem, then it may be the security settings of IE browser problem, this online a lot of reference, here do not repeat.

JS Serial communication call MSCOMM32 Control link Electronic scale (full version implementation plan)

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.