User`s manual

Table Of Contents
UC-7400-CE User’s Manual Programming Examples
5-5
Windows® API function CreateFile(…) opens a named file corresponding to a serial port. For any
serial port from “COM10” or after, the file name must be prefixed by “$device\\”. For “COM1” to
“COM9”, the name can be either prefixed by “$device\\” or not. API function GetCommState(..) is
the standard function to get the current parameters of a serial port, including its baudrate, data bits
(bytesize),parity and stop bits. In contrast to GetCommState(), API function SetCommState(...)
controls the serial communication by setting these parameters. Use other parameters including
flow control, DTR signal, and RTS signal to fine tune the serial port. Be sure to set the flow
control correctly or else you will lose data.
Depending on the device a serial port connects to, the UC-7400-CE computer supports serial
communication in four operation modes: RS-232, RS-422, 2-wire RS-485, and 4-wire RS-485;
RS-232 is the default. To change the operation mode, your program should include the following
macro definitions; also insert the following DeviceIoControl function on the open handle before
the program performs read/write operations.
/* --------- extracted from <moxa/devices> -------------------
#define MOXA_SET_OP_MODE (0x400 + 66)
#define MOXA_GET_OP_MODE (0x400+ 67)
#define RS232_MODE 0
#define RS485_2WIRE_MODE 1
#define RS422_MODE 2
#define RS485_4WIRE_MODE 3
*/
/* for example, set the mode to 4-wire RS485 */
BYTE mode = RS485_4WIRE_MODE;
DeviceIoControl(hCom,MOXA_SET_OP_MODE, &mode, sizeof(mode), NULL,0,NULL,NULL);
/* for example, get the current mode */
BYTE mode, size;
DeviceIoControl(hCom,MOXA_GET_OP_MODE, NULL, 0, (LPVOID)&mode,
sizeof(mode),&size,NULL);
Example #2Buzzer
The UC-7400-CE computer supports buzzer hardware for your applications to generate alarms on
critical errors. You can set the frequency and the duration of the buzzer at the application level by
using the APIs as follows: (within “mxdev.lib”). For example, the codes shown trigger the buzzer
for 2000 milliseconds at frequency 500Hzs.
/* execute a beeper at a specified frequency for lasting a duration in miniseconds */
int mxbeep(unsigned int nHz, unsigned int nMiniSec);
#include <moxa/devices.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
mxbeep(500,2000);
return 0;
}