ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions
serial_echo.c File Reference

Serial Echo More...

#include <libmoxa_rtu.h>

Macros

#define MAX_SERIAL_PORT   3
 
#define BUFFER_SIZE   512
 

Functions

int main (int argc, char *argv[])
 

Detailed Description

Serial Echo

Date
04-10-2013
Author
Eddy Kao
Version
V1.0
serial_echo.jpg
Serial Echo
Introduction:
This is an echo example. The controller will echo each byte that is sent by the PC.
Example:
1. Using default: ./serial_echo
2. Setting port and baud rate only: ./serial_echo -p1 -B9600
Default:
Slot = 0
Port = 0
Mode = RS232
Baud Rate = 115200
Parity = NONE
Data Bits = 8
Stop Bit = 1
Flow Control = Hardware
Help:
root@Moxa:/tmp#./serial_echo -h
Serial Echo.

Usage: ./serial_echo [OPTIONS]

Options:
    -s      slot [0-9]. Default slot = 0
            (slot 0: Built-in COM Ports, slot 1 ~ 9: Expansion COM Ports)
    -p      port [0-3]. Default port = 0
    -M      Mode [0-3]. Default Mode = 0
            (0: RS232, 1: RS485_2WIRE, 2: RS422, 3: RS485_4WIRE)
    -B      Baud Rate [300-921600]. Default Baud Rate = 115200
    -P      Parity [0-2]. Default Parity = 0
            (0: NONE, 1: ODD, 2: EVEN)
    -D      Data Bits [5-8]. Default Data Bits = 8
    -S      Stop Bit [1-2]. Default Stop Bit = 1
    -F      Flow Control [0-2]. Default Flow Control = 1
            (0: NO_FLOW_CONTROL, 1: HW_FLOW_CONTROL, 2: SW_FLOW_CONTROLN)

Library:
Serial APIs

Macro Definition Documentation

#define MAX_SERIAL_PORT   3
#define BUFFER_SIZE   512

Function Documentation

