### GEM Equipment Side Setup Example Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Demonstrates setting up the equipment side (passive) for HSMS communication and utilizing the GEM helper class. It configures equipment properties like model and software revision and handles incoming messages by replying with GEM-defined messages. Requires the 'secs4js' library. ```typescript // 1. Set up equipment side (Passive) const equipComm = new HsmsPassiveCommunicator({ ip: "127.0.0.1", port: 5000, deviceId: 1, isEquip: true, name: "Equipment", }); // Use the GEM helper class (optional) const equipGem = new Gem(equipComm); equipGem.mdln = "MyEquip"; equipGem.softrev = "1.0.0"; equipComm.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Passive received: ${msg.toSml()}`); // Reply to Host using messages defined in the Generic Equipment Model if (msg.stream === 1 && msg.func === 1) { await equipGem.s1f2(msg); } })(); }); ``` -------------------------------- ### Run Secs4js Examples from Source Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Commands to execute example scripts for the secs4js library using tsx, demonstrating its usage with different example files. ```shell pnpm dlx tsx examples/gem_example.ts # ... pnpm dlx tsx examples/.ts ``` -------------------------------- ### Install Secs4js using Package Managers Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Instructions for installing the secs4js library using various package managers like npm, pnpm, yarn, and bun. ```shell npm i secs4js pnpm add secs4js yarn add secs4js bun add secs4js ``` -------------------------------- ### SECS-I Active Serial Communication Setup (TypeScript) Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Details the setup for active SECS-I communication over a serial port. It shows how to initialize the serial communicator with port and baud rate, handle incoming messages, and send a reply to S1F1. This requires the secs4js library and a compatible serial port device. ```typescript import { A, L, Secs1SerialCommunicator, SecsMessage } from "secs4js"; async function SerialActive() { const active = new Secs1SerialCommunicator({ path: "COM5", // Serial port path baudRate: 9600, // Baud rate deviceId: 10, isEquip: false, // Whether it is equipment }); active.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Active received: ${msg.toSml()}`); if (msg.stream === 1 && msg.func === 1) { await active.reply(msg, 1, 2, L(A("MDLN-A"), A("SOFTREV-1"))); } })(); }); active.on("connected", () => { console.log("Active connected"); }); await active.open(); console.log("Active opened"); } SerialActive().catch((err) => console.error(err)); ``` -------------------------------- ### SECS-I Over TCP/IP Communication with secs4js Source: https://context7.com/lostcat-qian/secs4js/llms.txt Shows how to implement SECS-I communication over TCP/IP using Secs1OnTcpIpActiveCommunicator and Secs1OnTcpIpPassiveCommunicator. This is useful for virtual serial port setups. Ensure the IP addresses and ports are correctly configured for your network. ```typescript import { Secs1OnTcpIpActiveCommunicator, Secs1OnTcpIpPassiveCommunicator, SecsMessage, L, A } from "secs4js"; // Active side (HOST) const tcpActive = new Secs1OnTcpIpActiveCommunicator({ ip: "192.168.1.50", port: 5000, deviceId: 10, isEquip: false, timeout3: 45 }); tcpActive.on("connected", () => { console.log("SECS-I/TCP Active connected"); }); tcpActive.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Received: ${msg.toSml()}`); if (msg.stream === 2 && msg.func === 13) { await tcpActive.reply(msg, 2, 14, L(A("OK"))); } })(); }); await tcpActive.open(); // Send a message await tcpActive.send(1, 1, true, L()); // Passive side (Equipment) const tcpPassive = new Secs1OnTcpIpPassiveCommunicator({ ip: "0.0.0.0", port: 5000, deviceId: 1, isEquip: true }); tcpPassive.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Equipment received: ${msg.toSml()}`); if (msg.stream === 1 && msg.func === 1) { await tcpPassive.reply(msg, 1, 2, L( A("TCP-EQUIP"), A("v1.5.0") )); } })(); }); await tcpPassive.open(); console.log("SECS-I/TCP Passive listening"); ``` -------------------------------- ### Create HSMS Active Communicator (HOST) with TypeScript Source: https://context7.com/lostcat-qian/secs4js/llms.txt Illustrates how to establish an active HSMS (High-Speed-Message-Services) connection as a host system using 'secs4js'. It includes configuration for IP address, port, device ID, timeouts, and logging. The example also shows how to handle connection events, send and reply to SECS messages, and close the connection. Requires the 'secs4js' library. ```typescript import { HsmsActiveCommunicator, SecsMessage, L, A } from "secs4js"; const active = new HsmsActiveCommunicator({ ip: "192.168.1.100", port: 5000, deviceId: 10, isEquip: false, timeoutT3: 45, // Reply timeout timeoutT5: 10, // Connection separation timeout timeoutT6: 5, // Control transaction timeout linkTestInterval: 5000, log: { enabled: true, console: true, baseDir: "./logs", retentionDays: 30, detailLevel: "info", secs2Level: "info" } }); // Listen for connection events active.on("connected", () => console.log("TCP Connected")); active.on("disconnected", () => console.log("Disconnected")); active.on("selected", () => console.log("HSMS Selected - Ready")); // Handle incoming messages active.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Received: ${msg.toSml()}`); // Reply to S1F1 (Are You There) if (msg.stream === 1 && msg.func === 1) { await active.reply(msg, 1, 2, L(A("HOST-A"), A("SOFTREV-1"))); } // Reply to S1F13 (Establish Communications) if (msg.stream === 1 && msg.func === 13) { await active.reply(msg, 1, 14, L(A("ACK"))); } })(); }); await active.open(); await active.untilConnected(); // Wait until HSMS Selected // Send a message and wait for reply const reply = await active.send(1, 1, true, L()); console.log(`Reply: ${reply?.toSml()}`); // Send without expecting reply await active.send(6, 11, false, L(A("Event occurred"))); // Close connection when done await active.close(); ``` -------------------------------- ### SECS-I TCP/IP Passive Communication Example Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Illustrates setting up a passive SECS-I server that listens for incoming TCP/IP connections. This is useful for testing or when the equipment acts as the server. Requires the 'secs4js' library. ```typescript import { Secs1OnTcpIpPassiveCommunicator, SecsMessage, L, A } from "secs4js"; async function TcpPassive() { const passive = new Secs1OnTcpIpPassiveCommunicator({ ip: "0.0.0.0", port: 5000, deviceId: 10, isEquip: true, }); passive.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Passive received: ${msg.toSml()}`); // Process message and reply... })(); }); await passive.open(); console.log("Passive server started"); } ``` -------------------------------- ### HSMS Active Communication Setup and Message Handling (TypeScript) Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Demonstrates how to set up an active HSMS communicator, handle connection events, and process incoming messages. It includes sending a reply to S1F1 and receiving a subsequent reply. Dependencies include the secs4js library. ```typescript const active = new HsmsActiveCommunicator({ ip: "127.0.0.1", port: 5000, deviceId: 10, isEquip: false, // If you need to customize the timeout values, you can add additional parameters // timeoutT1: 10, // ... }); active.on("connected", () => console.log("Active TCP Connected")); active.on("disconnected", () => console.log("Active Disconnected")); active.on("selected", () => console.log("Active Selected (HSMS Ready)")); await active.open(); console.log("Active opened"); // Active will automatically send SelectReq and start heartbeat await active.untilConnected(); // Wait for Select success // When you need to receive and process messages, you can listen for the "message" event active.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Active received: ${msg.toSml()}`); if (msg.stream === 1 && msg.func === 1) { await active.reply(msg, 1, 2, L(A("MDLN-A"), A("SOFTREV-1"))); } if (msg.stream === 1 && msg.func === 13) { await active.reply(msg, 1, 14, L(A("ACK"))); } })(); }); const reply = await active.send(1, 1, true); console.log(`Active received reply: ${reply?.toSml()}`); ``` -------------------------------- ### HSMS Active Communicator Logging Configuration Example Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Shows how to configure logging for an HSMS Active Communicator using the 'secs4js' library. It details parameters for enabling logging, console output, storage paths, log retention, detail and SECS-II log levels, and maximum hex byte recording. Requires the 'secs4js' library and Pino for logging. ```typescript const active = new HsmsActiveCommunicator({ ip: "127.0.0.1", port: 5000, deviceId: 10, isEquip: false, log: { enabled: true, // Whether to enable logging console: true, // Whether to output logs to console baseDir: "./secs4js-logs", // Path for log storage retentionDays: 30, // Number of days to retain logs detailLevel: "trace", // Level for DETAIL logs secs2Level: "info", // Level for SECS-II logs maxHexBytes: 65536, // Maximum number of hex bytes to record }, }); ``` -------------------------------- ### SECS-I TCP/IP Active Communication Example Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Shows how to establish an active SECS-I communication link over TCP/IP. This is typically used for testing or connecting via a terminal server. It connects to a specified IP address and port. Requires the 'secs4js' library. ```typescript import { Secs1OnTcpIpActiveCommunicator, SecsMessage, L, A } from "secs4js"; async function TcpActive() { const active = new Secs1OnTcpIpActiveCommunicator({ ip: "127.0.0.1", port: 5000, deviceId: 10, isEquip: false, }); active.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Active received: ${msg.toSml()}`); // Handle message... })(); }); active.on("connected", () => { console.log("Active connected"); }); await active.open(); } ``` -------------------------------- ### SECS-I Serial Passive Communication Example Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Demonstrates setting up a passive SECS-I serial communicator. It listens for incoming messages on a specified COM port and responds to specific stream/function codes. Requires the 'secs4js' library. ```typescript import { Secs1SerialCommunicator, SecsMessage, L, A } from "secs4js"; async function SerialPassive() { const passive = new Secs1SerialCommunicator({ path: "COM5", baudRate: 9600, deviceId: 10, isEquip: true, }); passive.on("message", (msg: SecsMessage) => { void (async () => { if (msg.stream === 1 && msg.func === 1) { await passive.reply(msg, 1, 2, L(A("MDLN-A"), A("SOFTREV-1"))); } console.log(`Passive received: ${msg.toSml()}`); })(); }); await passive.open(); console.log("Passive opened"); } SerialPassive().catch((err) => console.error(err)); ``` -------------------------------- ### Create SECS-II Message Body in TypeScript Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Demonstrates the creation of a SECS-II message body using the declarative syntax provided by secs4js. This example shows how to construct a list item containing ASCII and Unsigned 1-byte integers. ```typescript import { L, A, U1, SecsMessage } from "secs4js"; const body: AbstractSecs2Item = L(A("Hello, SECS/GEM!"), U1(123)); ``` -------------------------------- ### HSMS Passive Communication Setup and Message Handling (TypeScript) Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Illustrates how to configure a passive HSMS communicator, acting as the equipment side. It shows how to handle specific SECS messages like S1F13, S1F17, and S2F17, and reply accordingly using the GEM helper class. Requires the secs4js library. ```typescript import { HsmsPassiveCommunicator, SecsMessage, CommAck, OnlAck, Gem, } from "secs4js"; // 1. Set up Equipment side (Passive) const equipComm = new HsmsPassiveCommunicator({ ip: "127.0.0.1", port: 5000, deviceId: 1, isEquip: true, name: "Equipment", }); // Use the GEM helper class (optional) const equipGem = new Gem(equipComm); equipGem.mdln = "MyEquip"; equipGem.softrev = "1.0.0"; // Handle received messages equipComm.on("message", (msg: SecsMessage) => { void (async () => { try { // S1F13: Establish Communications Request if (msg.stream === 1 && msg.func === 13) { console.log("[Equip] Received S1F13, replying S1F14..."); await equipGem.s1f14(msg, CommAck.OK); } // S1F17: Request ON-LINE else if (msg.stream === 1 && msg.func === 17) { console.log("[Equip] Received S1F17, replying S1F18..."); await equipGem.s1f18(msg, OnlAck.OK); } // S2F17: Date and Time Request else if (msg.stream === 2 && msg.func === 17) { console.log("[Equip] Received S2F17, replying S2F18..."); await equipGem.s2f18Now(msg); } else { console.log( `[Equip] Received unhandled message S${msg.stream}F${msg.func}`, ); } } catch (err) { console.error("[Equip] Error handling message:", err); } })(); }); await equipComm.open(); console.log("Passive opened and listening"); ``` -------------------------------- ### Extract Data from SECS-II Messages using secs4js Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Demonstrates how to extract data from SECS-II messages using the secs4js library. It covers parsing various data types like ASCII, numeric, booleans, and nested lists, including type assertions for TypeScript safety. The examples show direct value retrieval for single items and array access for lists. ```typescript import { A, L, Secs2ItemAscii, Secs2ItemList, Secs2ItemNumeric, SmlParser, } from "secs4js"; function getItemValue() { const body = L(A("MDLN-A"), A("SOFTREV-1")); const firstA = body?.[0] as Secs2ItemAscii; console.log("MDLN: ", firstA.value); const smlBody = ` > > > `; const smlBodySecs2Items = SmlParser.parseBody(smlBody); const zeroItem = smlBodySecs2Items?.[0] as Secs2ItemAscii; const firstU1 = smlBodySecs2Items?.[1] as Secs2ItemNumeric; const secondItem = smlBodySecs2Items?.[2] as Secs2ItemNumeric; const eighthItem = smlBodySecs2Items?.[8] as Secs2ItemNumeric; const tenthItem = smlBodySecs2Items?.[10] as Secs2ItemNumeric; const twelfthItem = smlBodySecs2Items?.[12] as Secs2ItemNumeric; const nestedList = smlBodySecs2Items?.[13] as Secs2ItemList; const nestedListFirstItem = nestedList?.[0] as Secs2ItemAscii; console.log("ASCII value: ", zeroItem.value); console.log("U1 value: ", firstU1.value); console.log("U2 value: ", secondItem.value); console.log("I8 value: ", eighthItem.value); console.log("F8 value: ", tenthItem.value); console.log("BOOLEAN value: ", twelfthItem.value); console.log("NESTED ASCII value: ", nestedListFirstItem.value); } getItemValue(); ``` -------------------------------- ### SECS-I Serial Communication with secs4js Source: https://context7.com/lostcat-qian/secs4js/llms.txt Demonstrates how to establish SECS-I serial communication using the Secs1SerialCommunicator class. This includes configuring active and passive communicators, handling messages, and managing connections. Ensure the correct serial port path and communication parameters are set. ```typescript import { Secs1SerialCommunicator, SecsMessage, L, A } from "secs4js"; // Active serial communicator (HOST) const activeSerial = new Secs1SerialCommunicator({ path: "/dev/ttyUSB0", // Linux: /dev/ttyUSB0, Windows: COM1 baudRate: 9600, deviceId: 10, isEquip: false, timeout1: 1, // Inter-character timeout timeout2: 10, // Protocol timeout timeout3: 45, // Reply timeout timeout4: 45 // Inter-block timeout }); activeSerial.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Serial received: ${msg.toSml()}`); if (msg.stream === 1 && msg.func === 1) { await activeSerial.reply(msg, 1, 2, L( A("Serial-Device"), A("v1.0") )); } })(); }); activeSerial.on("connected", () => { console.log("Serial port connected"); }); await activeSerial.open(); // Send message over serial const response = await activeSerial.send(1, 1, true); console.log(`Response: ${response?.toSml()}`); await activeSerial.close(); // Passive serial communicator (Equipment) const passiveSerial = new Secs1SerialCommunicator({ path: "/dev/ttyUSB1", baudRate: 9600, deviceId: 1, isEquip: true }); passiveSerial.on("message", (msg: SecsMessage) => { void (async () => { if (msg.stream === 1 && msg.func === 1) { await passiveSerial.reply(msg, 1, 2, L( A("EQUIP-SERIAL"), A("v2.5") )); } })(); }); await passiveSerial.open(); ``` -------------------------------- ### Use GEM Helper for Standard Messages in TypeScript Source: https://context7.com/lostcat-qian/secs4js/llms.txt Implements common GEM scenarios using high-level helpers from the secs4js library. This simplifies handling standard SEMI messages like establishing communication, going online/offline, and time synchronization. It requires the secs4js library and an initialized HSMS communicator. ```typescript import { HsmsPassiveCommunicator, Gem, CommAck, OnlAck, OflAck, TiAck, SecsMessage, Clock } from "secs4js"; // Setup equipment with GEM helper const equipComm = new HsmsPassiveCommunicator({ ip: "127.0.0.1", port: 5000, deviceId: 1, isEquip: true, name: "Equipment" }); const equipGem = new Gem(equipComm); equipGem.mdln = "EtchTool-5000"; equipGem.softrev = "2.4.1"; equipGem.clockType = "A16"; // 16-character ASCII clock format // Handle GEM messages equipComm.on("message", (msg: SecsMessage) => { void (async () => { try { // S1F1: Are You There - auto reply with model and software revision if (msg.stream === 1 && msg.func === 1) { await equipGem.s1f2(msg); } // S1F13: Establish Communications Request else if (msg.stream === 1 && msg.func === 13) { console.log("Establishing communications..."); await equipGem.s1f14(msg, CommAck.OK); } // S1F15: Request OFF-LINE else if (msg.stream === 1 && msg.func === 15) { await equipGem.s1f16(msg); // Auto sends OflAck.OK } // S1F17: Request ON-LINE else if (msg.stream === 1 && msg.func === 17) { await equipGem.s1f18(msg, OnlAck.OK); } // S2F17: Date and Time Request else if (msg.stream === 2 && msg.func === 17) { await equipGem.s2f18Now(msg); // Reply with current time } // S2F31: Date and Time Set Request else if (msg.stream === 2 && msg.func === 31) { // Extract time and set system clock... await equipGem.s2f32(msg, TiAck.OK); } // S9Fx: Error handling else if (msg.stream === 9) { console.log(`Error notification: S9F${msg.func}`); } } catch (err) { console.error("GEM message handling error:", err); // Send S9F1 (Unrecognized Device ID) if needed await equipGem.s9f1(msg); } })(); }); await equipComm.open(); console.log("Equipment GEM ready"); // Host side GEM usage // const hostGem = new Gem(hostComm); // const commAck = await hostGem.s1f13(); // Returns CommAck value // const onlAck = await hostGem.s1f17(); // Request online // const clock = await hostGem.s2f17(); // Get equipment time // await hostGem.s2f31Now(); // Set equipment time to now ``` -------------------------------- ### Create SECS-II Messages with TypeScript Helpers Source: https://context7.com/lostcat-qian/secs4js/llms.txt Demonstrates how to create SECS-II messages using helper functions in TypeScript. This includes creating simple and complex list structures, as well as various numeric data types. The output can be converted to SML format for readability. Requires the 'secs4js' library. ```typescript import { L, A, U1, U2, I4, F8, B, BOOLEAN } from "secs4js"; // Create a simple list with ASCII and numeric items const simpleMessage = L( A("Equipment Model XYZ"), U1(123) ); // Create complex nested structures const complexMessage = L( A("Status"), U1(1), L( A("Temperature"), F8(25.5, 26.3, 27.1) ), L( A("Wafer IDs"), A("W001"), A("W002"), A("W003") ), B(Buffer.from([0x20, 0x40])), BOOLEAN("TRUE") ); // Create various numeric types const numericItems = L( U1(255), // Unsigned 8-bit U2(65535), // Unsigned 16-bit I4(-1000, 2000), // Signed 32-bit array F8(3.14159265359) // 64-bit float ); console.log(complexMessage.toSml()); ``` -------------------------------- ### Send and Reply to SECS Messages using secs4js Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Illustrates how to send active messages and reply to received messages using the secs4js library. It explains the automatic generation of `SystemBytes` for sent messages and the usage of `SystemBytes` from primary messages for replies. The `send` and `reply` methods are detailed with their parameters. ```typescript active.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Active received: ${msg.toSml()}`); await active.send(2, 18, true, L()); if (msg.stream === 1 && msg.func === 1) { await active.reply(msg, 1, 2, L(A("MDLN-A"), A("SOFTREV-1"))); } if (msg.stream === 1 && msg.func === 13) { await active.reply(msg, 1, 14, L(A("ACK"))); } })(); }); ``` -------------------------------- ### Create HSMS Passive Communicator (Equipment Side) in TypeScript Source: https://context7.com/lostcat-qian/secs4js/llms.txt Sets up a passive HSMS server for the equipment side. It listens on a specified IP and port, handles incoming messages, and can send replies and unsolicited reports. Dependencies include the secs4js library. It takes configuration options like IP, port, deviceId, and timeouts. ```typescript import { HsmsPassiveCommunicator, SecsMessage, L, A, U1 } from "secs4js"; const passive = new HsmsPassiveCommunicator({ ip: "0.0.0.0", // Listen on all interfaces port: 5000, deviceId: 1, isEquip: true, timeoutT7: 10, // Not Selected timeout timeoutT8: 5, // Network inter-character timeout timeoutRebind: 5, // Rebind timeout after connection loss name: "Equipment-1" }); // Handle incoming messages from host passive.on("message", (msg: SecsMessage) => { void (async () => { console.log(`Equipment received: ${msg.toSml()}`); try { // S1F1: Equipment Status Request if (msg.stream === 1 && msg.func === 1) { await passive.reply(msg, 1, 2, L( A("EquipModel-X"), A("SW-v3.2.1") )); } // S2F13: Equipment Constant Request else if (msg.stream === 2 && msg.func === 13) { await passive.reply(msg, 2, 14, L( A("CONST1"), U1(100) )); } // Send unsolicited event report else if (msg.stream === 5 && msg.func === 1) { // After some processing... await passive.send(6, 11, true, L( U1(1), // Event ID A("Process Complete") )); } } catch (err) { console.error("Error handling message:", err); } })(); }); // Listen for connection state changes passive.on("connected", () => console.log("Host connected")); passive.on("disconnected", () => console.log("Host disconnected")); passive.on("selected", () => console.log("HSMS Selected")); await passive.open(); console.log("Equipment listening on port 5000"); // Keep running until closed // await passive.close(); ``` -------------------------------- ### Create SECS Message Instance in TypeScript Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Demonstrates the creation of a complete SECS message using the `SecsMessage` class in secs4js. It shows how to specify the stream, function, W-bit, and the SECS-II message body. ```typescript import { SecsMessage } from "secs4js"; const newMsg = new SecsMessage(1, 13, true, L(L(A("Hello World"), U1(123)))); ``` -------------------------------- ### Configure Logging for HsmsActiveCommunicator Source: https://context7.com/lostcat-qian/secs4js/llms.txt This snippet demonstrates how to enable and configure detailed logging for the HsmsActiveCommunicator in Secs4js. It allows specifying log output to the console, a base directory for log files, log retention period, and different detail levels for general logs and SECS-II messages. Logs are automatically rotated daily and old logs are cleaned up based on the retention period. ```typescript import { HsmsActiveCommunicator } from "secs4js"; const communicator = new HsmsActiveCommunicator({ ip: "127.0.0.1", port: 5000, deviceId: 10, isEquip: false, log: { enabled: true, console: true, baseDir: "./secs4js-logs", retentionDays: 30, detailLevel: "debug", secs2Level: "info", maxHexBytes: 65536 } }); await communicator.open(); // Logs will be created in: // ./secs4js-logs/detail-YYYY-MM-DD.log // ./secs4js-logs/secs2-YYYY-MM-DD.log // Log files automatically rotate daily // Old logs are cleaned up after retentionDays ``` -------------------------------- ### Create SECS-II Message Body using Factory Methods Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Illustrates creating a SECS-II message body using the `Secs2ItemFactory` provided by secs4js. This approach offers an alternative to the declarative syntax for constructing complex SECS-II items. ```typescript import { Secs2ItemFactory } from "secs4js"; // Create a message containing L, A, and U1 items const newMsg = Secs2ItemFactory.createListItem( Secs2ItemFactory.createAsciiItem("Hello World"), Secs2ItemFactory.createU1Item(123), ); ``` -------------------------------- ### Convert SECS-II Item to SML Text Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Shows how to convert a SECS-II item, created using secs4js, into its SML (SECS Message Language) text representation using the `toSml` method. This is useful for debugging and understanding message structures. ```typescript console.log(newMsg.toSml()); // Output: // // // >. ``` -------------------------------- ### Parse SML Text to SECS Messages with TypeScript Source: https://context7.com/lostcat-qian/secs4js/llms.txt Shows how to parse SML (SECS Message Language) text into SecsMessage objects using the SmlParser from 'secs4js'. It covers parsing complete messages including stream, function, and W-bit, as well as parsing only the message body. Extracted data can be accessed with type assertions. Requires the 'secs4js' library. ```typescript import { SmlParser, Secs2ItemList, Secs2ItemAscii } from "secs4js"; // Parse complete SML text with stream, function, and W-bit const smlText = "\n S1F13 W\n \n \n >\n."; const message = SmlParser.parse(smlText); console.log(message.stream); // 1 console.log(message.func); // 13 console.log(message.wBit); // true // Parse only the message body const bodyText = "\n \n \n \n >\n."; const body = SmlParser.parseBody(bodyText); console.log(body?.toSml()); // Extract data from parsed messages with type assertions const listBody = body as Secs2ItemList; const firstItem = listBody[0]; const secondItem = listBody[1] as Secs2ItemAscii; console.log(secondItem.value); // "Test Data" ``` -------------------------------- ### Extracting SECS-II Message Data with secs4js Source: https://context7.com/lostcat-qian/secs4js/llms.txt Illustrates how to parse and extract data from SECS-II messages formatted as SML strings using the SmlParser. It covers various data types including ASCII, numeric (single values and arrays), binary, boolean, and nested lists. Proper type assertions are crucial for accurate data retrieval. ```typescript import { SmlParser, Secs2ItemList, Secs2ItemAscii, Secs2ItemNumeric, Secs2ItemBoolean, Secs2ItemBinary } from "secs4js"; // Parse a complex SML message const smlBody = ` > > >. `; const body = SmlParser.parseBody(smlBody) as Secs2ItemList; // Extract values with proper type assertions const statusText = (body[0] as Secs2ItemAscii).value; console.log(`Status: ${statusText}`); // "OK" const u1Value = (body[1] as Secs2ItemNumeric).value; console.log(`U1: ${u1Value}`); // 20 (single value, not array) const u2Array = (body[2] as Secs2ItemNumeric).value; console.log(`U2 Array: ${u2Array}`); // [1000, 2000] const i4Values = (body[3] as Secs2ItemNumeric).value as number[]; console.log(`I4[0]: ${i4Values[0]}, I4[1]: ${i4Values[1]}`); // -500, 600 const f8Values = (body[4] as Secs2ItemNumeric).value as number[]; console.log(`Float values: ${f8Values.join(", ")}`); const binaryData = (body[5] as Secs2ItemBinary).value; console.log(`Binary: ${Buffer.from(binaryData).toString("hex")}`); const boolArray = (body[6] as Secs2ItemBoolean).value; console.log(`Booleans: ${boolArray}`); // [true, false, true] // Navigate nested lists const nestedList = body[7] as Secs2ItemList; const nestedText = (nestedList[0] as Secs2ItemAscii).value; console.log(`Nested text: ${nestedText}`); // "Nested" const deepList = nestedList[2] as Secs2ItemList; const deepText = (deepList[0] as Secs2ItemAscii).value; console.log(`Deep text: ${deepText}`); // "Deep" ``` -------------------------------- ### Import SECS-II Data Types in TypeScript Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Imports necessary data type constructors from the secs4js library for building SECS-II messages. These types represent various SECS-II data formats like Binary, Unsigned Integers, Floating Point, and ASCII. ```typescript import { B, BOOLEAN, U1, U2, U4, U8, I1, I2, I4, I8, F4, F8, A, L, } from "secs4js"; ``` -------------------------------- ### Parse Complete SML Text to SecsMessage Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Utilizes the `SmlParser` from secs4js to parse a complete SML string, including stream, function, and body, into a `SecsMessage` object. It then extracts and logs specific details from the parsed message. ```typescript import { SmlParser } from "secs4js"; // Complete SML text const sml = "\n S1F13 W\n \n \n >."; // Parse complete SML text into a SecsMessage instance using the parse method const parsedMessage = SmlParser.parse(sml); const firstBodyItem = parsedMessage.body instanceof Secs2ItemList ? parsedMessage.body.value[0] : null; const bytes = firstBodyItem instanceof Secs2ItemBinary ? firstBodyItem.value : null; console.log( parsedMessage.stream, parsedMessage.func, parsedMessage.wBit, bytes, ); ``` -------------------------------- ### Parse SML Body Text to Secs2Item Source: https://github.com/lostcat-qian/secs4js/blob/main/README.md Demonstrates using the `SmlParser.parseBody` method from secs4js to parse an SML string containing only the message body into an `AbstractSecs2Item`. The resulting item can then be converted back to SML. ```typescript import { SmlParser } from "secs4js"; // SML text containing only the message body const smlBody = "\n \n \n >."; // Parse SML text containing only the message body into an AbstractSecs2Item instance using the parseBody method const parsedBody = SmlParser.parseBody(smlBody); console.log(parsedBody?.toSml()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.