0% found this document useful (0 votes)
66 views

ATOP ABLELinkTM Serial Server GW21x Series Software Development Kit

This document provides a programming guide for the GW21x series SDK (Software Development Kit). It includes information on installing the SDK, the development environment, locating application programs on the GW21x serial server, and the serial server kernel process flow. It also provides an overview of the SDK APIs for general functions, serial I/O, audio/LED, TCP/IP, memory, timers, watchdog timer, digital I/O, time functions, accounts, and wireless LAN commands.

Uploaded by

Najeeb Al-Ahwas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

ATOP ABLELinkTM Serial Server GW21x Series Software Development Kit

This document provides a programming guide for the GW21x series SDK (Software Development Kit). It includes information on installing the SDK, the development environment, locating application programs on the GW21x serial server, and the serial server kernel process flow. It also provides an overview of the SDK APIs for general functions, serial I/O, audio/LED, TCP/IP, memory, timers, watchdog timer, digital I/O, time functions, accounts, and wireless LAN commands.

Uploaded by

Najeeb Al-Ahwas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

GW21x series SDK Guide

ATOP ABLELinkTM Serial Server


GW21x series
Software Development Kit

Programming Guide
Version 1.61

Updated at: 2006/11/28

ATOP Technologies, Inc.


Tel: 886-3-5508137
Fax: 886-3-5508131
E-mail: [email protected]

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Revision History

2003/10/20: Created
2004/05/14: Update ComConfig(), add mark and space parity.
2006/07/31: Update SetNetworkType(), using 4 to configure as ad-hoc mode, but not 3.
2006/11/28: Update the description of return value of GetSerialData().
2006/11/28: Remove ReadMacEEPROM() and WriteMacEEPROM(), because there are more user
friendly functions can be used. e.g. GetLocalIpAddr(), SetLocalIpAddr(),
GetLocalNetmask(), SetLocalNetmask(),..etc .
2006/12/21: Add SetDefaultKeyProc() to provide a customized default key process.
Add Read_ApState(), Getnewporttype(), Setnewporttype(), Setnewporttype2()
2007/02/13: Remove Figure 1.2 GW21x-MAXI Front View. because it is GW21S-256 front view.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Contents
1. Introduction....................................................................................................................................8

1.1 Packaging...................................................................................................................................8
1.2 Product Description ...................................................................................................................8

2. Installation ......................................................................................................................................9

2.1 Development Environment .........................................................................................................9


2.2 ABLELinkTM Serial Server SDK Installation..............................................................................9
2.3 ABLELinkTM SDK Package Content ........................................................................................10

3. Development Description ............................................................................................................13

3.1 AP development flow................................................................................................................13


3.2 Locate Application Program on ABLELinkTM Serial Server....................................................15
3.3 Process Flow of Serial Server Kernel......................................................................................16

4 Advance Course.............................................................................................................................18

4.1 Simulation on PC .....................................................................................................................18


4.1.1 Simulation Introduction ....................................................................................................18
4.1.2 Differences Between Serial Server and PC.......................................................................18
4.2 Download Multiple Sections ....................................................................................................19

5. API Overview................................................................................................................................20

5.1 General Function Overview.....................................................................................................20


5.2 Serial I/O Functions Overview ................................................................................................20
5.3 Audio & LED Function Overview............................................................................................21
5.4 TCP/IP Function Overview......................................................................................................21
5.5 Memory Function Overview.....................................................................................................23
5.6 Timer Interrupt Handling Function Overview .........................................................................23
5.7 Watch Dog Timer Overview .....................................................................................................23
5.8 Advanced Serial Port API (in comapi.obj) ..............................................................................24
5.9 Digital Input and Digital Output Overview..........................................................................24
5.10 Time Functions Overview.......................................................................................................25
5.11 Account Overview...................................................................................................................25
5.12 Wireless LAN Command Overview ........................................................................................25

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

6. API Reference ...............................................................................................................................27

6.1 General Functions....................................................................................................................27


GetIntrVector() ...........................................................................................................................27
SetIntrVector()............................................................................................................................27
GetVersion() ...............................................................................................................................28
GetAPSerialNo()........................................................................................................................28
SetAPSerialNo() ........................................................................................................................29
GetGwModel() ...........................................................................................................................29
GetConfigData().........................................................................................................................30
GetConsoleSta() .........................................................................................................................31
SYSTEM_RESET() ...................................................................................................................31
BatteryLowDetect() ...................................................................................................................31
SetDefaultKeyProc()..................................................................................................................32
Read_ApState ().........................................................................................................................33
6.2 Serial I/O Functions.................................................................................................................33
InitSerialMode().........................................................................................................................33
SendSerialData() ........................................................................................................................34
GetSerialData() ..........................................................................................................................35
GetSerialStatus() ........................................................................................................................35
EnableSerialHWintr() ................................................................................................................36
DisableSerialHWintr() ...............................................................................................................36
EnableRS485rx()........................................................................................................................37
EnableRS485tx()........................................................................................................................37
SerialIntrEOI() ...........................................................................................................................37
Getnewporttype() .......................................................................................................................38
Setnewporttype()........................................................................................................................38
Setnewporttype2()......................................................................................................................39
Getporttype()..............................................................................................................................39
Get485type() ..............................................................................................................................39
Set485type() ...............................................................................................................................40
GetTotalPort() ............................................................................................................................41
GetPhyState() .............................................................................................................................41
6.3 Audio & LED Functions ..........................................................................................................41
SetupProcessLed() .....................................................................................................................41
BuzzerOn().................................................................................................................................42
SetupComLed()..........................................................................................................................42
6.4 TCP/IP Functions.....................................................................................................................42
tcp_connect()..............................................................................................................................43
tcp_listen() .................................................................................................................................44
tcp_disconnect() .........................................................................................................................45
tcp_send()...................................................................................................................................45
tcp_receive()...............................................................................................................................46
tcp_getdata()...............................................................................................................................47
tcp_getsockopt().........................................................................................................................48
tcp_setsockopt() .........................................................................................................................49
udp_open() .................................................................................................................................50
ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

udp_close().................................................................................................................................51
udp_receive() .............................................................................................................................51
udp_receiveX()...........................................................................................................................52
udp_send()..................................................................................................................................53
udp_reset() .................................................................................................................................54
GetLocalIPAddr().......................................................................................................................54
GetLocalEthAddr() ....................................................................................................................55
GetDefaultGateway() .................................................................................................................55
GetLocalNetmask()....................................................................................................................56
GetHostName() ..........................................................................................................................56
GetDhcp()...................................................................................................................................57
GetConnectStatus() ....................................................................................................................57
SetLocalIPAddr() .......................................................................................................................59
SetDefaultGateway()..................................................................................................................60
SetLocalNetmask().....................................................................................................................60
SetHostName()...........................................................................................................................61
6.5 Memory Functions ...................................................................................................................61
EraseSector()..............................................................................................................................61
ProgOneByte() ...........................................................................................................................62
ProgString()................................................................................................................................63
ReadEEPROM().........................................................................................................................64
WriteEEPROM()........................................................................................................................64
6.6 Watch Dog Timer Functions.....................................................................................................65
WatchDogCtrl()..........................................................................................................................65
ClrWatchDogTime()...................................................................................................................65
SetupWatchDogTime()...............................................................................................................66
6.7 Advanced Serial Port API (in comapi.obj) ..............................................................................66
Com_Config() ............................................................................................................................66
Com_GetBuf() ...........................................................................................................................67
Com_PutBuf()............................................................................................................................67
Com_ClearRxQue() ...................................................................................................................68
Com_ClearTxQue() ...................................................................................................................68
Com_TxQueIsEmpty() ..............................................................................................................69
Com_DTR_Active()...................................................................................................................69
Com_RTS_Active() ...................................................................................................................70
Com_DSR_Status()....................................................................................................................70
Com_CTS_Status() ....................................................................................................................70
Com_RI_Status() .......................................................................................................................71
Com_DCD_Status() ...................................................................................................................71
6.8 Digital Input and Digital Output Functions .........................................................................71
SetIOMode() ..............................................................................................................................71
GetIOMode()..............................................................................................................................72
SetOutState() ..............................................................................................................................72
GetInState()................................................................................................................................72
ReadDigitalInput() .....................................................................................................................73
SetupRelayOutput() ...................................................................................................................73

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

6.9 Time Functions.........................................................................................................................73


ReadRTCTime().........................................................................................................................73
ReadRTCDate()..........................................................................................................................74
WriteRTCTime() ........................................................................................................................74
WriteRTCDate().........................................................................................................................74
EnableWRProtect() ....................................................................................................................75
DisableWRProtect() ...................................................................................................................75
GetStartupTime() .......................................................................................................................75
GetSysMsec().............................................................................................................................76
GetSysTick() ..............................................................................................................................76
GetSysSec()................................................................................................................................77
GetSysTime() .............................................................................................................................77
6.10 Account Functions..................................................................................................................77
AddAccount().............................................................................................................................77
DelAccount()..............................................................................................................................78
VerifyAccount() .........................................................................................................................78
6.11 Wireless LAN Command Functions........................................................................................79
GetESSID() ................................................................................................................................79
SetESSID().................................................................................................................................79
GetFrame() .................................................................................................................................80
SetFrame()..................................................................................................................................80
GetFragment()............................................................................................................................80
SetFragment().............................................................................................................................80
GetChannel()..............................................................................................................................81
SetChannel() ..............................................................................................................................81
GetNetWorkType().....................................................................................................................81
SetNetWorkType() .....................................................................................................................82
GetTransmitRate()......................................................................................................................82
SetTransmitRate() ......................................................................................................................82
GetXI300WEPFlag() .................................................................................................................82
SetXI300WEPFlag() ..................................................................................................................83
GetXI300WEPBit()....................................................................................................................83
SetXI300WEPBit() ....................................................................................................................84
GetXI300WEPKey()..................................................................................................................84
Set XI300WEPKey()..................................................................................................................84
GetRFRealChannel()..................................................................................................................84
GetRFTXRate()..........................................................................................................................85
GetAPName().............................................................................................................................85
GetAPMac() ...............................................................................................................................85
LanRestart() ...............................................................................................................................86
WGetMacStatus().......................................................................................................................86
WGetBssQuality()......................................................................................................................86
GetCountry() ..............................................................................................................................87
GetLegalChannel().....................................................................................................................87
WscanRequest() .........................................................................................................................88
WscanReady()............................................................................................................................89

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Appendix A: Serial port configuration table .................................................................................91

Appendix B. Upgrade System Software .........................................................................................92

B.1 Upgrade Procedures................................................................................................................92


B.2 Critical Issues of Upgrading ...................................................................................................93
B.3 Error Messages........................................................................................................................93

APPENDIX C:SAMPLE PROGRAM ........................................................................................95

C.1 TCP/IP sample program..........................................................................................................95


C.2 Serial port sample program.....................................................................................................99

Appendix D. Configuration Utility ...............................................................................................101

D.1 Run the utility ........................................................................................................................101


D.2 Detecting Operational Controllers and Devices...................................................................102
D.3 Configuring Controllers and Devices ...................................................................................102

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

1. Introduction
1.1 Packaging
GW21x-MAXI ABLELinkTM Embedded Serial Server x 1.
Product CD containing Software Development Kit (SDK).

1.2 Product Description


The ATOP ABLELinkTM Embedded Serial Server is a powerful, low-cost and compact unit. The
Embedded Serial Server offers one RS-232/ RS-485 port, one Ethernet (100BaseT), one Watch Dog
Timer and etc. With these I/O modules and a customized application program, the Embedded Serial
Server becomes a powerful device for communication controls and monitoring.

A DOS-like development toolkit is provided for developing and downloading applications from a PC on
the network to the Serial Server through Ethernet. With the built-in TCP/IP engine, the Serial Server can
then communicate local area network directly. In versatile applications, it can function as a gateway
between field devices and cell controller. By using the RS-232/RS-485 ports, it functions as a gateway
between field bus and Ethernet.

The development toolkit is ATOP ABLELinkTM Embedded Serial Server SDK. It contains a set of API
library, sample programs, development utilities, a download utility and a configuration utility. You can
develop an application program in a PC-based environment. After you develop the application program,
you can download the application program to the Serial Server. Then the Serial Server can work as you
wish.

The Serial Server Software Development Kit (SDK) is a PC-based software package for developing
specific applications on the Serial Server. There are two important types of applications.
The application that handles the TCP/IP communication protocol and data exchange between the
Serial Server and a host computer.
The application that handles the RS-232/RS-485 communication protocol and data exchange
between the Serial Server and field devices.

The system architecture may be like Figure 1.1.

Figure 1.1 System Architecture of GW21x-MAXI Applications

The GW21x-MAXI is a generic term for ATOP Embedded ABLELinkTM Serial Servers. About detailed
hardware information please refer to each model’s user manual. This document only describes how to
develop an application been run in GW21x-MAXI.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

2. Installation
2.1 Development Environment
The application program of the Serial Server is developed in the PC environment. After the application
program is developed and ready, you can download it to the Serial Server. The Serial Server with the
specific application program will be ready the next time the power is turned on.

During application development stage, you may simulate the application program in the PC environment.
If the simulation is OK, you can compile and link the application again for the Serial Server and then
download the application program to the Serial Server to perform test on it.

The developing environment is as followings:

Development environment: DOS, Win95, Win98, NT, Win Me, Win2000


Simulation environment: DOS, Win95, Win98
Development tools: Please see the following table.

Tool Vendor Remark


MSC 6.00A MicrosoftTM 16 bits Compiler, *note 1.
MSC 7.X
MSVC 1.X
MASM 5.X or later MicrosoftTM
Locate Utility System & Software, Inc
Serial Server API ATOPTM Serial Server Library
Download Utility ATOPTM Download AP
Packet Driver LAN card Maker Simulation only, see*note 2.
TCP/IP for MS-DOS ATOPTM Simulation only, see *note 2.
Table 2-1 Development Tools

Note:
1. These compilers have been tested and found fully compatible with the ATOP ABLELinkTM Serial
Server. The final version is MSVC 1.52.
2. For debugging and simulation purpose. Please refer to section 4.1 for more details.

2.2 ABLELinkTM Serial Server SDK Installation

Step 1:Copy all the files to local disk


MD C:\GW21S2 <ENTER>
CD C:\GW21S2 <ENTER>
XCOPY CD-ROM Drive: *.* /S <ENTER>

Step 2:Rebuild MSC library

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

CD C:\GW21S2\NEWLIB <ENTER>
Edit “MEMU.BAT” to select MSC6.0,MSC7.0,or later and model.
Edit “MAKEFILE” to set the 'SRC' field to the correct path name of MSC library.
MEMU <ENTER>
COPY C:\GW21S2\NEWLIB\SSILCE.LIB C:\GW21S2\LIB

Step 3:Installation OK
You can now start creating your applications based on SDK sample program.

Note:
The host (PC) AP can use the standard MSC library, but the ABLELinkTM Serial Server AP can
only use the rebuilt MSC library resides for example in the “C:\GW21S2\NEWLIB” directory.

2.3 ABLELinkTM SDK Package Content

Directory File Name Descriptions