int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* Serial Echo
*
* Date Author Comment
* 04-10-2013 Eddy Kao Created.
******************************************************************************/
#include <libmoxa_rtu.h>
#define MAX_SERIAL_PORT 3
#define BUFFER_SIZE 512
int main(int argc, char *argv[])
{
int retval = 0;
UINT32 rc = 0;
UINT32 nBytes = 0;
UINT32 readBytes = 0;
UINT32 writeBytes = 0;
INT32 fd = 0;
UINT8 modeCheck = 0;
char buf[BUFFER_SIZE];
unsigned char slot = 0; //slot 0: Built-in COM Ports, slot 1 ~ 9: Expansion COM Ports
unsigned char port = PORT1;
unsigned char mode = RS232_MODE;
unsigned int baudRate = BAUD_RATE_115200;
unsigned char parity = SERIAL_PARITY_NONE;
unsigned char dataBits = SERIAL_DATA_BITS_8;
unsigned char stopBit = SERIAL_STOP_BIT_1;
unsigned char flowControl = HW_FLOW_CONTROL;
while((retval = getopt(argc, argv, "hs:p:M:B:P:D:S:F:")) != -1)
{
switch(retval)
{
case 's':
slot = atoi(optarg);
if(slot > MAX_SLOT)
{
printf("Error slot = %d\r\n", slot);
exit(1);
}
break;
case 'p':
port = atoi(optarg);
if(port > MAX_SERIAL_PORT)
{
printf("Error port = %d\r\n", port);
exit(1);
}
break;
case 'M':
mode = atoi(optarg);
if(mode > RS485_4WIRE_MODE)
{
printf("Error mode = %d\r\n", mode);
exit(1);
}
break;
case 'B':
baudRate = atoi(optarg);
if(baudRate < BAUD_RATE_300 || baudRate > BAUD_RATE_921600)
{
printf("Error baud rate = %d\r\n", baudRate);
exit(1);
}
break;
case 'P':
parity = atoi(optarg);
if(parity > SERIAL_PARITY_EVEN)
{
printf("Error parity = %d\r\n", parity);
exit(1);
}
break;
case 'D':
dataBits = atoi(optarg);
if(dataBits < SERIAL_DATA_BITS_5 || dataBits > SERIAL_DATA_BITS_8)
{
printf("Error data bits = %d\r\n", dataBits);
exit(1);
}
break;
case 'S':
stopBit = atoi(optarg);
if(stopBit > SERIAL_STOP_BIT_2)
{
printf("Error stop bit = %d\r\n", stopBit);
exit(1);
}
break;
case 'F':
flowControl = atoi(optarg);
if(flowControl > SW_FLOW_CONTROL)
{
printf("Error flow control = %d\r\n", flowControl);
exit(1);
}
break;
case '?':
case 'h':
default:
printf("Serial Echo.\n\n");
printf("Usage: ./serial_echo [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s slot [%d-%d]. Default slot = %d\n", "-s", 0, MAX_SLOT, slot);
printf("\t%-8s (slot 0: Built-in COM Ports, slot 1 ~ 9: Expansion COM Ports)\n", "");
printf("\t%-8s port [%d-%d]. Default port = %d\n", "-p", 0, MAX_SERIAL_PORT, port);
printf("\t%-8s Mode [%d-%d]. Default Mode = %d\n", "-M", RS232_MODE, RS485_4WIRE_MODE, mode);
printf("\t%-8s (0: RS232, 1: RS485_2WIRE, 2: RS422, 3: RS485_4WIRE)\n", "");
printf("\t%-8s Baud Rate [%d-%d]. Default Baud Rate = %d\n", "-B", BAUD_RATE_300, BAUD_RATE_921600, baudRate);
printf("\t%-8s Parity [%d-%d]. Default Parity = %d\n", "-P", SERIAL_PARITY_NONE, SERIAL_PARITY_EVEN, parity);
printf("\t%-8s (0: NONE, 1: ODD, 2: EVEN)\n", "");
printf("\t%-8s Data Bits [%d-%d]. Default Data Bits = %d\n", "-D", SERIAL_DATA_BITS_5, SERIAL_DATA_BITS_8, dataBits);
printf("\t%-8s Stop Bit [%d-%d]. Default Stop Bit = %d\n", "-S", SERIAL_STOP_BIT_1, SERIAL_STOP_BIT_2, stopBit);
printf("\t%-8s Flow Control [%d-%d]. Default Flow Control = %d\n", "-F", NO_FLOW_CONTROL, SW_FLOW_CONTROL, flowControl);
printf("\t%-8s (0: NO_FLOW_CONTROL, 1: HW_FLOW_CONTROL, 2: SW_FLOW_CONTROLN)\n", "");
printf("\n");
return;
}
}
rc = MX_RTU_SerialOpen(slot, port);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialOpen(%d, %d), return code = %d\r\n", slot, port, rc);
exit(1);
}
rc = MX_RTU_SerialSetMode(slot, port, mode);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialSetMode(%d, %d, %d), return code = %d\r\n", slot, port, mode, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
rc = MX_RTU_SerialSetSpeed(slot, port, baudRate);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialSetSpeed(%d, %d, %d), return code = %d\r\n", slot, port, baudRate, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
rc = MX_RTU_SerialSetParam(slot, port, parity, dataBits, stopBit);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialSetParam(%d, %d, %d, %d, %d), return code = %d\r\n", slot, port, parity, dataBits, stopBit, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
rc = MX_RTU_SerialFlowControl(slot, port, flowControl);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialFlowControl(%d, %d, %d), return code = %d\r\n", slot, port, flowControl, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
rc = MX_RTU_SerialGetMode(slot, port, &modeCheck);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialGetMode(%d, %d, &modeCheck), modeCheck = %d, return code = %d\r\n", slot, port, modeCheck, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
if(mode != modeCheck)
{
printf("Mode mismatch, mode = %d, modeCheck = %d\r\n", mode, modeCheck);
MX_RTU_SerialClose(slot, port);
exit(1);
}
rc = MX_RTU_FindFD(slot, port, &fd);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_FindFD(%d, %d, &fd), fd = %d, return code = %d\r\n", slot, port, fd, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
printf("Slot = %d, Port = %d, Mode = %d, Baud Rate = %d, Parity = %d, Data Bits = %d, Stop Bit = %d, Flow Control = %d, fd = %d\r\n", slot, port, mode, baudRate, parity, dataBits, stopBit, flowControl, fd);
while(1)
{
//Receive
readBytes = 0;
memset(buf, 0, sizeof(buf));
rc = MX_RTU_SerialNonBlockRead(slot, port, sizeof(buf), buf, &readBytes);
if(rc == SERIAL_ERR_OK && readBytes != 0)
{
if(readBytes == -1 && errno == EAGAIN) continue;
if(readBytes == -1)
{
printf("MX_RTU_SerialNonBlockRead(%d, %d, %d, buf, %d), errno = %d\r\n", slot, port, sizeof(buf), readBytes, errno);
MX_RTU_SerialClose(slot, port);
exit(1);
}
if(buf[0] == 0x1B) // 'ESC'
break;
printf("Receive %d bytes from slot %d and port %d.\r\n", readBytes, slot, port);
}
else if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialNonBlockRead(%d, %d, %d, buf, %d), return code = %d\r\n", slot, port, sizeof(buf), readBytes, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
//Send
nBytes = 0;
rc = MX_RTU_SerialDataInOutputQueue(slot, port, &nBytes);
if(rc == SERIAL_ERR_OK && (SERIAL_MAX_OQUEUE_LENGTH - nBytes) < readBytes)
{
printf("MX_RTU_SerialDataInOutputQueue(%d, %d, %d), Output Queue space = %d\r\n", slot, port, nBytes, (SERIAL_MAX_OQUEUE_LENGTH - nBytes));
MX_RTU_SerialClose(slot, port);
exit(1);
}
else if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialDataInOutputQueue(%d, %d, &nBytes), return code = %d\r\n", slot, port, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
writeBytes = 0;
rc = MX_RTU_SerialWrite(slot, port, readBytes, buf, &writeBytes);
if(rc != SERIAL_ERR_OK)
{
printf("MX_RTU_SerialWrite(%d, %d, %d, buf, &writeBytes), return code = %d\r\n", slot, port, readBytes, rc);
MX_RTU_SerialClose(slot, port);
exit(1);
}
else if(rc == SERIAL_ERR_OK && writeBytes != readBytes)
{
printf("MX_RTU_SerialWrite(%d, %d, %d, buf, %d), errno = %d\r\n", slot, port, readBytes, writeBytes, errno);
MX_RTU_SerialClose(slot, port);
exit(1);
}
printf("Send %d bytes to slot %d and port %d.\r\n", writeBytes, slot, port);
}
return 0;
}