
Бывалый

Профиль
Группа: Участник
Сообщений: 199
Регистрация: 22.5.2006
Где: Украина, Киев
Репутация: 2 Всего: 8
|
Вобщем, в итоге для работы с COM портом получилась такая штука: | Код | /** * Class for working with RS232 serial ports. * Uses ActiveX, i.e. MSIE only compatible. MSCOMM32.OCX properly installed and registered is also needed. * * Small using example: * <code> * var comm = null; * var request = 'ATX\r'; * var answer = ''; * * // Pseudo-obj creation * new COMPort(1, '9600', 'N', '8', '1', 100); * * // Needed obj creation * comm = new COMPort(1, '9600', 'N', '8', '1', 100); * * // Obj initialization * if (!comm.init()) alert(comm.Errors[comm.lastErrorIndex]); * * // Opening port * if (!comm.open()) alert(comm.Errors[comm.lastErrorIndex]); * * // Sending data in port * if (!comm.send(msg)) alert(comm.Errors[comm.lastErrorIndex]); * * // Receiving data * if (!(answer = comm.receive())) alert(comm.Errors[comm.lastErrorIndex]); * else alert('You have received: ' + answer); * * // Closing port. Don't forget abuot it! No other application can use port if it is not closed. * if (!comm.close()) alert(comm.Errors[comm.lastErrorIndex]); * </code> * * @author WIPS */
/** * Constructor. Before creating needed object you need to create pseudo-object create this function * prototype. Do like this: * <code> * // Creation of pseudo-object. No variable assign is done. * new COMPort(1, '9600', 'N', '8', '1', 100); * </code> * * @param int port_number - number of COM port (1, 2 ...) * @param int communication_speed * @param char parity - usually N * @param int data_bits_cnt - usually 8 * @param int stop_bits_cnt - usually 1 */ function COMPort(port_number, communication_speed, parity, data_bits_cnt, stop_bits_cnt) { /** * Last error index in COMPort.prototype.Errors array. * @var int */ this.lastErrorIndex = 0;
/** * COM port number. * @var int */ this.portNumber = port_number; /** * COM port speed (9600, 19200 ...). * @var int */ this.communicationSpeed = communication_speed; /** * Parity. * @var char */ this.parity = parity; /** * Number of data bits. * @var int */ this.dataBitsCnt = data_bits_cnt;
/** * Number of stop bits. * @var int */ this.stopBitsCnt = stop_bits_cnt; /** * Object from MSCOMM32.OCX created here. * @var int */ this.msComm = null; }
/** * Known errors collection. * @var array */ COMPort.prototype.Errors = new Array ( '', // 0 'Unable to initiate MSCOMM32.OCX. ActiveX error.', // 1 'Port already opened.', // 2 'Unable to open port.', // 3 'Port already closed.', // 4 'Unable to close port.', // 5 'Port is closed.', // 6 'Unable to initiate port.' // 7 ); /** * Initialazer. Run this after constructor. * We need this thing 'cause of JavaScript OOP feachures. Constructor have to return object. * This function initializes it and reports about errors. * * @return bool */ COMPort.prototype.init = function() { try { if(!(this.msComm = new ActiveXObject("MSCommLib.MSComm"))) throw "MSCommLib.MSComm"; } catch(e) { try { if(!(this.msComm = new ActiveXObject("MSCommLib.MSComm.1"))) throw "MSCommLib.MSComm.1"; } catch(e) { this.lastErrorIndex = 1; return false; } } try { this.msComm.CommPort = this.portNumber; this.msComm.Settings = this.communicationSpeed + ',' + this.parity + ',' + this.dataBitsCnt + ',' + this.stopBitsCnt; return true; } catch(e) { this.lastErrorIndex = 7; return false; } } /** * Checks if port is opened. * * @return bool */ COMPort.prototype.isOpened = function() { try { if (this.msComm.PortOpen == true) return true; else return false; } catch(e) { return false; } } /** * Closes port. * * @return bool */ COMPort.prototype.close = function() { if (!this.isOpened()) { this.lastErrorIndex = 4; return true; } this.msComm.PortOpen = false; if (!this.isOpened()) return true; else { this.lastErrorIndex = 5; return false; } } /** * Opens port. * * @return bool */ COMPort.prototype.open = function() { if (this.isOpened()) { this.lastErrorIndex = 2; return true; } this.msComm.PortOpen = true; if (this.isOpened()) return true; else { this.lastErrorIndex = 3; return false; } } /** * Receives message from port. * * @return string */ COMPort.prototype.receive = function() { var msg = ""; var isRed = false; if (!this.isOpened()) { this.lastErrorIndex = 6; return false; } var res = this.msComm.Input; return res; }
/** * Sends message in port. * * @param string msg * @return bool */ COMPort.prototype.send = function(msg) { if (!this.isOpened()) { this.lastErrorIndex = 6; return false; } this.msComm.Output = msg; return true; }
|
По поводу MsComm.CommEvent так ничего и не нашел и по сему забил  P.S. Предлоджения и замечания приветствуются... Добавлено @ 19:38 А для модема вот такая: | Код | /** * Class for working with modems connected to the COM port. Needs COMPort class to be in its scope. * Uses ActiveX, i.e. MSIE only compatible. MSCOMM32.OCX properly installed and registered is also needed. * * @author WIPS */
/** * Constructor. You can assing modem port (COM1, COM2 ...) or leave this parameter blank. * If port number isn't set, first founded modem will be used (I don't recommend to use this feature ;). * Before creating needed object you need to create pseudo-object to create this function * prototype. Do like this: * <code> * // Creation of pseudo-object. No variable assign is done. * new Modem(); * </code> * * @param int port_number - number of COM port (1, 2 ...) */ function Modem(port_number) { // Pseudo-obj creation new COMPort(1, '19200', 'N', '8', '1'); /** * Last error index in Modem.prototype.errors array. * @var int */ this.lastErrorIndex = 0; /** * Modems COM port number. * @var int */ this.portNumber = port_number; /** * Object of COMPort class created here. * @var obj */ this.comm = null; /** * There is false in this array on indexes where no-modem found at port. * @var array */ this.modemOnPort = new Array(); } /** * Max port number for modem search. * @var int */ Modem.maxComNumber = 5; /** * Timeout, ms (we need this 'cause we cant communicate with COM-port instantly. * @var int */ Modem.answerTimeout = 500;
/** * Known errors collection. * @var array */ Modem.prototype.errors = new Array ( '', // 0 'Port error.' // 1 ); /** * Commands for modem initialization. * @var array */ Modem.prototype.predialCommands = new Array ( 'AT', // don't erase this command 'X', 'E0', 'L0' ); /** * This function dials number. * * @param string number * @return bool */ Modem.prototype.dial = function(number) { if (!(this.comm.open())) { this.lastErrorIndex = 1; return false; } var command = ''; for (var i = 0; i < this.predialCommands.length; i++) command += this.predialCommands[i]; command += 'DT' + number + '\r'; this.comm.send(command); } /** * This function hangs-up phone. * * @return bool */ Modem.prototype.hangUp = function(number) { if (!(this.comm.open())) { this.lastErrorIndex = 1; return false; } this.comm.close(); } /** * Initialazer. Run this after constructor. * We need this thing 'cause of JavaScript OOP feachures. Constructor have to return object. * This function initializes it and reports about errors. */ Modem.prototype.init = function() { for (var i = 1; i <= this.maxComNumber; i++) this.modemOnPort[i] = false; // This is atempt to define modem port if its not set. if (!this.portNumber || this.portNumber == '') { for (i = 1; i <= this.maxComNumber; i++) { this.isModemOnPortRequest(i); } } // We know modem number. else { this.comm = new COMPort(this.portNumber, '9600', 'N', '8', '1'); this.comm.init(); this.modemOnPort[this.portNumber] = true; } } /** * Sending request to modem for checking its existence on this port. */ Modem.prototype.isModemOnPortRequest = function(port_number) { var com = new COMPort(port_number, '9600', 'N', '8', '1'); if (!com.init()) { this.modemOnPort[port_number] = false; return false; } if (!com.open()) { this.modemOnPort[port_number] = false; return false; } if (!com.send('ATE0\r')) { this.modemOnPort[port_number] = false; return false; } setTimeout('this.isModemOnPortAnswer(com)', this.answerTimeout); return 'maybe'; } /** * Getting answer from port. */ Modem.prototype.isModemOnPortAnswer = function(com) { var msg = com.receive(); if (!msg || msg == '') { this.modemOnPort[com.portNumber] = false; delete com; } else { this.modemOnPort[com.portNumber] = true; this.comm = com; delete com; this.comm.init(); } }
|
Если не указывать на каком порту модем, может быть найдет его сама, а может и нет  Не проверял. P.S. Предложения и замечания тоже приветствуются...
--------------------
Автопроизводители, машины, марки, модели!
|