… \BIN\ DAPTCPIP.EXE :TCP/IP driver for MS-DOS
HOST.CFG :accessory file of DAPTCPIP.EXE, it is the
TCP/IP configuration file for MS-DOS
PC_TCPIP_START.BAT :a batch file to startup TCP/IP driver in PC
PC_TCPIP_STOP.BAT :a batch file to stop and remove TCP/IP driver in
PC
EXE2OMF.EXE :locate utility for converting .EXE file to .OMF
file
XOH86.EXE :accessory utility of location, it converts .OMF
file to .HEX file
DOS4GW.EXE :accessory file for locate utility
…\LIB\ DAPAPI.LIB :API library of GW21x-MAXI, including BIOS,
DOS, TCP/IP, … API functions
STARTUP1.OBJ :application startup program, must be included
and be the first object file when linking to
execution file
STARTUP2.OBJ :application startup program, must be included
and be the first object file when linking to
execution file
The "STARTUP1.OBJ" does not support floating
operation and changing stack size (always 4 kb).
Using "STARTUP2.obj" and "stackf.asm"(in
"source\" directory) to solve these 2 problems.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

If you want to use floating operation or change


the stack size, please use the "STARTUP2.obj" to
replace the "STARTUP1.obj" and then edit the
"stackf.asm".
(Note: Update "STACK_SIZE" and "FLOAT"
symbol in "makefile" are useless. Please update
"stackf.asm" directly.)
COMAPI.OBJ :advanced serial port API, using hardware
interrupt service routine to send and receive serial
port data
COMALO.OBJ :accessory file of COMAPI.OBJ, to tune the size
of send and receive buffer in this file
…\INC\ GW_API.H :a generic include file, it contains following
include files. You can just only include this file
dapapi.h :type and constant definition and declare
functions of BIOS, DOS API
TCPAPI.H :declare functions of TCP/IP API
TCPDEF.H :type and constant definition of TCP/IP API
COMAPI.H :declare functions of serial port API
…\NEWLIB\ EMINIT.OBJ :for 8087 emulation mode library
MAKEFILE :make file for re-building MSC library
MEMU.BAT :batch file for re-build MSC library
REMOVE.C6 :input file for re-building MSC 6.00A library
REMOVE.C7 :input file for re-building MSC 7.00 or later
library
…\SOURCE\ COM.MAK :a makefile to compiler advanced serial port API,
after compiler you should copy object file to the
“…\LIB\” directory
COMAPI.C :advanced serial port API, using hardware
interrupt service routine to send and receive serial
port data
COMALO.C :accessory file of COMAPI.C, to tune the size of
send and receive buffer in this file
TIMER.C :to create and get system timer counted from
startup
STACKF.ASM :using this file to change stack size
… \SAMPLE\ \VB_TCP\ :a VB TCP sample program, in host side (PC)
\WINSOCK_TCP\ :a C TCP sample program, in host side (PC)

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

\GW21_CMD\ :R/W flash memory sample program


\GW21_COM\ :a serial port sample program
\GW21_COM2\ :a serial port sample program for RS485(2 wires)
\GW21_CONNECT\ :a TCP client sample program
\GW21_FUNC\ :an API sample program, include LED control,
get system time and watchdog control
\GW21_Listen4\ :a TCP server sample program (4 connections)
\GW21_FLOAT\ :a sample program to calculate floating data
\GW21S3_TCP&COM\ :a TCP server and serial port sample program
\GW21S3_TCP&COM2\ :a TCP server and serial port sample program
with communication handshaking
… \CONFIG\ MONITOR.EXE :a configuration utility for GW21x-MAXI
SEG.CFG :accessory file of MONITOR.EXE
… \DOC\ GW21x-MAXI SDK :a SDK document of GW21x-MAXI
Reference Guide.doc
… GWDL.EXE :a download firmware utility for GW21x-MAXI
\DOWNLOAD\
DAPDL.CFG :accessory file of GWDL.EXE
DOWNLOAD.BAT :a batch file to download firmware to
GW21x-MAXI

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

3. Development Description
3.1 AP development flow
Before developing an application program, please install Microsoft C 6.0 or above (16 bits complier)
and GW21x-MAXI SDK. For more information, you can refer chapter 2.
The process flow of developing an application program is as following:

AP
C.ASM

COMPILER

All related APIs AP MSC


.OBJ .OBJ .LIB

LINK

AP
USERLOC.CMD .EXE

EXE2HEX

AP
.OMF

XOH86

AP
.HEX

DOWNLOAD
to
GW21S-512

Figure 3-1 Program Flow Diagram

Step 1. Setup environment parameters


1. Run the batch file to setup Microsoft C environment parameters. Then you can compiler and
link application program. The batch file is provided in Microsoft compiler. For example, it
may be “msc6.bat” in Microsoft C compiler \bin directory. To run the batch file may be like

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

following:

2. Append the SDK \BIN directory to the system environment parameter: PATH. Then you can
use SDK utilities in any directory.

Step 2. Write your application program


You can write your application program by any text editor.

Step 3. Compile your application program


After writing application program, you can compile the program. The C compiler option
arguments are ”/AL /Zi /Gs /c /Oi /I. /Fpi”. For example, to compile ‘userap.c’ is as following:
cl /AL /Zi /Gs /c /Oi /I. /Fpi userap.c.

Step 4. Link your application program


After compiling application program, you can link the program with other API library (maybe .obj
or .lib) to generate an execution file (.exe). The link option arguments are ”/NOD /NOE”. For
example, to link ‘userap.obj’ is as following:
link /NOD /NOE @userlnk.cmd.
The ‘userlnk.cmd’ is a text file to include all object files and library files which would be linked
together. It may be like following:
..\..\LIB\S_G21S3+USERAP,
USERAP, USERAP, ..\..\LIB\DAPAPI.LIB+..\..LIB\SSILCE.LIB, ,
Note:
1. The S_G21S3.OBJ, DAPAPI.LIB can be found in SDK \LIB directory.
2. The S_G21S3.OBJ must be the first object file when linking.
3. The SSILCE.LIB is the new Microsoft C library which is generated from SDK \NEWLIB
directory

Step 5. Locate the application program


The GW21 series do not support to locate an execution file dynamically. After linking to an
execution file, you must locate the execution file to the specified code segment and data segment
to generate an .OMF file. For example:
exe2omf @userloc.cmd
Note: You can find the file: ’userloc.cmd’ from sample program.
And then using an utility to transfer the binary file format of OMF file to a text file format: HEX
file. For example:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

xoh86 userap.omf userap.hex

Note: To make your development more simply, we suggest following steps.


Step 1. Copy a sample program (a directory) to a new directory.
Step 2. Modify old source programs (C or Assembly) or create new source programs (C or Assembly) for
your application programs.
Step 3. Depending on step2 to modify following files: ‘MAKEFILE’, ‘userlnk.cmd’.
Step 4. Run the Microsoft utility: ‘nmake’ to do compiler, link and locate procedures. And then check the
‘userap.err’ file to get the result messages. To run the nmake utility may be like followings:

3.2 Locate Application Program on ABLELinkTM Serial Server


Total Space:
SRAM 0x00000 ~ 0x7FFFF (512K bytes)
FLASH ROM 0x80000 ~ 0xFFFFF (512K bytes)

Memory partition with the Serial Server is listed as follow:


Memory Type Range (size) Use
SRAM 0x00000~0x0FFFF(064K bytes) Used by kernel (reserved)
0x10000~0x7FFFF(448K bytes) Used by your AP (data)
FLASH ROM 0x80000~0xDFFFF(384K bytes) Used by your AP (code)
0xE0000~0xFFFFF(128K bytes) Used by kernel (reserved)
Table 2-1 Memory Partition of Serial Server

After you have generated the user application programs in .exe format and before downloading them to
the Serial Server, please do the following:
1. Find the memory location of the user application program .exe file:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

The Serial Server kernel does not support dynamic locating function. You can run a user
application program .exe file in the DOS system directly, but you cannot do it in the Serial
Server. During development stage of Serial Server, it is necessary to locate the code and data
segment of the user application program (.exe file) before downloading it to the Serial Server.
After generating the user AP .exe format, you must use the locating tool to locate the .exe file to
a specific memory location. The .OMF file will then be generated.

2. Transform the file format to HEX code format for download:


Download tool recognize HEX code format. You must transform the located .OMF file to the
download file format, the .HEX file.

The user application program memory location is specified in the “userloc.cmd” file as follow.
(Following data is for 512 K flash ROM and more than 256K SRAM.)
AP code segment = 0x8000, data segment = 0x1000

Note:
Please don’t change the context of the “userloc.cmd” unless you wish to download multiple APs to
the Serial Server. However, this is not recommended.

3.3 Process Flow of Serial Server Kernel


The ABLELinkTM Serial Server kernel resides in the flash memory providing the following functions:
System Initialization (including: H/W diagnosis, Data Initialization)
Download Service
Packet driver and TCP/IP driver
Subset of BIOS functions
Subset of DOS functions
Timer Interrupt Handling functions

The ABLELinkTM Serial Server kernel will get the processing control immediately after the Serial Server
boots up, and the kernel program tries to transfer the control right to the user application program located
in the memory address of 0x8000 if the user application program was found. otherwise the kernel
program will set the Serial Server to the listen state to wait for a user application program to be
downloaded.

You may short the JP1 (in main board) to prevent the user application program from running. This
function is particularly used in the situation that you download a wrong firmware causing the
GW21x-MAXI system down and the correct application program firmware cannot be downloaded.

For JP1 location information, please refer to Figure 1.1 GW21x-MAXI Front View for details.

Please note that when the GW21x-MAXI powers on, you will see the RUN LED blinking twice. This is
happened before “System Initialization”. After “System Initialization” the RUN LED will blink twice
again. The time interval is about 1 seconds.
Note: After ‘System Initialization’, ‘User AP ok’ and ‘Enable User AP’ the RUN LED will all blink twice.
Because the time intervals between them are too short, we cannot identify each other but maybe
only one.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

The operational flow of the system is depicted in the following picture.

Power On

2
1

RUN-LED
Blinking twice

End of
No Timeout ? No
System Downloading ?
Initialization

Yes
RUN-LED Yes
Blinking twice 1
Programming
Flash memory
User AP
No
OK ?

Yes All ok ? No Clear memory


RUN-LED
Blinking twice Yes

RUN-LED
1
Blinking thrice
Enable
No
User AP ?

Yes
1
RUN-LED
Blinking twice

Upon downloading
Upon downloading,
,
Jump to IDLE LOOP DAP will suspend
GW21S-512 will suspend
User AP (WAIT DOWNLOADING) anything and
Anything,and GOTO 2
(ENDLESS LOOP) GOTO 2

RUN-LED RUN-LED
Always On Always Blinking

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

4 Advance Course
4.1 Simulation on PC
4.1.1 Simulation Introduction
It’s not necessary to download the target application programs to the Serial Server immediately after
writing the application programs & drivers, Instead, you can conduct a preliminary functional test on the
PC to verify their integrities with the system including program flow, RS-232/RS-485 communication
handling, TCP/IP connection handling with host computer, message handling between field equipment
and host, and etc. Once the simulation is done, you can download your application programs to the Serial
Server.

4.1.2 Differences Between Serial Server and PC


The Serial Server is not fully compatible with the PC in terms of operational functionalities. The
following section describes these differences.

Direction control of RS-485 transceiver


PC Use RTS/DTR signal.
Serial Server Use EnableRS485rx() and EnableRS485tx() API.

Please note that if you are sending/receiving data through the API, the Serial Server kernel will
resolve the differences. If you are using the H/W interrupt technique to send/receive data, you will
need to resolve the differences by yourself.

RS-232 Registers
PC Normal.
Serial Server The ‘Out1’ bit of MCR (Modem Control Register) is used to control the
COM LED.

DOS-dependent functions
PC Fully supported.
Serial Server Only support following functions:
SetIntrVector( ), GetIntrVector( ) and GetVersion( ).

BIOS-dependent functions
PC Fully supported.
Serial Server Only support followings:
INT10H: Video, Audio I/O and Run LED functions. (This is like
INT10H of DOS)
INT14H: Serial I/O functions. (This is like INT14H of DOS)
INT1CH: Software timer interrupt (every 55 ms). (This is like INT1CH
of DOS)

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

4.2 Download Multiple Sections


For special application requirement, you can download an user application program and multiple data
sections to a Serial Server.

When you do this job, following criteria are recommended.

Each downloaded section must be arranged into the multiplication of a segment (64K bytes).
The starting address of a segment can only be 0x8000:0, 0x9000:0, 0xA000:0, 0xB000:0,
0xC000:0, 0xD000:0.
Download one section at a time.

For example, you will do the following when downloading multiple application programs.

Application Memory Segment


AP1 code segment=0x8000 data segment=0x1000
Data1 code segment=0xC000
Data2 code segment=0xD000

Note:
1. The reasonable value of code segment is from 0x8000 to 0xD000.
2. The reasonable value of data segment is 0x1000.
3. The code and data segments cannot be overlapped.
4. By editing the ‘userloc.cmd’ file, you can change the code segment and data segment value
as you wish.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

5. API Overview
The Serial Server SDK supports many basic C-function calls for your application development. All of the
function calls are implemented by ISR (Interrupt Service Routine) and can be divided into the following
groups:

These APIs use application program’s stack, meaning that the Serial Server kernel does not change the
stack when your AP requests the service of API. The default stack size is 4 K bytes and is defined in
“makefile”. (Please refer the sample program)

In general, reserving 1K bytes stack memory is enough for these APIs calling and Serial Server kernel
processing.

5.1 General Function Overview


Function Descriptions
GetIntrVector Get ISR (Interrupt Service Routine) vector.
SetIntrVector Redirect ISR (Interrupt Service Routine) vector.
GetVersion Get the kernel version.
GetAPSerialNo Get the AP version.
SetAPSerialNo Setup AP version.
GetGwModel Get the model number.
GetConfigData Get some configuration data about IP address and version.
GetConsoleSta Check it is in console mode or not.
SYSTEM_RESET Reset system.
BatteryLowDetect Detect the system battery. (Battery is for RTC and SRAM)
(only for GW26A)

5.2 Serial I/O Functions Overview

Function Descriptions
InitSerialMode Initialize the serial port communication mode. Set baud rate,
data bit, parity and stop bit
SendSerialData Send data to the serial port.
GetSerialData Receive data from the serial port.
GetSerialStatus Get serial line status.
EnableSerialHWintr Enable hardware interrupts of serial port.
DisableSerialHWintr Disable hardware interrupts of serial port.
EnableRS485rx Set the RS-485 transceiver to RX mode.
EnableRS485tx Set the RS-485 transceiver to TX mode.
SerialIntrEOI The same as EOI(End Of Interrupt) command of assembly
language
Set485type Set COM Port to RS-422(4 wire) / RS-485(2 wire).
Get485type Get the type of COM Port, if it is RS485 (2 wires / 4 wires).
Getporttype Get the type of COM (RS-232/RS-485).
Getnewporttype Get the type of COM Port (RS232/RS422/RS485).
ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

GetTotalPort Get the count of serial ports.

5.3 Audio & LED Function Overview


Function Description
SetupProcessLed Set RUN LED on/off
BuzzerOn Make a beep
SetupComLed Set COM port LED on/off

5.4 TCP/IP Function Overview


The TCP is a reliable and connected communication protocol. Using TCP the application program can
pay less attention on the error handling of communication. In the Serial Server, the AP can establish up to
4 TCP connections with other nodes on the local area network or internet. In TCP protocol, the maximum
data length per packet must be less than 1460 bytes.

The UDP is an unreliable and connectionless communication protocol. Unlike TCP protocol, using UDP
the application program must pay more attention on the error handling of communication. In the Serial
Server, the AP can support up to 16 channels for UDP communications. It supports both point-to-point
and broadcast communications. For normal UDP application, the server device must issue udp_receive( )
or udp_receiveX( ) first. Then the client device sends data by issuing an udp_send( ) function call. If the
sequence is wrong, the data might be lost. The maximum data length per packet must be less than 1460
bytes.

Function Descriptions
tcp_connect Try to establish a connection by TCP client.
tcp_listen Try to establish a connection by TCP server.
tcp_disconnect Disconnect the established connection.
tcp_send Send data to the remote site via TCP connection.
tcp_receive Receive data from the remote site with post function.
tcp_getdata Receive data from the remote site with polling method
tcp_getsockopt Get some configuration option of the TCP connection.
tcp_setsockopt Set some configuration option to the TCP connection.
udp_open Establish an UDP communication channel.
udp_close Close an UDP communication channel.
udp_receive Receive data from any node of network.
udp_receiveX Receive data from the specific node.
udp_send Send data to the destination node.
udp_reset Release all of the UDP ports.
udp_close Close an UDP communication channel.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

udp_receive Receive data from any node of network.


udp_receiveX Receive data from the specific node.
udp_send Send data to the destination node.
udp_reset Release all of the UDP ports.
GetLocalIPAddr Get the Serial Server’s IP address.
GetLocalEthAddr Get the Serial Server’s ETHERNET address.
GetDefaultGateway Get the Serial Server’s default gateway address.
GetLocalNetmask Get the Serial Server’s NET-MASK value.
GetHostName Get the name of this device.
GetDhcp Check the DHCP mode being enable or disable.
GetConnectStatus Get connection status.
SetLocalIPAddr Get the local device’s IP address.
SetDefaultGateway Set the Serial Server’s default gateway address.
SetLocalNetmask Set the Serial Server’s NET-MASK value.
SetHostName Set a name for the device.

There is one structure definition named CmdBlock in the header file “tcpdef.h”. Through this structure,
you can request the TCP/IP’s service either by TCP or UDP and examine the returned code. The
following structure elements are important to your AP:

Data Type Element Name Use


Int Retcode Return value for post function
Int Handle The unique TCP or UDP handle created by tcp_connect,
tcp_listen and udp_open, the handle will be stored to this
element by tcp_send, tcp_receive, udp_send, udp_receive, and
udp_receiveX function calls.
Char far * Sendbuf Store the pointer of the sending buffer by tcp_send and
udp_send
Short Sendlen Store the expect to send data length of the sending buffer by
tcp_send and udp_send
Char far * recvbuf Store the pointer of the receiving buffer by tcp_receive,
udp_receive and udp_receiveX.
Short Recvlen Store the practical received length of the receiving buffer by
tcp_receive, udp_receive and udp_receiveX.

The sample programs will provide further reference.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

5.5 Memory Function Overview

Function Descriptions
EraseSector Erase one sector of flash memory.
ProgOneByte Write one byte into flash memory.
ProgString Write a block of data into flash memory.
ReadEEPROM Read one word from EEPROM.
WriteEEPROM Write one word into EEPROM

5.6 Timer Interrupt Handling Function Overview


There are 3 types of timer interrupt in the GW21x-MAXI, the 1 ms, 5 ms and 55 ms timer interrupts.

These corresponding post routines are as followins.

Timer interrupt Post routine Descriptions


1 ms timer interrupt INT 7D INT 7D is called every 1 ms
5 ms timer interrupt INT 7F INT 7F is called every 5 ms
55 ms timer interrupt INT 2C INT 2C is called every 55 ms

You can use GW21S API SetIntrVector() routine to redirect these 3 post routines as you need. The default
post routine is IRET.

5.7 Watch Dog Timer Overview


The Serial Server provides a Watch Dog Timer function. The system kernel or user AP must clear the
Watch Dog Timer in a specified time period. Otherwise the system hardware will reset after a timeout
occurs. So, if the software (including: system kernel and application program) encounters a failure in the
Serial Server, the hardware will automatically reset and restart.

Function Descriptions
WatchDogCtrl Set the Watch Dog Timer to be auto mode or manual mode.
As auto mode, the Watch Dog Timer is cleared by system (55
ms)
As manual mode, the Watch Dog Timer is cleared by AP
ClrWatchDogTime Clear Watch Dog Timer
SetupWatchDogTime Set the timeout period of the Watch Dog Timer. Only for
manual mode.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

5.8 Advanced Serial Port API (in comapi.obj)


There are some advanced serial port functions in comapi.obj. We use ISR (Interrupt Service Routine) to
process data that are sent to or come from serial port. The ISR is triggered by hardware interrupt, so the
performance and the reliability are fine.

When data are come from serial port, the ISR will get these data and put to the received queue
automatically. And then we can use the “COM_GetBuf()” to get these input data. The received queue is
defined in ‘comalo.c”. You can modify this file to change the size of queue.

When data are sent to serial port, we use the “COM_PutBuf()” to put these output data to sent queue. And
the ISR will get these data and send to serial port automatically. The sent queue is defined in ‘comalo.c”.
You can modify this file to change the size of queue.

Function Descriptions
Com_Config Initialize the serial port. Including ISR, baud rate, data bit,
parity and stop bit
Com_GetBuf Get data from receiving buffer (automatically received by
ISR).
Com_PutBuf Put data to sending buffer (automatically sent by ISR).
Com_ClearRxQue Clear receiving buffer.
Com_ClearTxQue Clear sending buffer.
Com_TxQueIsEmpty Check sending buffer is empty or not.
Com_DTR_Active Control the DTR signal.
Com_RTS_Active Control the RTS signal.
Com_DSR_Status Get the status of DSR signal.
Com_CTS_Status Get the status of CTS signal.
Com_RI_Status Get the status of RI signal.
Com_DCD_Status Get the status of DCD signal.

5.9 Digital Input and Digital Output Overview


The controller provides Digital Input and Digital Output functions. These functions are useful to monitor
or control GPIO channels. There are 4 GPIO channels in controller. Each channel can be configured to be
DI channel or DO channel.

(Note: These functions are only for GW21S-maxi or GW26A)

Function Descriptions
SetIOMode Set a channel to be DI/DO.
GetIOMode Get the channel’s I/O mode.
SetOutState Set the DO channel’s state (on/off)
GetInState Get the DI channel’s state (on/off)
ReadDigitalInput get data from the digital input port.
SetupRelayOutput Control relay outputs to on/off.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

5.10 Time Functions Overview


The controller provides Date/Time functions.
(RTC functions are only for GW21C-maxi, GW21S-maxi, GW26A)

Function Descriptions
ReadRTCTime Read system time (including: hour, min, sec)
ReadRTCDate Read system date (including: year, month, day, week)
WriteRTCTime Write system time (including: hour, min, sec).
WriteRTCDate Write system date (including: year, month, day, week).
EnableWRProtect Allow to write system date/time.
DisableWRProtect Not allow to write system date/time.

GetStatuupTime Time interval from system startup, by ms.


GetSysMsec Time interval from system startup, by ms.
GetSysTick Time interval from system startup, by tick (55ms).
GetSysSec Time interval from system startup, by second (1000ms).
GetSysTime Time interval from system startup, by second (1000ms).

5.11 Account Overview


The controller provides some API to maintain user names and password. You can use these functions to
do some security protection.

Function Descriptions
AddAccount Add an account (add user name and password)
DelAccount Delete an account (delete user)
VerifyAccount Verify an account (verify user name and password)

5.12 Wireless LAN Command Overview


There functions are only for models with wireless LAN:
(for GW21W-maxi, GW21W)

Function Descriptions
GetESSID Get the Extended Service Set ID
SetESSID Set the Extended Service Set ID
GetFrame Get frame length used for RTS/CTS handshake control
SetFrame Set frame length used for RTS/CTS handshake control
GetFragment Get fragment length for unicast message transmission
SetFragment Set fragment length for unicast message transmission
GetChannel Get communication channel number for BSS creation
SetChannel Get net connection control characteristics
GetNetWorkType Get net connection control characteristics
SetNetWorkType Set net connection control characteristics
GetTransmitRate Get data rate for message transmission
SetTransmitRate Set data rate for message transmission
GetXI300WEPFlag Get WEP flag
SetXI300WEPFlag Set WEP flag

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

GetXI300WEPBit Get WEP bits


SetXI300WEPBit Set WEP bits
GetXI300WEPKey Get WEP key
SetXI300WEPKey Set WEP key
GetRFRealChannel Get the RF channel when linking with Access Point
GetRFTxRate Get the real transmission rate when linking with Access Point
GetAPName Get the name of Access Point which linking with
GetAPMac Get the MAC address of Access Point which linking with
LanRestart Get the MAC address of Access Point which linking with
WGetMacStatus Get the WLAN connection status.
WGetBssQuality Get communication quality of the WLAN.
GetCountry About WLAN, different countries have different legal channels. When
manufacturing, every device was assigned a country code which can
be use to decide legal channels.
GetLegalChannel About WLAN, different countries have different legal channels. When
manufacturing, every device was assigned a country code which can
be used to decide legal channels.
WScanRequest Request to scan Basic Service Set (BSS). In general it is to search
Access Points (AP).

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

6. API Reference
6.1 General Functions

GetIntrVector()
Description: Get ISR (Interrupt Service Routine) entry point.
Include: dapapi.h
Usage: ulong GetIntrVector (uchar vector)
Input Parameters: vector: target interrupt vector
Return Value: ISR (Interrupt Service Routine) entry point is the starting code
address of the routine. It is represented as a LONG data format. It
contains the “Segment” and “Offset” address of the entry point.
Link: dapapi.dll
Example: Get the entry point of 1ms timer interrupt
ulong isr_1ms
isr_1ms = GetIntrVector(0x7D)
Remarks: Get the entry point of the interrupt vector specified by vector. This
function is generally used with SetIntrVector. To replace an interrupt
first save the current vector entry point by GetIntrVector, then set the
vector to your own interrupt function with SetIntrVector. The saved
vector can be restored if needed.

System reserved interrupt vectors


Vector Description
0x1C 1 tick (55 ms) timer interrupt
0x7D 1 ms timer interrupt
0x0C the 1st RS-232/RS-485 communication interrupt
0x0D the 2nd RS-232/RS-485 communication interrupt
0x80 ~ 0xFF reserved for your application

Note: Because Interrupt Service Routine maybe disable hardware interrupt, please don’t spend too
many CPU time in an interrupt service routine.

SetIntrVector()
Description: Set ISR (Interrupt Service Routine) entry point.
Include: dapapi.h
Usage: void SetIntrVector (uchar vector, farFUNC isr )
Input Parameters: vector target interrupt vector to replace
isr: the entry point of ISR
Return Value: None
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: Setup the ISR to the 1st RS-232/RS-485 communication port. The
ISR codes in assembly language.
extern void far SIOIntr0();
main() {
SetIntrVector ( 0x0C, SIOIntr0 );
:
}

SIOIntr0 proc far


:
iret
SIOIntr0 endp
Remarks: Set the new ISR of isr to be the interrupt vector specified by vector.
If the isr is a C function, it must have been previously declared with
the interrupt attribute. Otherwise, you must make sure that the
function satisfies the requirements for an interrupt-handle routine.
For example, if the ISR is an assembler function, it must be a far
routine that returns with an IRET instead of a RET. Refer to the
example.

Note: Because Interrupt Service Routine maybe disable hardware interrupt, please don’t spend too
many CPU time in an interrupt service routine.

GetVersion()
Description: Gets the Serial Server F/W (kernel) version.
Include: dapapi.h
Usage: ushort GetVersion (void )
Input Parameters: None
Return Value: Low byte: major version number
High byte: minor version number
Link: dapapi.dll
Example: ushort dap_version;
dap_version = GetVersion()
Remarks: Gets the Serial Server firmware version number.

GetAPSerialNo()
Description: Get the version of the Application Program. This version string is set
by SetAPSerialNo().
Include: dapapi.h
Usage: void GetAPSerialNo(char*buf);

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Input Parameters: buf:buffer for storing the Application Program version


The max. buffer length is 80+1 bytes including a null terminator.
Return Value: None
Link: dapapi.dll
Example: #define AP_VERSION_LEN 80
Char buf[AP_VERSION_LEN+1] ;

buf[[AP_VERSION_LEN] = ‘\0’ ;
GetAPSerialNo(buf);
Remarks: buf must be a characters array of 80+1 bytes

SetAPSerialNo()
Description: Set AP version
Include: dapapi.h
Usage: void SetAPSerialNo(char*ap_version);
Input Parameters: ap_version: a string which specifies the Application Program version
and some information about it.
Return Value: None
Link: dapapi.dll
Example: SetAPSerialNO(“termsrv1.0”);
Remarks: 1. ap_version is a null terminated string with 80 bytes maximum.
2. The ap_version can be displayed on the tool of ”moniter.exe”.

GetGwModel()
Description: Get the model number of this device
Include: dapapi.h
Usage: void GetGwModel(char*buf);
Input Parameters: buf:buffer for storing the model number.
The max. buffer length is 17 bytes (max. model number is 16
characters + 1 byte null terminator)
Return Value: None
Link: dapapi.dll
Example: #define GW_MODEL_LEN 16
Char buf[GW_MODEL_LEN+1] ;

GetGwModel( buf);
Remarks: buf must be a characters array of 17 bytes

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 2
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

GetConfigData()
Description: Get the device’s configuration data including TCP/IP configuration
data and version information.
Include: dapapi.h
Usage: void GetConfigData(char*buf);
Input Parameters: buf: buffer for storing the configuration data.
The buffer length must be 300 bytes
Return Value: None
Link: dapapi.dll
Example: #define GW_CONFIG_LEN 300
Char buf[GW_CONFIG_LEN] ;

GetConfigData( buf);
Remarks:
Reserved (4 Bytes)
Reserved (4 Bytes)
Reserved (4 Bytes)
Device’s IP Address (4 Bytes)
Reserved (4 Bytes)
Reserved (4 Bytes)
Gateway IP Address (4 Bytes)
(16 Bytes)
Offset 0-5 : Device MAC Address
Offset 6-15 : Reserved
(64 Bytes)
Offset 0-15 : Model Name
Offset 16-62 : Reserved
Offset 63 : Number of serial ports provided by device
(128 Bytes)
Offset 0 : Major version of kernel
Offset 1 : Minor version of kernel
Offset 2-127 : AP version
(64 Bytes)
Offset 0-3 : Subnet Mask
Offset 4-63 : Reserved

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

GetConsoleSta()
Description: Get the state which specifies it is in console mode or not.
Include: dapapi.h
Usage: ushort GetConsoleSta( void);
Input Parameters: None
Return Value: 0: console mode disable
1: console mode enable
Link: dapapi.dll
Example: ushort console_mode ;

console_mode = GetConsoleSta();
Remarks:

SYSTEM_RESET()
Description: Reset the system.
Include: dapapi.h
Usage: void SYSTEM_RESET(void);
Input Parameters:
Return Value: None
Link: dapapi.dll
Example: SYSTEM_RESET();
Remarks:

BatteryLowDetect()
Description: Detect the system battery. (Battery is for RTC and SRAM)
(only for GW26A)
Include: dapapi.h
Usage: int BatteryLowDetect(void);
Input Parameters:
Return Value: 0 : battery low;(please change battery).
Other: battery normal;
Example: batt_detect = BatteryLowDetect();
if(!batt_detect) {
/* error, battery low */
};
;
Remarks:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

SetDefaultKeyProc()
Description: Provide a customized default key process.
Include: dapapi.h
Usage: void SetDefaultKeyProc( farFUNCi );

Note: typedef int (far *farFUNCi)();


Input Parameters: farFUNCi: a customized default key processing function.

Int farFUNCi( int tick, int released_flag)


tick: the time which the default key has been pressed for. (1 tick=55ms)
released_flag: 0:the default key is still pressed.
1: the default key is just released.
Return value: 1:restart, 2: restored default setting and then restart,
other:ignore.
Return Value: None
Link: dapapi.dll
Example: int _loadds far default_key_proc( unsigned int tick, int released_flag)
{
if ( released_flag==0) /*---default key is still pressed */
{
if ((tick % (5*18)) == 0) BuzzerOn() ;
return(0) ;
}
else /*---default key is released */
if ( tick < 5*18) /*--- <5 sec: reset to restart */
{
return(1) ;
}
else /*--- >5 sec: restored default */
{
return(2) ;
}
}

main() {

SetDefaultKeyProc( default_key_proc) ;
…for (;;) {

}
}

Remarks:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Read_ApState ()
Description: Read the application program jumper is enabled or disabled.
Include: dapapi.h
Usage: unsigned short Read_ApState(void);
Input Parameters: None
Return Value: 0:the allpication program is enabled, 1:enabled
Link: dapapi.dll
Example: ret = Read_ApState();
Remarks:

6.2 Serial I/O Functions

InitSerialMode()
Description: Initialize the serial port communication mode.
Include: dapapi.h
Usage: ushort InitSerialMode (ushort port, uchar mode)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
mode: Mode consists of four parts: baud rate, parity, data length and
stop bit length all in binary numbers. For detailed information,
please refer followings.
Bit 7 6 5 4 3 2 1 0
Setting Baud rate Parity Stop bit Data length
000 – 19200 X0 - None 0 – 1 10 - 7 bits
001 – 38400 01 - Odd 1 – 2 11 – 8 bits
010 – 57600 11 - Even
011 –115200
100 - 1200
101 – 2400
110 - 4800
111 – 9600

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Return Value: Unsigned short


High byte contain: LSR (Line Status Register)
Bit 7 = Time out. 0: No timeout; 1: Send data timeout
Bit 6 = Transmission shift register empty
Bit 5 = Transmission holding register empty
Bit 4 = Break detect
Bit 3 = Framing error
Bit 2 = Parity error
Bit 1 = Overrun error
Bit 0 = Data ready
Low byte contain: MSR (Modem Status Register)
Bit 7 = Receive Line Signal Detect
Bit 6 = Ring Indicator
Bit 5 = Data Set Ready
Bit 4 = Clear to Send
Bit 3 = Delta Receive Line Signal Detect
Bit 2 = Trailing Edge Ring Indicator
Bit 1 = Delta Data Ready
Bit 0 = Delta Clear to Send
Link: dapapi.dll
Example: /* Initialize the first serial port with mode 9600,n,8,1 */
Line_Status = InitSerialMode( 0, 0xE3 )

SendSerialData()
Description: Send data to the serial port.
Include: dapapi.h
Usage: ushort SendSerialData (ushort port, uchar Send_data)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Send_data: unsigned char
A character to be sent out
Return Value: The status of the specified serial line.
High byte contain: LSR(Line Status Register)
Bit 7 = Time out. 0: No time out; 1: Send data Time out
Bit 6 = Transmission shift register empty
Bit 5 = Transmission holding register empty
Bit 4 = Break detect
Bit 3 = Framing error
Bit 2 = Parity error
Bit 1 = Overrun error
Bit 0 = Data ready

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Low byte contain: Send_data


Link: dapapi.dll
Example: /* Send data '0' (0x30) to the 1st port. */
Line status = SendSerialData( 0, '0' )
Remarks: This function sends one character data out to the specific serial port.

GetSerialData()
Description: Receive data from the serial port.
Include: dapapi.h
Usage: ushort GetSerialData (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: The status of the specified serial line and received data.
High byte contain:
Bit 7 = Time out
Bit 4 = Break detect
Bit 3 = Framing error
Bit 2 = Parity error
Bit 1 = Overrun error

Low byte contain: received data


Link: dapapi.dll
Example: /* Get data from 1st port. */
Status_Data = GetSerialData(0) ;
if ( Status_Data & 0x8000) {
/* no data */
}
else {
receive_char = Status_Data & 0xFF ;
}
Remarks: This function poll to get a character data from the specific serial port.
If there is no data been received then the return value’s bit 7 of high
byte is set to 1 (timeout). If the return value’s bit 7 of high byte is set
to 0 then there is a character been received and you can check the
return value’s bit 0 to bit 3 of high byte to verify the received data is
ok.

GetSerialStatus()
Description: Get the line status of the specific serial port.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Include: dapapi.h
Usage: ushort GetSerialStatus (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: Refer to InitSerialMode() return value
Link: dapapi.dll
Example: /* Get line status of the 1st port. */
Line_status = GetSerialStatus(0)
Remarks:

EnableSerialHWintr()
Description: Enable the hardware interrupt of the specific serial port.
Include: dapapi.h
Usage: void EnableSerialHWintr (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: None
Link: dapapi.dll
Example: /* Enable the 1st serial port H/W interrupt. */
EnableSerialHWintr( 0 )
Remarks: Before calling this function, you must setup the Interrupt Service
Routine (ISR) first.

DisableSerialHWintr()
Description: Disable the hardware interrupt of the specific serial port.
Include: dapapi.h
Usage: void DisableSerialHWintr (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: None
Link: dapapi.dll
Example: Disable the 1st serial port H/W interrupt.
DisableSerialHWintr(0)
Remarks:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

EnableRS485rx()
Description: Set the RS-485 transceiver to RX mode for the specific serial port.
Include: dapapi.h
Usage: void EnableRS485rx (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: None
Link: dapapi.dll
Example: Enable RX mode of the 1st serial port.
EnableRS485rx( 1 )
Remarks: This function has no effect to RS-232 port. If the RS-485 serial port
want to send data then its RS-485 transceiver must be set to TX
mode. After sending data is over then its RS-485 transceiver must be
set to RX mode for receiving data.

EnableRS485tx()
Description: Set the RS-485 transceiver to TX mode for the specific serial port.
Include: dapapi.h
Usage: void EnableRS485tx (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: None
Link: dapapi.dll
Example: Enable TX mode of the 1st serial port.
EnableRS485tx( 0 )
Remarks: This function has no effect to RS-232 port. If the RS-485 serial port
want to send data then its RS-485 transceiver must be set to TX
mode. After sending data is over then its RS-485 transceiver must be
set to RX mode for receiving data.

SerialIntrEOI()
Description: Execute the EOI command for the serial port H/W interrupt service
routine.
Include: dapapi.h
Usage: void SerialIntrEOI (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: None

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Link: dapapi.dll
Example: Issue EOI command for the 1st port.
SerialIntrEOI( 1 )
Remarks: It must issue the H/W End Of Interrupt (EOI) command before
terminating the H/W ISR (Interrupt Service Routine).

Getnewporttype()
Description: Get the type of COM Port (RS232/RS422/RS485).
Include: dapapi.h
Usage: ushort Getnewporttype (ushort port)
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
Return Value: 0: RS232
1: RS485 (2 wires)
2: RS422 (4 wires)
3: Console mode
4: RS422/RS485
5:Unknown
Link: dapapi.dll
Example: ushort port_type ;

port_type = Genewporttype();
Remarks:

Setnewporttype()
Description: Set the type of COM Port (RS232/RS422/RS485).
(only for SE5001 and SE5002)
Include: dapapi.h
Usage: ushort Setnewporttype (ushort port, uchar type)
Input Parameters: port: unsigned short, =0: for COM1, 1: for COM2
type: unsigned char, =0: RS232, 1: RS485 (2 wires),
2: RS422 (4 wires)
Return Value: <0: setting failed, >=0:ok
Link: dapapi.dll
Example: /* Set COM1 as RS-232 */
Senewporttype( 0, 0);
Remarks:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Setnewporttype2()
Description: Set the type of COM Port (RS232/RS422/RS485) and save the
configuration to EEPRPOM.
(only for SE5001 and SE5002)
Include: dapapi.h
Usage: ushort Setnewporttype2 (ushort port, uchar type)
Input Parameters: port: unsigned short, =0: for COM1, 1: for COM2
type: unsigned char, =0: RS232, 1: RS485 (2 wires),
2: RS422 (4 wires)
Return Value: <0: setting failed, >=0:ok
Link: dapapi.dll
Example: /* Set COM1 as RS-232 */
Senewporttype2( 0, 0);
Remarks:

Getporttype()
Description: Get the type of COM (RS-232/RS-485)
(Note: For GW26A, it can not be detected to RS232 or RS485)
Include: dapapi.h
Usage: ushort Getporttype(void )
Input Parameters: None
Return Value: unsigned short.
= 0xFFFF: The device does not support this function
= Others:
bit 0 (Port1): 0-> RS485, 1-> RS232
bit 1 (Port2): 0-> RS485, 1-> RS232
Note: If the device does not have COM2, then the bit 1 can be ignored.
Link: dapapi.dll
Example: get_porttype = Getporttype();
if (get_porttype & 1)
/* This is RS232 for port1*/
else
/* This is RS485 for port2 */
Remarks:

Get485type()
Description: Get the type of COM Port, if it is RS485 (2 wires / 4 wires).
(Note: GW21S-MAXI does not support this function.)
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 3
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: ushort Get485type(void )


Input Parameters: None
Return Value: unsigned short
= 0xFFFF: The device does not support this function
= Others:
bit 0 (Port1): 0-> RS422(4 wires), 1-> RS485(2 wires)
bit 1 (Port2): 0-> RS422(4 wires), 1-> RS485(2 wires)
Note: If the device does not have COM2, then the bit 1 can be ignored.
Link: dapapi.dll
Example: get_porttype = Getporttype();
if (get_porttype & 1)
/* This is RS232 for port1*/
else {
/* This is RS485 for port1 */
get_485type = Get485type() ;
if ( get_485type & 1)
/* This is RS-485 for port1 */
else
/* This is RS-422 for port1 */
}
Remarks:

Set485type()
Description: Set Com Port to RS-422(4 wire) / RS-485(2 wire).
(Note: Only GW21L, GW21R and GW21W support this function)
Include: dapapi.h
Usage: int Set485type( uchar Set_485type)
Input Parameters: Set_485type: unsigned char,
bit 0 (Port1): 0-> RS422(4 wires), 1-> RS485(2 wires)
bit 1 (Port2): 0-> RS422(4 wires), 1-> RS485(2 wires)
Note: If the device does not have COM2, then the bit 1 can be ignored.
Return Value: None
Link: dapapi.dll
Example: /* set COM1 as RS485 and COM2 as RS422 */
Set485type(0x01)

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: Select RS-485 /RS-422.


Bit 1 Bit 0 Com Port 2 Com Port 1
0 0 RS-422 RS-422
0 1 RS-422 RS-485
1 0 RS-485 RS-422
1 1 RS-485 RS-485

GetTotalPort()
Description: Get the count of serial ports in the device.
Include: dapapi.h
Usage: ushort GetTotalPort ( void);
Input Parameters: None
Return Value: >=0: count of serial ports
Link: dapapi.dll
Example: ushort port_count ;

port_count = GetTotalPort ();


Remarks:

GetPhyState()
Description: Get the Ethernet cable connecting status.
Include: dapapi.h
Usage: ushort GetPhyState ( void);
Input Parameters: None
Return Value: >0: ok
Link: dapapi.dll
Example: int ret ;

ret = GetPhyState ();


Remarks:

6.3 Audio & LED Functions

SetupProcessLed()
Description: Set RUN LED to on/off.
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: void SetupProcessLed( uchar run_led);


Input Parameters: run_led: unsigned char
= 0:off, 1:on
Return Value: None
Link: dapapi.dll
Example: SetupProcessLed(1); // set RUN led on
SetupProcessLed(0); // set RUN led off
Remarks:

BuzzerOn()
Description: Make a buzzer on.
Include: dapapi.h
Usage: Void BuzzerOn ( void )
Input Parameters: None
Return Value: None
Link: dapapi.dll
Example: BuzzerOn()
Remarks: Turn the buzzer on for 40ms and then off.
For GW21S-maxi and GW21L, they do not have buzzers so
calling this function is the same as calling SetupProcessLed().
Except that SetupProcessLed() is a state changed function and
BuzzerOn() is a trigger function.

SetupComLed()
Description: Set LEDs on/off of a specified COM ports.
Include: dapapi.h
Usage: void SetupComLed(ushort port, uchar com_led);
Input Parameters: port: unsigned short
=0: for COM1, 1: for COM2
com_led: unsigned char
= 0:off, 1:on
Return Value: None
Link: dapapi.dll
Example: SetupComLed(0,1); // com port1 led on
SetupComLed(1,0); // com port2 led off
Remarks:

6.4 TCP/IP Functions

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

tcp_connect()
Description: Request to establish a connection to a peer by client site.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_connect (int far *handle, unsigned short fromPort,
unsigned long toIPaddr, unsigned short toPort, farFUNC postfunc,
char far *parg)
Input Parameters: phandle: the address of an unique handle establishing the
connection.
fromPort: local TCP socket port number
toIPaddr: the IP address of remote device which you want to
connect to
toPort: remote TCP socket port number
postfunc: entry point of the post-function which will be called by
kernel system after the connecting is successful or failed.
parg : the address of an argument to pass to post-function
Return Value: =SUCCESS : the TCP/IP driver accepts this request.
others : error, refer to error message defined in tcpdef.h
Link: dapapi.dll
Example: /* about detailed program please refer the sample in Appendix */
/* establishes a TCP connection */
ushort localPort = 0;
ulong remoteIP = 0x2034000a; /* 10.0.52.32 */
ushort remotePort = 0x4321;

/* for client device: */


ret = tcp_connect( (int far *)&handle, localPort, remoteIP,
remotePort, postfunc, arg);
if ( ret != SUCCESS )
{
//fatal error

}
else
{
// wait until a connect post-function

}

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: This function is used by the client device to establish a connection


with the server device. Upon the server issuing tcp_listen command,
the connection will be established accordingly.

When the connection is successful or failed, the post-function which


will be called by kernel system

tcp_listen()
Description: Establish a connection by server site.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_listen (int far *phandle, unsigned short fromPort, farFUNC
postfunc, char far *parg)
Input Parameters: phandle: the address of an unique handle establishing the
connection.
fromPort: local TCP socket port number
postfunc:entry point of the post-function which will be called by
kernel system after the connecting is successful or
failed.
parg: The address of an argument to pass to post-function.
Return Value: =SUCCESS: the TCP/IP driver accepts this request.
others : error, please refer to the file tcpdef.h
Link: dapapi.dll
Example: /* about detailed program please refer the sample in Appendix */
ushort localPORT = 0x1234;

/* for server device: */


ret = tcp_listen( (int far *)&handle, localPORT, postfunc, arg);
if ( ret != SUCCESS )
{
//fatal error

}
else
{
//wait until a listen post-function

}

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: This function is used by the server device to establish a connection


with the client device. Upon the client issuing tcp_connect
command, the connection will be established accordingly.

When the connection is successful or failed, the post-function which


will be called by kernel system

tcp_disconnect()
Description: Disconnect the established connection.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_disconnect (int handle)
Input Parameters: handle: the unique handle of the established connection
Return Value: =SUCCESS : disconnect is successful
others : error, please refer to the file tcpdef.h
Link: dapapi.dll
Example: /* disconnect to the device */
tcp_discount ( handle ); // disconnect the established connection

Remarks: This function is used by the client and severs devices to disconnect
the connection that has already been established. After calling this
function one of the devices (client or server) will disconnect first and
the other device will disconnect in 2 seconds for garbage processing.
You can establish a new connection between these two devices again.

tcp_send()
Description: Request to send data to the remote site via TCP connection.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_send (int handle, char far *pbuf, unsigned short len, farFUNC
postfunc, char far *parg)
Input Parameters: handle: the unique handle of the established connection
pbuf : the pointer of data buffer to be sent out
len: data length
postfunc: entry point of the post-function which will be called by
kernel system after the data are sent to remote site
successfully or failed to send.
parg: The address of an argument to pass to post-function.
Return Value: =SUCCESS : the TCP/IP driver accepts this request.
others : error, please refer to the file tcpdef.h
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: /* send data via TCP and check the data are sent to remote site
successfully or not. */
int flag_result;
int main()
{
:
flag_result = 1;
ret = tcp_send( handle, “1234”, 4, send_post, NULL);
if ( ret == SUCCESSFUL) {
while( flag_result == 1 ) ; // wait for result
return( flag_result );
}
:
}

void _loadds far send_post( CMDBLK far *cb, void far *arg)
{
if( cb->retcode == SUCCESS )
flag_result = OK; //=0
else flag_result = cb->retcode; //<0
}

Remarks: This function is used to send data to the remote site via the
established connection. The maximum data length is 1460 bytes. In
the post-function, you can check whether this function is successful
or not. In the tcp_send post-function, you can issue tcp_send again.

When the data are sent to remote site successfully or not, the
post-function which will be called by kernel system

tcp_receive()
Description: Request to receive data from the remote site via TCP connection.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_receive (int handle, char far *pbuf, unsigned short len,
farFUNC postfunc, char far *parg)
Input Parameters: handle: the unique handle of the established connection
postfunc: entry point of the post-function which will be called by
kernel system after data are received or the connection is
disconnected.
parg: The address of an argument to pass to post-function.
pbuf: the address of receive buffer

len: data length

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Return Value: =SUCCESS : connect is successful


others : error, please refer to the file tcpdef.h
Link: dapapi.dll
Example: /* receive data once the connection is established. */
void _loadds far listen_post(CMDBLK far *cb, void far *arg)
{
if (cb->retcode == SUCCESS )
{
tcp_receive( handle, buf, len, receive_post, arg);

}
}
void _loadds far receive_post(CMDBLK far *cb, void far *arg)
{
if(cb->retcode == SUCCESS )
{
: //Process incoming data,and then
: //issue tcp_receive(....) again
tcp_receive( handle, buf, len, receive_post, arg);
}
}
Remarks: This function is used to get data from the remote site via the
established connection. In the post-function, you can check whether
this function is successful or not. If successful, then you can process
the incoming data. Normally, the first time to call tcp_receice() is in
the tcp_listen() or tcp_connect() post-function. In the tcp_receive()
post-function, you can issue tcp_receive() again.

When the data are received or the connection is failed, the


post-function will be called by kernel system

tcp_getdata()
Description: Poll to receive data from the remote site via TCP connection.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_getdata (int handle, char far *pbuf, short far *plen)
Input Parameters: handle: the unique handle of the established connection
plen: the address of requested length when calling, and the
address of actual length after calling
pbuf the address of receive buffer
Return Value: =SUCCESS : connect is successful
others : error, please refer to the file tcpdef.h
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: //expect to get 100 bytes data length form TCP driver.
char buf[512];
short len;

len = 100;
if ( tcp_getdata( handle, buf, (short far *)&len) == SUCCESS )
{
if ( len == 0 )
{
//No received data
}
else
{
//Process received data

}
}
else
{
//fatal error, the connection may be failed.

}
Remarks: This function is almost the same as tcp_receive(). The difference
between these two functions is that tcp_getdata() is without
post-function. The practical received data length will store into the
plen.

tcp_getsockopt()
Description: Get some configuration option of the TCP connection.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_getsockopt (int handle, int cmd_type, char far *pbuf, short far
*plen)
Input Parameters: handle: the unique handle of the established connection
cmd_type: A command ID to identify a configuration job.

pbuf the address of supplement buffer


plen: the length of supplement buffer
Return Value: =SUCCESS : command is successful
others : error, please refer to the file tcpdef.h
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: //get the start time of sending keep-alive packet.

short keep_alive;

keep_alive = 0;
if ( tcp_getsockopt( handle, SO_KEEPALIVE_TIME , &keep_alive,
sizeof(keep_alive)) == SUCCESS )
{
/* ok */
}
else
{
/* failed */
}

Remarks: For cmd_type = SO_KEEPALIVE_TIME


Please refer the remark of tcp_setsockopt().

For cmd_type = SO_RETRANSMIT_MAX


Please refer the remark of tcp_setsockopt().

tcp_setsockopt()
Description: Set some configuration option to the TCP connection.
Include: tcpapi.h, tcpdef.h
Usage: int tcp_setsockopt (int handle, int cmd_type, char far *pbuf, short far
*plen)
Input Parameters: handle: the unique handle of the established connection
cmd_type: A command ID to identify a configuration job.

pbuf the address of supplement buffer


plen: the length of supplement buffer
Return Value: =SUCCESS : command is successful
others : error, please refer to the file tcpdef.h
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 4
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: //set to disable keep-alive packet.


short keep_alive;

keep_alive = 0;
if ( tcp_setsockopt( handle, SO_KEEPALIVE_TIME , &keep_alive,
sizeof(keep_alive)) == SUCCESS )
{
/* ok */
}
else
{
/* failed */
}
Remarks: For cmd_type = SO_KEEPALIVE_TIME
If the TCP connection is silent more than n*10 sec, then the TCP
driver will send a keep-alive packet to make sure the connection is
ok. If the remote site is still no response then the TCP driver will
re-send the keep-alive packet every 10 sec at least 7 times then
disconnect this TCP connection.
The n is an short integer type and its address is as the pbuf parameter.
Its default value is 4. The default disconnect time is (4*10sec +
7*10sec) which is about 110 sec.
If its value is 0 then there is no keep-alive packet will be sent.

For cmd_type = SO_RETRANSMIT_MAX


If a TCP packet is failed to be sent to remote site then the TCP driver
will re-send this packet after delaying 280*(1<<k) ms. The k is the
failure count. The 1st times timeout is 280 ms. Others timeouts are
280*2 ms, 280*4 ms, 280*8 ms, 280*16 ms, etc.
If the failure count k is more than n times then the TCP connection
will be disconnect.
The n is an short integer type and its address is as the pbuf parameter.
Its default value is 7. The default disconnect time is (280 ms + 280*2
+ 280*4 + 280*8 + .. + 280*256) which is about 140 sec.
If its value is 0 then the disconnect time is (280 ms + 280*2) = 840
ms.

udp_open()
Description: Establish an UDP communication channel.
Include: tcpapi.h, tcpdef.h
Usage: int udp_open (int *phandle, short *plocalPort)

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Input Parameters: Phandle: the address of the unique handle establishing


communication channel
plocalPort: the address of local UDP port number
Return Value: =SUCCESS : connect is successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: //Open an UDP channel with local port = 0x1234.
int handle;
short localport=0x1234 ;

if (udp_open((int *)&handle, (short *)&localport) == SUCCESS)


{
// open ok , and return a unique handler
:
}
else { // fatal error happened
:
}

Remarks: This function can be used by a client or a sever to establish an UDP


communication channel.

udp_close()
Description: Close an UDP communication channel.
Include: tcpapi.h, tcpdef.h
Usage: int udp_close (int handle)
Input Parameters: handle the unique handle of the established communication
channel
Return Value: =SUCCESS : close is successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: //Close an exist UDP channel
udp_close ( handle );

Remarks: This function can be used by a client or a severs to close an UDP


communication channel.

udp_receive()
Description: Receive data from the remote site.
Include: tcpapi.h, tcpdef.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: int udp_receive (int handle, char far *pbuf, unsigned short len,
farFUNC postfunc, void far *parg)
Input Parameters: handle the handle of the UDP channel created by udp_open

postfunc entry point of the post-function which will be called by


kernel system after data are received.
pbuf the address of receive buffer
parg the address of an argument to pass to post-function
len data length of receive buffer
Return Value: =SUCCESS : connect is successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: //receive data from any other node and terminates the program until
//the special character of ‘\x1A’ is received.
main()
{
flag_continue = TRUE;
upd_open( &handle );
udp_receive( handle, buf, 512, recv_post, “”)
while( flag_continue ) {
: // processing
}
}

void _loadds far recv_post(CMDBLK far *cb, void far *arg)


{
if ( cb->retcode == SUCCESS ) {
for ( i = 0; i < cb->recvlen; i++ )
if (buf[ i ] == ‘\x1A’) {
flag_continue = FALSE;
return;
}
udp_receive(cb->handle, buf,len, recv_post, “”);
}
}
Remarks: This function is used to get data from ANY OTHER IP ADDRESS.
The maximum data length is 1460 bytes. In the post-function, you
can check whether this function is successful or not. If successful,
then you can process the incoming data. In the udp_receive()
post-function, you can issue udp_receive() again.

udp_receiveX()
Description: Request to receive UDP data from the remote site.
Include: tcpapi.h, tcpdef.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: int udp_receive (int handle, short fromPort, long fromIPaddr,


char far *pbuf, short len, farFUNC postfunc, void far *parg)
Input Parameters: handle the handle of the UDP channel created by udp_open
fromPort the source node’s UDP port number where incoming
data came from.
fromIPaddr the source node’s IP address where incoming data
came from.
pbuf the address of receive buffer
len the length of receive buffer
postfunc entry point of the post-function which will be called
by kernel system after data are received.
parg the address of an argument to pass to post-function
Return Value: =SUCCESS : the request is successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* receive data from specific node (IP address: 10.0.52.100; port
number: 0x1234). */
ulong remoteIP = 0x6434000a; // IP addr = 10.0.52.100
ushort remotePORT = 0x1234;
main()
{
udp_open( &handle )
udp_receiveX( handle, remotePORT, remoteIP, buf, len,
recv_post, arg );
: // processing incoming data
:
}
Remarks: This function is used to get data from the specific node and port
number. The maximum data length is 1460 bytes. In the
post-function, you can check whether this function is successful or
not. If successful, then you can process the incoming data. In the
udp_receiveX() post-function, you can issue udp_receiveX() again.

udp_send()
Description: Send data to the destination node.
Include: tcpapi.h, tcpdef.h
Usage: int udp_send (int handle, short toPort, long toIPaddr, char far
*pbuf, short len)
Input Parameters: handle the unique handle of the established connection
toPort the UDP port number of destination node

toIPaddr the IP address of destination node


len the data length of sent buffer
pbuf the address of data buffer to be sent out

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Return Value: =SUCCESS : send is successful


others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* send data to the specific node (IP address: 10.0.52.100; port
number: 0x1234). */
ulong remoteIP = 0x6434000a; // IP addr = 10.0.52.100
ushort remotePORT = 0x1234;

main()
{
udp_open( &handle )
udp_send ( handle, remotePORT, remoteIP, buf, len);
: // processing
:
}
Remarks: This function is used to send UDP data to the destination node. The
maximum data length is 1460 bytes.

udp_reset()
Description: Release all of the UDP ports.
Include: tcpapi.h, tcpdef.h
Usage: int udp_reset ()
Input Parameters:
Return Value: =SUCCESS : successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* Close all of the UDP channels */
udp_open ( handle1 );
udp_open ( handle2 );
:
udp_reset ( );

Remarks: This function is used to close all of the UDP communication


channels

GetLocalIPAddr()
Description: Get the local device’s IP address.
Include: tcpapi.h, tcpdef.h
Usage: int GetLocalIPAddr ( char far *buf )
Input Parameters: buf buffer to store the IP address. The buffer must be more than 4
bytes.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Return Value: =SUCCESS : successful


others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* Get the Serial Server’s own IP address */
uchar buf[4];

GetLocalIPAddr ( buf ); // assume IP address = 10.0.52.100


// buf[0] = 0x0a
// buf[1] = 0x00
// buf[2] = 0x34
// buf[3] = 0x64
Remarks: This function is used to get the Serial Server local IP address. Each
Serial Server must have its own IP address to identify the node on the
local network via TCP/IP.

GetLocalEthAddr()
Description: Get the Serial Server’s ETHERNET address
Include: tcpapi.h, tcpdef.h
Usage: int GetLocalEthAddr (char far *buf )
Input Parameters: buf buffer to store the ETHERNET address. The buffer must be
more than 6 bytes.
Return Value: =SUCCESS : successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* Gets the Serial Server’s ETHERNET address */
uchar buf[6];
GetLocalEthAddr ( buf ); // assume ETHERNET address
// buf[0] = 0x00 // = 00:60:E9-01:02:03
// buf[1] = 0x60
// buf[2] = 0xE9
// buf[3] = 0x01
// buf[4] = 0x02
// buf[5] = 0x03
Remarks: This function is used to get the Serial Server’s physical address, it is
also called the MAC (Medium Access Control) address. An example
of the ETHERNET address shown below
manufacture ID board ID
| 0x00 0x60 0xE9 | 0x01 0x02 03 |

GetDefaultGateway()
Description: Get the Serial Server’s default gateway address.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Include: tcpapi.h, tcpdef.h


Usage: int GetDefaultGateway (char far *buf )
Input Parameters: buf buffer to store the default gateway address. The buffer must
be more than 4 bytes.
Return Value: =SUCCESS : successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* Get the Serial Server’s default gateway IP address */
uchar buf[4];
GetDefaultGateway ( buf ); // assume default gateway IP address
// buf[0] = 0x0a // = 10.0.52.250
// buf[1] = 0x00
// buf[2] = 0x34
// buf[3] = 0xfa
Remarks: This function is used to get the default gateway IP address in the
Serial Server. Each Serial Server must have a default gateway IP
address to forward the TCP/IP packet to another segment of the
network.

GetLocalNetmask()
Description: Get the Serial Server’s NET-MASK value.
Include: tcpapi.h, tcpdef.h
Usage: int GetLocalNetmask (char far *buf )
Input Parameters: buf: buffer to store the NET-MASK value. The buffer must be
more than 4 bytes.
Return Value: =SUCCESS :successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* Get the Serial Server’s NET-MASK value */
uchar buf[4];
GetLocalNetmask ( buf ); // assume NET-MASK value
// buf[0] = 0xFF // = 255.255.255.0
// buf[1] = 0xFF
// buf[2] = 0xFF
// buf[3] = 0x00
Remarks: This function is used to get the NET-MASK value. Each Serial
Server must have a NET-MASK value.

GetHostName()
Description: Get the name of this device.
Include: tcpapi.h, tcpdef.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: int GetHostName (char far *buf )


Input Parameters: buf: buffer to store the device name. The buffer size is 16+1 bytes
including null terminator.
Return Value: =SUCCESS :successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* Get the device name */
char buf[16+1];

GetHostName ( buf );

Remarks: This host name is as the DHCP host name and the SNMP system
name.

GetDhcp()
Description: Check the DHCP mode being enable or disable.
Include: tcpapi.h, tcpdef.h
Usage: int GetDhcp (char far *dhcp_mode )
Input Parameters: dhcp_mode: The address of DHCP mode.
Return Value: =SUCCESS :successful
others : error, the definition of ERROR number please refer to the
file “tcpdef.h”
Link: dapapi.dll
Example: /* Set the device name */
char dhcp_mode;

GetDhcp( &dhcp_mode);
If ( dhcp_mode==0) {
/* DHCP is disable */
}
else {
/* DHCP is enable */
}
Remarks: After calling this function, if the value is 0 then the DHCP is disable and
the IP address being gotten from GetLocalIPAddr() is a fixed IP,
otherwise the DHCP is enable and the IP address is a dynamic IP..

GetConnectStatus()
Description: Get the status of a TCP connection.
Include: tcpapi.h, tcpdef.h
Usage: int GetConnectStatus (int handle, struct connectStatus far *conn_status )
Input handle: the unique handle of the established connection.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

conn_status the address of the connectStatus structure.


Return Value: =SUCCESS :successful
others : error, the definition of ERROR number please refer to the file
“tcpdef.h”
Link: dapapi.dll
Example: /* Get the status of a connection */
int handle;
struct connectStatus conn_status ;

/* open the connection */



if ( GetConnectStatus( handle, &conn_status)) == SUCCESS )
{
/* ok */
}
else
{
/* failed */
}

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: About the connectStatus structure, please refer tcpdef.h.


struct ConnectStatus
{
unsigned long local_address; /* my IP address */
unsigned short local_port; /* my port number */
unsigned long remote_address; /* remote IP address */
unsigned short remote_port; /* remote port number */
unsigned short state; /* connection state (refer below) */
unsigned short reserved; /* reserved */
};

/* The connection state includes followings: */


#define CONNECTION_FREED 1
#define CONNECTION_CLOSED 1
#define CONNECTION_LISTEN 2
#define CONNECTION_SYN_SENT 3
#define CONNECTION_SYN_RECEIVED 4
#define CONNECTION_ESTABLISHED 5
#define CONNECTION_FINWAIT1 6
#define CONNECTION_FINWAIT2 7
#define CONNECTION_CLOSE_WAIT 8
#define CONNECTION_LAST_ACK 9
#define CONNECTION_CLOSING 10
#define CONNECTION_TIME_WAIT 11

/* In general,
state = 1 : connection is closed
state = 2, 3, 4 : wait to connect or to be connected
state = 5 : connection is ok
state = 6,7,8,9,10,11: connection is closing
*/

SetLocalIPAddr()
Description: Get the local device’s IP address.
Include: tcpapi.h, tcpdef.h
Usage: int SetLocalIPAddr ( unsigned long ip )
Input Parameters: ip new IP address.
Return Value: =SUCCESS : successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* set a new IP address = 10.0.0.1*/
unsigned long ip;

ip = 0x100000a ;
SetLocalIPAddr ( ip ); // assume IP address = 10.0.0.1

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 5
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: This function is used to set the Serial Server local IP address. Each
Serial Server must have its own unique IP address to identify the
node on the local network via TCP/IP.

SetDefaultGateway()
Description: Set the Serial Server’s default gateway address.
Include: tcpapi.h, tcpdef.h
Usage: int SetDefaultGateway (unsigned long gateway)
Input Parameters: gateway new gateway address.
Return Value: =SUCCESS : successful
others : error, the definition of ERROR number please refer to the
file “tcpdef.h”
Link: dapapi.dll
Example: /* set a new gateway = 10.0.0.254*/
unsigned long gateway;

gateway = 0xFE00000a ;
SetDefaultGateway ( gateway);

Remarks: This function is used to set the default gateway IP address in the Serial
Server. Each Serial Server must have a default gateway IP address to
forward the TCP/IP packet to another segment of the network.

SetLocalNetmask()
Description: Set the Serial Server’s NET-MASK value.
Include: tcpapi.h, tcpdef.h
Usage: int SetLocalNetmask (unsigned long netmask)
Input Parameters: netmask new netmask
Return Value: =SUCCESS :successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* set a new netmask = 255.255.255.0 */
unsigned long netmask;

netmask = 0x00FFFFFF ;
SetLocalNetmask ( netmask);

Remarks: This function is used to set the NET-MASK value. Each Serial
Server must have a NET-MASK value.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

SetHostName()
Description: Set a name for the device.
Include: tcpapi.h, tcpdef.h
Usage: int SetHostName (char far *buf )
Input Parameters: buf: buffer to store the device name. The buffer size is 16+1 bytes
including null terminator.
Return Value: =SUCCESS :successful
others : error, the definition of ERROR number please refer to
the file “tcpdef.h”
Link: dapapi.dll
Example: /* Set the device name */
char buf[16+1];

strcpy( buf, “host-001”) ;


SetHostName ( buf );

Remarks: This host name is as the DHCP host name and the SNMP system
name.

6.5 Memory Functions

EraseSector()
Description: Erase one sector of flash memory.
Include: dapapi.h,
Usage: ushort EraseSector ( uchar sector_id )
Input Parameters: sector_id: sector id to be erase.

For GW21x-maxi, GW21R: (one sector is 64K bytes)


id sector erase range
0 0x8000 0x8000:0000 ~ 0x8000:FFFF
1 0x9000 0x9000:0000 ~ 0x9000:FFFF
2 0xA000 0xA000:0000 ~ 0xA000:FFFF
3 0xB000 0xB000:0000 ~ 0xB000:FFFF
4 0xC000 0xC000:0000 ~ 0xC000:FFFF
5 0xD000 0xD000:0000 ~ 0xD000:FFFF

For GW21L, GW21W: (one sector is 128K bytes)


id sector erase range
0 0x8000 0x8000:0000 ~ 0x9000:FFFF
1 0x9000 0x8000:0000 ~ 0x9000:FFFF
2 0xA000 0xA000:0000 ~ 0xB000:FFFF
3 0xB000 0xA000:0000 ~ 0xB000:FFFF
4 0xC000 0xC000:0000 ~ 0xD000:FFFF
5 0xD000 0xC000:0000 ~ 0xD000:FFFF

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Return Value: 0 : successful


others : error
Link: dapapi.dll
Example: /* Erase A000:0000 ~ A000:FFFF for GW21x-maxi */
ushort ret ;

ret = EraseSector(2);
if ( ret ) {
// fatal error found !!!
}
else {
// OK !!!
}
Remarks: This function is used to erase one sector of flash memory. For
GW21x-maxi and GW21R, one sector is 64K bytes. But For GW21L
and GW21W, one sector is 128K bytes.

ProgOneByte()
Description: Write one byte into flash memory.
Include: dapapi.h
Usage: ushort ProgOneByte ( uchar data, uchar *addr)
Input Parameters: data byte value being written into flash memory
addr address of flash memory.

Note:
1. The segment value of the address must be 64k boundary,
it means that the legal segment value is 0x8000、
0x9000....or 0xD000. For example: the address D000:F000
is correct but DF00:0 is error.

2. All addresses in flash memory can be written only one


times. If you want to write the second times, you must call
EraseSector() function to erase the sector:64K (or 128K)
and then you can write the data to flash memory.
Return Value: 0 : successful
others : error
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: /* Write 0x55 at address A000:1234. */


ushort ret ;
uchar *addr;

addr = (uchar *) 0xA0001234 ;


/* the segment value of addr must be within 64k boundary,
it means that the legal segment value is 0x8000、0x9000... or
0xD000 */
ret = ProgOneByte( 0x55, addr ) ;
if ( ret ) {
// fatal error found !!!
}
else {
// OK !!!
}

Remarks: This function is used to write one byte data into the flash memory.

ProgString()
Description: Write a string of data into flash memory.
Include: dapapi.h
Usage: ushort ProgString ( ushort len, uchar *src, uchar *dest )
Input Parameters: len byte count of data
src source address of data
dest destination address of flash memory.

Note:
1. The segment value of the address must be 64k boundary,
it means that the legal segment value is 0x8000、
0x9000....or 0xD000. For example: the address D000:F000
is correct but DF00:0 is error.

2. All addresses in flash memory can be written only one


times. If you want to write the second times, you must call
EraseSector() function to erase the sector:64K (or 128K)
and then you can write the data.
Return Value: 0 : successful
others : error
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: /* Write "1234567890" at address A000:1234. */

ushort ret;
uchar *addr;

addr = (uchar *) 0xA0001234 ;


/* the segment value of addr must be within 64k boundary,
it means that the legal segment value is 0x8000、0x9000....or
0xd000 */
ret = ProgString( 10, "1234567890", addr) ;
if ( ret ) {
// fatal error found !!!
}
else {
// OK !!!
}

Remarks: This function is used to write a block of data into the flash memory.

ReadEEPROM()
Description: Read one word from EEPROM.
Include: dapapi.h,
Usage: ushort ReadEEPROM ( ushort index )
Input Parameters: index word index, address range (0 ~ 151), 152 words
Return Value: value being stored at the address of index in the EEPROM
Link: dapapi.dll
Example: /* Read the 5th word data in the serial EEPROM. */
ushort ee_data;
ee_data = ReadEEPROM ( 4 );

Remarks: This function is used to read one word data stored in the EEPROM.

WriteEEPROM()
Description: Write one word data into EEPROM.
Include: dapapi.h
Usage: void WriteEEPROM ( ushort id, ushort data )
Input Parameters: index word identifier, address range (0 ~ 151), 152 words
data value being written into the EEPROM
Return Value: None
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: /* Write the 24th word data with 0x1234 into the EEPROM. */

WriteEEPROM( 23, 0x1234 );

Remarks: This function is used to write one word data into the EEPROM.

6.6 Watch Dog Timer Functions

WatchDogCtrl()
Description: choose to control the watch-dog by the system or user.
Include: dapapi.h
Usage: void WatchDogCtrl(uchar ch);
Input Parameters: ch; = 0 : watch-dog controls by system automatically.
= 1 : watch-dog controls by user’s application program.
Return Value: None
Link: dapapi.dll
Example: /* System will clear the watch-dog timer automatically. */
WatchDogCtrl(0x00);

/* User must clear the watch-dog timer every one second. */


WatchDogCtrl(0x01);

Remarks: Default the watch dog timer is control by system kernel.


If you set to let user’s application program to handle the watch-dog
timer then the application program must call ClrWatchDogTime() to
clear the watch-dog timer by schedule to prevent system been reset
by watch-dog timer.

ClrWatchDogTime()
Description: clear watch dog timer.
Include: dapapi.h
Usage: void ClrWatchDogTime(void);
Input Parameters:
Return Value: None
Link: dapapi.dll
Example: /* Clear watch-dog timer. */
ClrWatchDogTime();

Remarks: When the watch dog timer is set to be controlled by user, the AP
must call this function to clear watch dog timer before watch dog is
timeout to reset the system.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

SetupWatchDogTime()
Description: Set the timeout time of watch dog timer.
Include: dapapi.h
Usage: void SetupWatchDogTime(ushort value);
Input Parameters: value: timeout time of watch dog timer by msec. (0x0000 -
0xFFFF). Timeout time = (value * 55 msec) + 1.6 sec.
Return Value: none
Link: dapapi.dll
Example: /* Watch Dog Time out = watch-dog-time * 55 msec + 1.6 sec
if watch-dog-time = 1000 (=0x3e8), then the timeout = 56.6 sec. */

SetupWatchDogTime( 0x3e8);

Remarks: When the watch dog timer is set to be controlled by user, the AP
must call ClrWatchDogTime() function to clear watch dog timer
before this timeout time is reached. Otherwise the system will be
reset.

6.7 Advanced Serial Port API (in comapi.obj)

Com_Config()
Description: Initialize the serial port. Including ISR, baud rate, data bit, parity and stop
bit
Include: comapi.h
Usage: int Com_Config (int iport, unsigned long lBaudRate, int iParity, int
iData, int iStop)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
lBaudRatet: Baud Rate: 1200, 2400, 4800, 9600, 19200, 38400, 57600,
115200.
iparity: Parity check: 0:none, 1:odd parity, 2:even parity, 3:mark
parity, 4:space parity
iData: Data bits: 7:7 data bits, 8:8 data bits
iStop: Stop bits: 1:1 stop bit, 2:2 stop bits
Return Value: 0: ok, others: parameters error
Link: comapi.obj

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: com_port = 0 ; // 0:COM1


ret=Com_Config(com_port, 9600L, 0, 8, 1); //open com port: 9600,n,8,1
if (ret != 0)
{
//com port open error
}
Remarks:

Com_GetBuf()
Description: Get data from receiving buffer (automatically received by ISR).
Include: comapi.h
Usage: int Com_GetBuf (int iport, char *buf, int cnt)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
buf: Give a buffer for storing received data.
cnt: Size of buffer.
Return Value: Count of received data in buffer, 0: no data.
Link: comapi.obj
Example: Char buf[256] ;
com_port = 0 ; // 0:COM1
cnt = 256 ;
ret = Com_GetBuf( com_port, buf, cnt) ; //get data from com port
if (ret>0) { //receive data
receive_count = ret ;
}
else {
//receive no data
}
Remarks:

Com_PutBuf()
Description: Put data to sending buffer (automatically sent by ISR).
Include: comapi.h
Usage: int Com_PutBuf (int iport, char *buf, int cnt)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

buf: a buffer for storing sent data.


cnt: Size of buffer.
Return Value: ret=0:sent buffer in ISR is not enough, >0:put data to sent buffer is ok.
Link: comapi.obj
Example: char buf[256] ;
com_port = 0 ; // 0:COM1
strcpy( buf, "\r\nReceive data=") ;
cnt = strlen( buf) ;
ret = Com_PutBuf( com_port, buf, cnt) ; //put data to com port
if (ret<=0) {
//error, sent buffer of ISR is not enough
}
else {
//send ok
}
Remarks:

Com_ClearRxQue()
Description: Clear receiving buffer.
Include: comapi.h
Usage: void Com_ClearRxQue (int iport)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
Return Value: none.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
Com_ClearRxQue( com_port) ;
Remarks:

Com_ClearTxQue()
Description: Clear sending buffer.
Include: comapi.h
Usage: void Com_ClearTxQue (int iport)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
Return Value: none.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
Com_ClearTxQue( com_port) ;
Remarks:

Com_TxQueIsEmpty()
Description: heck sending buffer is empty or not.
Include: comapi.h
Usage: int Com_TxQueIsEmpty (int iport)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
Return Value: 0:not empty, <>0:empty.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
ret = Com_TxQueIsEmpty( com_port) ;
if (ret==0) {
//not empty
}
else {
//is empty
}
Remarks:

Com_DTR_Active()
Description: Control the DTR signal.
Include: comapi.h
Usage: void Com_DTR_Active (int iport, int active)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
active: 0: inactive, 1: active
Return Value: none.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
active = 1
Com_DTR_Active( com_port, active) ;
Remarks:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 6
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Com_RTS_Active()
Description: Control the RTS signal.
Include: comapi.h
Usage: void Com_RTS_Active (int iport, int active)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
active: 0: inactive, 1: active
Return Value: none.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
active = 1
Com_RTS_Active( com_port, active) ;
Remarks:

Com_DSR_Status()
Description: Get the status of DSR signal.
Include: comapi.h
Usage: int Com_DSR_Status (int iport)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
Return Value: 0: inactive, <>0: active.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
status = Com_DSR_Status( com_port) ;
Remarks:

Com_CTS_Status()
Description: Get the status of CTS signal.
Include: comapi.h
Usage: int Com_CTS_Status (int iport)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
Return Value: 0: inactive, <>0: active.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
status = Com_CTS_Status( com_port) ;
Remarks:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Com_RI_Status()
Description: Get the status of RI signal.
Include: comapi.h
Usage: int Com_RI_Status (int iport)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
Return Value: 0: inactive, <>0: active.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
status = Com_RI_Status( com_port) ;
Remarks:

Com_DCD_Status()
Description: Get the status of DCD signal.
Include: comapi.h
Usage: int Com_DCD_Status (int iport)
Input Parameters: iport: serial port number of Serial Server, 0: 1st port, 1:2nd port
Return Value: 0: inactive, <>0: active.
Link: comapi.obj
Example: com_port = 0 ; // 0:COM1
status = Com_DCD_Status( com_port) ;
Remarks:

6.8 Digital Input and Digital Output Functions

(Note: These functions are only for GW21S-maxi, SE5001 or GW26A)

SetIOMode()
Description: Set a channel to be as DI or DO.
(only for GW21S-maxi, and SE5001)
Include: dapapi.h
Usage: void SetIOMode( uchar channel, uchar mode) ;
Input Parameters: channel: = 0~3 for channel 1~4
mode: = 0: as DO, 1: as DI
Return Value: None
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: SetIOMode(0,1); // set channel 1 to DI


SetIOMode(1,0); // set channel 2 to DO
Remarks:

GetIOMode()
Description: Get a channel’s I/O mode.
(only for GW21S-maxi, and SE5001)
Include: dapapi.h
Usage: ushort GetIOMode( uchar channel) ;
Input Parameters: channel: /= 0~3 for channel 1~4
Return Value: = 0:DO, 1:DI
Link: dapapi.dll
Example: mode1 = GetIOMode(0); // get channel-1’s mode
mode4 = GetIOMode(3); // get channel-4’s mode
Remarks:

SetOutState()
Description: Set the DO channel’s state (on/off).
(only for GW21S-maxi, and SE5001)
Include: dapapi.h
Usage: void SetOutState(uchar channel, uchar state) ;
Input Parameters: channel: = 0~3 for channel 1~4
State: = 0:DO off, 1:DO on
Return Value: None
Link: dapapi.dll
Example: SetOutState(0, 1); // set channel-1 DO to be on
SetOutState(0, 0); // set channel-1 DO to be off
Remarks:

GetInState()
Description: Get the DI/DO channel’s state (on/off).
(only for GW21S-maxi, and SE5001)
Include: dapapi.h
Usage: ushort GetInState( uchar channel) ;
Input Parameters: channel: = 0~3 for channel 1~4
Return Value: =0:off, 1:on
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: state1 = GetInState(0); // get channel-1 DI/DO state


state4 = GetInState(3); // get channel-4 DI/DO state
Remarks:

ReadDigitalInput()
Description: get data from the digital input port.
(only for GW26A)
Include: dapapi.h
Usage: ushort ReadDigitalInput(void);
Input Parameters: void
Return Value: An unsigned short value (2 bytes, contain 16 digital-input values)
Bit 0 ~ Bit 11 are for DI-1 ~ DI-12.
Example: DigitalInput_value = ReadDigitalInput();
Remarks:

SetupRelayOutput()
Description: Control relay outputs to on/off.
(only for GW26A)
Include: dapapi.h
Usage: void SetupRelayOutput(uchar relay);
Input Parameters: An unsigned char value (1 bytes, contain 8 digital-output values)
Bit 0 ~ Bit 7 are for DO-1 ~ DO-8.
Return Value: None
Example: //Enable relay output port Number 1 and Number 2.
SetupRelayOutput(0x03);
Remarks:

6.9 Time Functions

ReadRTCTime()
Description: Read the system time from the RTC.
(only for GW21C-maxi, GW21S-maxi, GW26A)
Include: dapapi.h
Usage: ulong ReadRTCTime(void);
Input Parameters: Void
Return Value: RTC time (hour/min/second) with BCD format.
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: unsigned long Sys_Time


;
Sys_Time = ReadRTCTime();
//if the time is (22:34:56) then Sys_Time will be 0x00223456
Remarks:

ReadRTCDate()
Description: Read the system Date from the RTC.
(only for GW21C-maxi, GW21S-maxi, GW26A)
Include: dapapi.h
Usage: ulong ReadRTCDate(void);
Input Parameters: Void
Return Value: RTC date (week/day/month/year) with BCD format.
Link: dapapi.dll
Example: unsigned long Sys_Date ;

Sys_Date = ReadRTCDate();
//if the Date is (2001/03/16 Friday) then Sys_Date will be
0x05160301
Remarks:

WriteRTCTime()
Description: Write the time to the RTC as system time.
(only for GW21C-maxi, GW21S-maxi, GW26A)
Include: dapapi.h
Usage: void WriteRTCTime( uchar hour, uchar minute, uchar
second);
Input Parameters: With BCD format
hour : 0x00 ~ 0x23 (24hrs, BCD format).
minute : 0x00 ~ 0x59 (60 minutes, BCD format).
second : 0x00 ~ 0x59 (60 seconds, BCD format).
Return Value: None
Link: dapapi.dll
Example: //if set the time to (14:23:59)
WriteRTCTime(0x14, 0x23, 0x59);
Remarks:

WriteRTCDate()
Description: Write the Date to the RTC as system date.
(only for GW21C-maxi, GW21S-maxi, GW26A)
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: void WriteRTCDate( uchar year, uchar month, uchar day,


uchar weekday);
Input Parameters: With BCD format
year : 0x00 ~ 0x99 (2000 ~ 2099 years, BCD format).
month : 0x01 ~ 0x12 (1 ~ 12 month, BCD format).
day : 0x01 ~ 0x31 (1 ~ 31 day, BCD format).
weekday : 0x01 ~ 0x07 (1 ~ 7 weekday, BCD format).
Return Value: None
Link: dapapi.dll
Example: //if set the date to (2001/03/16 F)
WriteRTCTime(0x01, 0x03, 0x16, 0x05);
Remarks:

EnableWRProtect()
Description: Enable RTC write protect. (user can only read RTC but can not
write it).
(only for GW21C-maxi, GW21S-maxi, GW26A)
Include: dapapi.h
Usage: void EnableWRProtect(void);
Input Parameters: void
Return Value: None
Link: dapapi.dll
Example: EnableWRProtect();
Remarks:

DisableWRProtect()
Description: Disable RTC write protect. (user can read and write RTC).
(only for GW21C-maxi, GW21S-maxi, GW26A)
Include: dapapi.h
Usage: void DisableWRProtect(void);
Input Parameters: void
Return Value: None
Link: dapapi.dll
Example: DisableWRProtect();
Remarks:

GetStartupTime()
Description: Get a time interval from system startup. It is by mini-second.
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: ulong GetStartupTime( void);


Input Parameters: None
Return Value: a time interval from system startup. It is by mini-second.
Link: dapapi.dll
Example: ulong sys_msec ;

sys_msec = GetStartupTime();

Remarks: The max. time interval is 2^32-1 min-second. It is about 49 days.


After the max. value, it will count from 0 again.
This function is the same as GetSysMsec()

GetSysMsec()
Description: Get a time interval from system startup. It is by mini-second.
Include: dapapi.h
Usage: ulong GetSysMsec( void);
Input Parameters: None
Return Value: a time interval from system startup. It is by mini-second.
Link: dapapi.dll
Example: ulong sys_msec ;

sys_msec = GetSysMsec();

Remarks: The max. time interval is 2^32-1 min-second. It is about 49 days.


After the max. value, it will count from 0 again.
This function is the same as GetStartupTime()

GetSysTick()
Description: Get a time interval from system startup. It is by tick (55ms).
Include: dapapi.h
Usage: ulong GetTickTime ( void);
Input Parameters: None
Return Value: a time interval from system startup. It is by tick.
Link: dapapi.dll
Example: ulong sys_sec ;

sys_sec = GetTickTime ();

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: The max. time interval is 2^32-1 tick. It is about 2734 days (about
7.5 years).

GetSysSec()
Description: Get a time interval from system startup. It is by second.
Include: dapapi.h
Usage: ulong GetSysSec( void);
Input Parameters: None
Return Value: a time interval from system startup. It is by second.
Link: dapapi.dll
Example: ulong sys_sec ;

sys_sec = GetSysSec();
Remarks: The max. time interval is 2^32-1 second. It is about 49710 days
(about 136 years).
This function is the same as GetSysTime()

GetSysTime()
Description: Get a time interval from system startup. It is by second.
Include: dapapi.h
Usage: ulong GetSysTime( void);
Input Parameters: None
Return Value: a time interval from system startup. It is by second.
Link: dapapi.dll
Example: ulong sys_sec ;

sys_sec = GetSysTime();
Remarks: The max. time interval is 2^32-1 second. It is about 49710 days
(about 136 years).
This function is the same as GetSysSec()

6.10 Account Functions

AddAccount()
Description: System provides some API to maintain user names and password.
This function is to add a user name and password. There are
maximum 4 user accounts which are kept in system.
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: ushort AddAccount(char *name_pwd);


Input Parameters: name_pwd : a string includes user name and password which is
separated with blank. User name is maximum 6
characters. Password is maximum 20 characters.
Both can not include blank characters.
Return Value: =0: overflow, already have 4 user accounts in system
=1: the user name already exist, it will change the password
=2: add a new user account ok
Link: dapapi.dll
Example: char namepwd[80], password[20];

sprintf( namepwd, "%s %s", "admin", password);
ret=AddAccount( namepwd);
if (ret==0) {
//overflow
}
else {
//add ok
}
Remarks: For example: the user name is “john” and his password is “12345”,
then the name_pwd will be “john 12345”.

DelAccount()
Description: This function is to delete a user account.
Include: dapapi.h
Usage: ushort DelAccount(char *name);
Input Parameters: name : a user name string
Return Value: 1 : delete successful
other: error, the user name not exist
Link: dapapi.dll
Example: …
ret=DelAccount( name);
if (ret==0) {
//error
}
else {
//ok
}
Remarks:

VerifyAccount()
Description: This function is to verify a user name and password.
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: ushort VerifyAccount(char *name_pwd);


Input Parameters: name_pwd : a string includes user name and password
seperated with blank.
Return Value: 0 : error, ether user name not exist or password not correct
1 : verify ok
Link: dapapi.dll
Example: char namepwd[80], password[20];

sprintf( namepwd, "%s %s", "admin", password);
ret=VerifyAccount(namepwd);
if (ret==0) {
//error, not correct
}
else {
//verify ok
}
Remarks: For example: the user name is “john” and his password is
“12345”, then the name_pwd will be “john 12345”

6.11 Wireless LAN Command Functions


(This section’s functions are only for GW21W-maxi, GW21W)

GetESSID()
Description: Get the Extended Service Set ID
Include: dapapi.h
Usage: void GetESSID ( uchar *buf);
Input Parameters: buf: buffer for storing the extended service set ID
Return Value: None
Link: dapapi.dll
Example: GetESSID(buf);
Remarks: buf must be a characters array of 32 bytes

SetESSID()
Description: Set the Extended Service Set ID
Include: dapapi.h
Usage: void SetESSID ( uchar *buf);
Input Parameters: buf: buffer for setting the extended service set ID
Return Value: None
Link: dapapi.dll
Example: SetESSID(buf);
Remarks: buf must be a characters array of 32 bytes

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 7
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

GetFrame()
Description: Get frame length used for RTS/CTS handshake control
Include: dapapi.h
Usage: uint GetFrame (void);
Input Parameters: None
Return Value: Frame length
Link: dapapi.dll
Example: GetFrame();
Remarks: Default:2432

SetFrame()
Description: Set frame length used for RTS/CTS handshake control
Include: dapapi.h
Usage: void SetFrame (uint frame_size);
Input Parameters: Frame_size
Return Value: None
Link: dapapi.dll
Example: SetFrame(frame_size);
Remarks: Frame_size value is from 0..3000

GetFragment()
Description: Get fragment length for unicast message transmission
Include: dapapi.h
Usage: uint GetFragment (void);
Input Parameters: None
Return Value: Fragment length
Link: dapapi.dll
Example: GetFragment();
Remarks: Default:2346

SetFragment()
Description: Set fragment length for unicast message transmission
Include: dapapi.h
Usage: void SetFragment (uint fragment_size);
Input Parameters: Framment_size
Return Value: None

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Link: dapapi.dll
Example: SetFragment(fragment_size);
Remarks: Fragment_size value is from 256..2346

GetChannel()
Description: Get communication channel number for BSS creation
Include: dapapi.h
Usage: Uchar GetChannel (void);
Input Parameters: None
Return Value: Channel No., The value is from 1..14
Example: GetChannel();
Remarks: The default channel is 3.

SetChannel()
Description: Set communication channel number for BSS creation
Include: dapapi.h
Usage: void SetChannel (uchar channel_no);
Input Parameters: Channel_no
Return Value: None
Link: dapapi.dll
Example: SetChannel(channel_no);
Remarks: Channel_no value is from 1..14

GetNetWorkType()
Description: Get the topology of WLAN.
Include: dapapi.h
Usage: Uchar GetNetWorkType (void);
Input Parameters: None
Return Value: Network Type : 1:InfraStructure mode, 3:Ad-hoc mode;
Link: dapapi.dll
Example: ret = GetNetWorkType();
If (ret==1) {
/* infrastructure mode */
}
else {
/* ad-hoc mode */
}

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: The default topology is infrastructure mode.

SetNetWorkType()
Description: Set the topology of WLAN.
Include: dapapi.h
Usage: void SetNetWorkType (uchar networktype);
Input Parameters: Networktype : 1:InfraStructure mode, 4:Ad-hoc mode
Return Value: None
Link: dapapi.dll
Example: /* set to ad-hoc mode */
networktype = 4;
SetNetWorkType( networkrtype);
Remarks: Network Type : 1:InfraStructure mode, 4:Ad-hoc mode.

GetTransmitRate()
Description: Get data rate for message transmission
Include: dapapi.h
Usage: Uchar GetTransmitRate (void);
Input Parameters: None
Return Value: Transmit rate : 1:1Mb, 2:2Mb, 3:Auto 1/2, 4:5.5Mb, 8:11Mb, 15:Full Auto
Link: dapapi.dll
Example: Ret =n GetTransmitRate();
Remarks: The default is 15 (Full auto)

SetTransmitRate()
Description: Set data rate for message transmission
Include: dapapi.h
Usage: void SetTransmitRatee (uchar transmit_rate);
Input Parameters: Transmit_rate : 1,2,3,4,8 or 15
Return Value: None
Link: dapapi.dll
Example: SetTransmitRate(transmit_rate);
Remarks: Transmit Rate : 1:1Mb, 2:2Mb, 3:Auto 1/2, 4:5.5Mb, 8:11Mb, 15:Full Auto

GetXI300WEPFlag()
Description: Get WEP flag
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: ushort GetXI300WEPFlag(void)


Input Parameters: None
Return Value: WEP flag : 0:Disable WEP,
1 : use WEP 1
2 : use WEP 2
3 : use WEP 3
4: use WEP 4
Link: dapapi.dll
Example: Flag = GetXI300WEPFlag ();
Remarks: Default:0

SetXI300WEPFlag()
Description: Set WEP flag
Include: dapapi.h
Usage: void SetXI300WEPFlag(uchar type);
Input Parameters: WEP flag : 0,1,2,3,4
Return Value: None
Link: dapapi.dll
Example: SetXI300WEPFlag (wep_flag);
Remarks: WEP flag : 0:Disable WEP,
1 : use WEP 1
2 : use WEP 2
3 : use WEP 3
4: use WEP 4

GetXI300WEPBit()
Description: Get WEP bits
Include: dapapi.h
Usage: ushort GetXI300WEPBit(void);
Input Parameters: None
Return Value: WEP Bit : 0 : 64 Bits,
1 : 128 Bits
Link: dapapi.dll
Example: Wep_bits = GetXI300WEPBit ();
Remarks: Default:1

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

SetXI300WEPBit()
Description: Set WEP bits
Include: dapapi.h
Usage: void SetXI300WEPBit(uchar type);
Input Parameters: WEP bit : 0,1
Return Value: None
Link: dapapi.dll
Example: SetXI300WEPBit (WEP_Bit);
Remarks: WEP Bit : 0 : 64 Bits,
1 : 128 Bits

GetXI300WEPKey()
Description: Get WEP Key
Include: dapapi.h
Usage: void GetXI300WEPKey(ushort index, uchar *buf);
Input Parameters: index: the index of WEP Key, 1~4
buf : the buffer for storing the WEP Key
Return Value: None
Link: dapapi.dll
Example: GetXI300WEPKey( id, buf) ;
Remarks: WEP64 is 5-Byte data. WEP128 is 13-Byte data

Set XI300WEPKey()
Description: Set WEP Key
Include: dapapi.h
Usage: void void SetXI300WEPKey( ushort index, uchar *buf) ;
Input Parameters: index: index of WEP Key, 1~4
buf : the WEP Key being set
Return Value: None
Link: dapapi.dll
Example: /* if the WEP key is 64 bits, set the 2nd WEP key to “12345” */
SetXI300WEPKey( 2, “12345”);
Remarks: WEP64 is 5-Byte data, WEP128 is 13-Byte data

GetRFRealChannel()
Description: Get the RF channel when linking with Access Point
Include: dapapi.h

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Usage: int GetRFRealChannel(void);


Input Parameters: None
Return Value: RF channel : 1..14
Link: dapapi.dll
Example: ret = GetRFRealChannel();
Remarks:

GetRFTXRate()
Description: Get the real transmission rate when linking with Access Point
Include: dapapi.h
Usage: int GetRFTxRate(void);
Input Parameters: None
Return Value: Transmit_rate : 1,2,3,4,8
Link: dapapi.dll
Example: GetRFTxRate ();
Remarks: Transmit Rate : 1:1Mb,2:2Mb,3:Auto 1/2,4:5.5Mb,8:11Mb,

GetAPName()
Description: Get the name of Access Point which linking with
Include: dapapi.h
Usage: void GetAPName(uchar * buf);
Input Parameters: buf: the AP name
Return Value: None
Link: dapapi.dll
Example: char buf[64] ;
GetAPName( buf);
Remarks:

GetAPMac()
Description: Get the MAC address of Access Point which linking with
Include: dapapi.h
Usage: void GetAPName(uchar * buf);
Input Parameters: buf:AP mac address
Return Value: None
Link: dapapi.dll
Example: char buf[64] ;
GetAPName( buf);

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks:

LanRestart()
Description: Reset and restart the wireless LAN driver
Include: dapapi.h
Usage: void LanRestart(void);
Input Parameters: None
Return Value: None
Link: dapapi.dll
Example: LanRestart ();
Remarks:

WGetMacStatus()
Description: Get the WLAN connection status.
Include: dapapi.h
Usage: int WGetMacStatus(void);
Input Parameters: None
Return Value: = 1: Disabled
= 2: Searching for initial connection
= 3: Connected to IBSS
= 4: Connected to ESS
= 5: Out of Range
others: unknown error
Link: dapapi.dll
Example: Ret = WGetMacStatus();
Remarks:

WGetBssQuality()
Description: Get communication quality of the WLAN.
Include: dapapi.h
Usage: unsigned short * WGetBssQuality(void);
Input Parameters: None
Return Value: The return value is an unsigned short pointer to a 2 word array.
The 1st word is the communication quality. Its range is from 0 to 92.
The 2nd word is the signal strength. Its range is from 27 to 154.
Link: dapapi.dll

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Example: unsigned short *uptr ;


uptr = WGetBssQuality();
quality = uptr[0]*100/92 ; //quality percentage
strength = (uptr[1]-27)*100/127; //strength percentage
Remarks:

GetCountry()
Description: About WLAN, different countries have different legal channels. When
manufacturing, every device was assigned a country code which can be use
to decide legal channels.
Include: dapapi.h
Usage: int GetCountry(void);
Input Parameters: None
Return Value: = 0: USA
= 1: Europe
= 2: Japan
= 3: France
= 4: Spain
others: unknown error
Link: dapapi.dll
Example: ret = GetCountry();
Remarks:

GetLegalChannel()
Description: About WLAN, different countries have different legal channels. When
manufacturing, every device was assigned a country code which can be
used to decide legal channels.
Include: dapapi.h
Usage: unsigned short GetLegalChannel( int country_code);
Input Parameters: country_code: this is gotten from GetCountry()
Return Value: The return value is a 16 bits unsigned short. Each bit represents a channel.
The bit 0 to bit 13 is as channel 1 to channel 14.
country_code= 0: USA, channel 1-11, return value=0x7FF
country_code = 1: Europe, channel 1-13, return value=0x1FFF
country_code = 2: Japan, channel 1-14, return value=0x3FFF
country_code = 3: France, channel 10-13, return value=0x1E00
country_code = 4: Spain, channel 10-11, return value=0x600

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Link: dapapi.dll
Example: ret = GetLegalChannel( 2); /* get the legal channels of Japan */
Remarks: Followings are legal channels of countries:
USA:1-11, Europe:1-13, Japan:1-14, France:10-13, Spain:10-11

WscanRequest()
Description: Request to scan Basic Service Set (BSS). In general it is to search Access
Points (AP).
Include: dapapi.h
Usage: Int WScanRequest( unsigned short *buf, int buf_size);
Input Parameters: buf : address of an unsigned short array for storing responded Access
Points information.
buf_size: count of the unsigned short array. The byte count of the array is
buf_size * 2.
Return Value: <0 : this request is failed. Maybe the previous request is still pending.
>=0: system accepts this request..
Link: dapapi.dll
Example:

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Remarks: After calling this function, you must call WscanReady() continually to check
the result is ready or not. When it is ready, the buf will be stored following
data:
Word offset Field description Size(words)
0 Data length= 3+31*n, (n=0,1…) 1
1 reserved 3
4 The 1st AP information 31

4+31*n The n-th AP information 31


About the Access Point’s information, please refer following structure:
typedef struct {
unsigned short channel ; /* BSS channel ID= 1~14 */
unsigned short noise ; /* BSS noise level= 0x1B~0x9A */
unsigned short signal ; /* BSS signal level= 0x1B~0x9A */
unsigned char macid[6] ; /* MAC address of the AP */
unsigned short beacon_time ; /* beacon interval, by ms */
unsigned short reserved ;
unsigned short ssid_len ; /* length of the BSS SSID */
unsigned char ssid[32] ; /* BSS SSID */
unsigned char support_rate[10] ; /* For each byte: 0x82: 1Mb, 0x84:
2Mb, 0x8B: 5.5Mb, 0x96: 11Mb,
0: terminator,
(data&0x7F)/2=(1/2/5.5/11Mb), */
unsigned short rcv_rate ; /* 0x0A: 1Mb, 0x14: 2Mb, 0x37:
5.5Mb, 0x6E: 11Mb,
data/10=(1/2/5.5/11Mb) */
} BSS_INFO ;

WscanReady()
Description: Check the searching Access Points request WscanRequest() is finished or
not.
Include: dapapi.h
Usage: Int WScanReady();
Input Parameters: None

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 8
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Return Value: -2 : Not request yet


-1 : Already request but not finish yet
>=0: the actual count of the unsigned short array which is prepared in
WscanRequest(). Its value will be 3+31*n. The n is the detected AP
count.
Link: dapapi.dll
Example: unsigned short tmpbuf[ 256] ;
ret = WScanRequest( tmpbuf, 256) ;
if ( ret < 0) {
/* error */
}
else {
for (;;) {
ret = WScanReady() ;
if (ret==-2) {
tmpstr = "not request. " ;
break ;
}
else
if (ret==-1) continue ; /* not ready yet */
else {
bss_cnt = (ret-3) / (sizeof(BSS_INFO)/2) ;
bssp = (BSS_INFO *)&tmpbuf[4] ;
for (i=0; i<bss_cnt; i++) {
j = bssp[i].ssid_len ;
if (j>32) j = 32 ;
memcpy( tmp_ssid, bssp[i].ssid, j) ;
tmp_ssid[ j] = '\0' ;
/* send out the SSID of this BSS */
} //for
} //else
} //for
} //else
Remarks: Please refer WscanRequest()

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Appendix A: Serial port configuration table


Baud Rate Table
Bit 7 6 5 Baud Rate (bps)
1 1 1 9600*
1 1 0 4800
1 0 1 2400
1 0 0 1200
0 1 1 115200
0 1 0 57600
0 0 1 38400
0 0 0 19200

Data Parity Table


Bit 4 3 Parity
1 1 Even
0 1 Odd
X 0 None*
X: Don’t care

Stop Bit Table


Bit 2 Number of stop bit
0 1*
1 2

Data Length Table


Bit 1 0 Number of data bit
1 1 8*
1 0 7
0 1 N/A
0 0 N/A

The default setting is (9600, n, 8, 1).


The baud rate 9600, none parity, 8-bit data, 1 stop bit 0xe3H

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Appendix B. Upgrade System Software


When new version of firmware released, customer may get a notice and a diskette containing the software
and upgrade procedure. Customer may decide to or not to upgrade the system’s firmware.

To do so, please follow the instructions listed below.

B.1 Upgrade Procedures


When you get a new software version, please follow the sequences below to upgrade your firmware of
GW21x-MAXI.

Step 1: Prepare a PC with Windows 95/98/NT/2000 and the GW21x-MAXI you wish to upgrade
the firmware and make sure they are all connected in the same TCP/IP network. Use
command ping or monitor.exe utility program to ensure their availability.

Step 2: Get the new version of software for the GW21x-MAXI. The new program (.HEX file) that
you want to download to the GW21x-MAXI should be found in the product CD or
diskette along with the shipping package or can be obtained from ATOP’s Web site.

Step 3: Prepare the download tool. The utility program gwdl.exe and its configuration file
dapdl.cfg can be found in the GW21x-MAXI product CD or diskette or from ATOP’s Web
site.

Step 4: Edit the "dapdl.cfg" file to fit your system and configurations. The format of the file
looks like the following. Be sure to save your modifications.

Remote_IP 10.0.21.128
Load Gw21Lterm23.hex

The first line identifies the IP address of GW21x-MAXI you want to download new
software to. The second line identifies the file name of to be downloaded.

Step 5: Execute the download utility program. Please go to Windows command mode or from
Windows, select the Run command in the Start menu and specify correct file path, then
execute the command <file_path>\gwdl dapdl.cfg.

Step 6: Type in user name and the password. After execute the download utility program, please
type in user name and the password as the following screen and new firmware will be
downloaded.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Step 7: Restart the GW21x-MAXI. The GW21x-MAXI will restart automatically each time the
program is successfully downloaded.

B.2 Critical Issues of Upgrading


1. You can always abort the upgrading process by pressing the <Esc> key from host PC during the
upgrading process. The GW21x-MAXI will restart itself automatically and the system remains
intact.

2. If the GW21x-MAXI does not receive any upgrade data within 30 seconds during process, the
GW21x-MAXI will restart itself automatically and the system remains intact.

3. After the upgrading process is finished, the GW21x-MAXI will program the flash memory.
During programming the flash memory, the GW21x-MAXI will clear the corresponding
memory and reset itself once an error occurs.

If everything is OK, the GW21x-MAXI's RUN LED blinks 3 times. It will then restart.
Normally, it will take around 10 seconds to complete programming the flash memory.

B.3 Error Messages


Software upgrade may not be successful if errors occur during the process.

Error Cause Message Comments


Illegal Hex file format Hex File Text Error
Hex File Check-Sum Error
Hex File Format Error
Hex File End of Record Error

GW21x-MAXI The GW21x-MAXI ACK Start Address Error


handshaking problem The GW21x-MAXI ACK Length Error
The GW21x-MAXI Response Command Error

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Configuration file Remote IP not found


Open configuration file failure

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

APPENDIX C:SAMPLE PROGRAM

C.1 TCP/IP sample program


/*--------------------------------------------------------------------
This is a TCP sample program to write flash memory for GW21 series.
*This program do following things:
1. Listen a TCP port = 23.
2. After connection ok, echo a message:
"Hello, this is GW21 series".
3. After receiving data, it will prompt a message and echo the received data.
For example: Receiving a string "ABCDE\r\n", then it will echo the message:
"Hello, I receive data =ABCDE"
4. If disconnected, it will listen again automatically.

*You can use the 'telnet' utility to test this sample program
--------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>

#include "..\..\inc\gw_api.h"

#define MY_PORT 23
#define DEF_MSS 512

#define _OK 0
#define _ERR 1
#define _WAITING 2
#define NULL_ARG (char far *)0

#define CONN_STATE_CLOSE 0 /* -> listen */


#define CONN_STATE_FAIL 1 /* -> idle */
#define CONN_STATE_IDLE 2 /* -> close */
#define CONN_STATE_LISTEN 6 /* -> connected / fail */
#define CONN_STATE_CONNECTED 7 /* -> close / fail */

int handle;
int conn_state ;
int connect_f = _ERR ;

unsigned char recvbuf[DEF_MSS];


unsigned char sendbuf[DEF_MSS];
int recvcnt=0 ;
int check_cnt=0 ;

void _loadds far send_post( CMDBLK far *cb, void far *arg) ;
void _loadds far listen_post( CMDBLK far *cb, void far *arg) ;

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

unsigned long now_sec=0 ;


unsigned long old_sec=0 ;

main()
{
int i, j, ret, free_cnt, loop_cnt ;
char *ptr ;
char tmpbuf[ 80+256] ;
unsigned long tmp_sec ;

conn_state = CONN_STATE_CLOSE ;
loop_cnt = 100 ;
handle = -1 ;

while(1)
{
if ( ++loop_cnt >= 10) /* to decrease the checking time count */
{
loop_cnt = 0 ;
tmp_sec = GetSysSec() ;
if ( tmp_sec != now_sec)
{
now_sec = tmp_sec ;
sprintf( tmpbuf, "tcp_getdata(), sec=%ld", now_sec) ;
SetAPSerialNo( tmpbuf); /*set AP version */
}
}

switch( conn_state)
{
case CONN_STATE_CLOSE:
conn_state = CONN_STATE_LISTEN ;
connect_f = _WAITING ;
ret = tcp_listen( (int far *)&handle, MY_PORT, listen_post, 0);
if ( ret != SUCCESS) /*fatal err */
{
SetAPSerialNo( "tcp_listen err"); /*set AP version */

/*wait a moment */
conn_state = CONN_STATE_IDLE ;
old_sec = now_sec ;
}
break ;

case CONN_STATE_IDLE:
if ( now_sec-old_sec >= 1)
{
conn_state = CONN_STATE_CLOSE ;

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

}
break ;

case CONN_STATE_FAIL:
if ( handle != -1)
{
tcp_disconnect( handle) ; /*diconnect */
handle = -1 ;
}
conn_state = CONN_STATE_IDLE ;
old_sec = now_sec ;
break ;

case CONN_STATE_LISTEN:
if ( connect_f==_OK) /*listen ok */
{
conn_state = CONN_STATE_CONNECTED ;
/*send a message after connecting */
ptr = "Hello, this is GW21 series\r\n" ;
ret = tcp_send( handle, ptr, strlen(ptr), send_post, NULL_ARG);
if ( ret != SUCCESS) /*if connection failed */
{
conn_state = CONN_STATE_FAIL ;
break ;
}
}
else
if ( connect_f==_ERR) /*listen err */
{
conn_state = CONN_STATE_FAIL ;
break ;
}
else
{
/*do nothing to wait listen ok */
}
break ;

case CONN_STATE_CONNECTED: /*after listen ok */


if ( connect_f==_ERR) /*if connection failed */
{
conn_state = CONN_STATE_FAIL ;
break ; /*try again */
}

/*connection ok, you can do some thing here */


free_cnt = DEF_MSS - recvcnt ;
ret = tcp_getdata( handle, (unsigned char far *)&recvbuf[ recvcnt], &free_cnt);

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

if ( ret != SUCCESS) /*get data failed */


{
conn_state = CONN_STATE_FAIL ;
break ; /*try again */
}
recvcnt += free_cnt ; /*returned free_cnt is the received data cnt */

for ( i=check_cnt; i<recvcnt; i++, check_cnt++)


if ( recvbuf[i]=='\r' || recvbuf[i]=='\n')
{
sprintf( tmpbuf, "Hello, I receive data =%*.*s\r\n", i, i, recvbuf) ;
ret = tcp_send( handle, tmpbuf, strlen( tmpbuf), send_post, NULL_ARG);
if ( ret != SUCCESS) /*get data failed */
{
conn_state = CONN_STATE_FAIL ;
break ; /*try again */
}

/* ProcessCmd( cb, recvbuf, i) ; */


if ( i<recvcnt-1)
{
memmove( recvbuf, &recvbuf[ i+1], recvcnt-i-1) ;
}
recvcnt = recvcnt - i -1 ;
check_cnt = 0 ;
}
break ;

default:
break ;
} /*---switch() */
} /*---while() */
}

void _loadds far send_post( CMDBLK far *cb, void far *arg)
{
/*----- check send success or not */
if( cb->retcode != SUCCESS ) {
connect_f = _ERR;
return;
}
}

void _loadds far listen_post( CMDBLK far *cb, void far *arg)
{
/*----- check listen success or not */
if( cb->retcode != SUCCESS ) {

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

connect_f = _ERR ;
return;
}
connect_f = _OK;
}

int ldumps2( unsigned char *dstbuf, unsigned char *srcbuf, int cnt, int auto_linefeed)
{
int i, k ;

if ( cnt < 0) cnt = strlen( (char*)srcbuf) ;

for ( i=k=0; i<cnt; i++)


{
sprintf( &dstbuf[k], "%02x ", srcbuf[i]) ;
k += 3 ;
if ( auto_linefeed)
if (( i&0xf) == 0xf)
{
dstbuf[ k++] = '\r' ;
dstbuf[ k++] = '\n' ;
}
}
dstbuf[ k++] = '\r' ;
dstbuf[ k++] = '\n' ;
dstbuf[ k++] = 0 ;
return( k) ;
}

C.2 Serial port sample program


/*-------------------------------------------------------------------------------------------------------
This is a serial port sample program for s.
*This program do following things:
1. Open COM1: 9600,n,8,1
2. After COM1 is opened, echo a message:
"Hello, this is GW21 series".
3. After receiving data, it will prompt a message and echo the received data.
For example: Receiving a 'A', then it will echo the message:
"Receive data =A"

*You can use the 'terminal' utility to test this sample program
-------------------------------------------------------------------------------------------------------*/
#include "dapapi.h"
#include "comapi.h"

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 9
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

//=============================================================
void main()
{
int i, com_port, cnt ;
int ret;
char buf[ 256], *ptr ;

SetAPSerialNo( "COM sample program"); //set AP version

com_port = 0 ; // 0:COM1
ret = Com_Config( com_port, 9600L, 0, 8, 1) ; //open com port: 9600,n,8,1
if (ret != 0)
{
//com port open error
}

COM_DTR_Active( com_port, 1); //set com port to be communication ready (for h/w
handshaking)
COM_RTS_Active( com_port, 1); //set com port to be send ready (for h/w handshaking)

ptr = "Hello, this is GW21 series\r\n" ;


Com_PutBuf( com_port, ptr, strlen(ptr)) ; //send data to com port

while(1)
{
cnt = 256 ;
ret = Com_GetBuf( com_port, buf, cnt) ; //receive data from com port
if (ret>0) {
cnt = ret ;
ptr = "\r\nReceive data=" ;
Com_PutBuf( com_port, ptr, strlen(ptr)) ; //send data to com port
Com_PutBuf( com_port, buf, cnt) ; //send data to com port
}
};
}

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Appendix D. Configuration Utility


The configuration utility monitor.exe came with the product CD or diskette is the main utility program
that allows user to perform all the functions they would need for the system.

D.1 Run the utility


To start the program, please double-click the program under Windows 95/98/NT/2000 environment and
following window appears.

Figure D1. Main window of monitor.exe utility program

Display Description

The following table lists the functional descriptions for all the fields.

Field Name Field Descriptions


Broadcast IP Except for the default IP 255.255.255.255, other items (IPs) are read from
the file “seg.cfg”. This field specifies a detecting IP range. It may be a
designated IP or a broadcast IP.
Wishes Specifies minimum number of the devices you wish to get reply from after
sending an Invite request. If there is not as many as devices responded to
your invitation, the system repeatedly sends invitation until your request is
fulfilled.
Reply Indicates the actual number of controllers or devices this utility program
detected.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

Retry Specify the number of times that an Invite request be re-sent.


Locate Locate the specified controller.
Reset Reset the selected controller or device.
Config Configure the selected controller or device.
Exit Exit this utility.
IP Address Indicate the IP address of the controller or device that replied to your
request.
Leading tag “!”: Stands for IP address collision. Possibly caused by
duplicated IPs on the network.
Leading tag “?”: Stands for Mac address collision. Possibly caused by
duplicated Mac address on the network.
MAC Address Indicates the detected Ethernet MAC address of responding controller or
device.
Gateway Indicates IP address of the TCP/IP gateway.
Subnet Mask Indicates the TCP/IP network mask.
OS Indicates the OS version of the responding controller or device.
AP Version Indicates the AP version number of the responding controller or device.
Model Indicates the model number of the responding controller or device. This
field is only available for monitor.exe version 2.0 and above.

D.2 Detecting Operational Controllers and Devices


You may do the following steps to detect controllers or devices that currently active on the network.

1. Start monitor.exe utility program.

2. Select an item from the Broadcast IP list.

3. Specify a number in the Wishes box.

4. Click on the Invite button. This will display all the controllers and devices information you
have requested.

D.3 Configuring Controllers and Devices


You may use monitor.exe configuration utility to configure the TCP/IP parameters of devices on the
network. To do so, please follow the steps below.

1. Repeat the steps in the section of D.2 Detecting Operational Controllers and Devices to bring
up controllers information.

2. Select the controller you want to configure from the IP Address column. Click on the Config

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249
GW21x series SDK Guide

button. A configuration dialog box will popup.

3. Fill in information in the box along with the Password for the device and click on Config it to
save the change or Cancel to abort. The default password is null. You can press the reset key of
product to reset password to the default value.

4. The target controller will return an ACK message indicating the modification is successfully
modified.

ATOP Technologies, Inc., Suite 305, No. 47 Park Avenue II, Science-Based Industrial Park, Hsinchu, Taiwan, R.O. C 1
Tel:+886 3 578 1962 Fax:+886 3 578 4249

You might also like