#define MAX_SERIAL_PORT 3
#define BUFFER_SIZE 512
int main(
int argc,
char *argv[])
{
int retval = 0;
unsigned char slot = 0;
unsigned char port =
PORT1;
while((retval = getopt(argc, argv, "hs:p:M:B:P:D:S:F:")) != -1)
{
switch(retval)
{
case 's':
slot = atoi(optarg);
{
printf("Error slot = %d\r\n", slot);
exit(1);
}
break;
case 'p':
port = atoi(optarg);
{
printf("Error port = %d\r\n", port);
exit(1);
}
break;
case 'M':
mode = atoi(optarg);
{
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);
{
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);
{
printf("Error stop bit = %d\r\n", stopBit);
exit(1);
}
break;
case 'F':
flowControl = atoi(optarg);
{
printf("Error flow control = %d\r\n", flowControl);
exit(1);
}
break;
case '?':
case 'h':
default:
printf("Serial Echo with Blocking Read.\n\n");
printf("Usage: ./serial_echo_blocking [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 (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 (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 (0: NO_FLOW_CONTROL, 1: HW_FLOW_CONTROL, 2: SW_FLOW_CONTROLN)\n", "");
printf("\n");
return 0;
}
}
{
printf("MX_RTU_SerialOpen(%d, %d), return code = %d\r\n", slot, port, rc);
exit(1);
}
{
printf("MX_RTU_SerialSetMode(%d, %d, %d), return code = %d\r\n", slot, port, mode, rc);
exit(1);
}
{
printf("MX_RTU_SerialSetSpeed(%d, %d, %d), return code = %d\r\n", slot, port, baudRate, rc);
exit(1);
}
{
printf("MX_RTU_SerialSetParam(%d, %d, %d, %d, %d), return code = %d\r\n", slot, port, parity, dataBits, stopBit, rc);
exit(1);
}
{
printf("MX_RTU_SerialFlowControl(%d, %d, %d), return code = %d\r\n", slot, port, flowControl, rc);
exit(1);
}
{
printf("MX_RTU_SerialGetMode(%d, %d, &modeCheck), modeCheck = %d, return code = %d\r\n", slot, port, modeCheck, rc);
exit(1);
}
if(mode != modeCheck)
{
printf("Mode mismatch, mode = %d, modeCheck = %d\r\n", mode, modeCheck);
exit(1);
}
{
printf("MX_RTU_FindFD(%d, %d, &fd), fd = %d, return code = %d\r\n", slot, port, fd, rc);
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)
{
readBytes = 0;
memset(buf, 0, sizeof(buf));
{
if(readBytes == -1)
{
printf("MX_RTU_SerialBlockRead(%d, %d, %d, buf, %d), errno = %d\r\n", slot, port, sizeof(buf), readBytes, errno);
exit(1);
}
if(buf[0] == 0x1B)
break;
printf("Receive %d bytes from slot %d and port %d.\r\n", readBytes, slot, port);
}
{
printf("MX_RTU_SerialBlockRead(%d, %d, %d, buf, %d), return code = %d\r\n", slot, port, sizeof(buf), readBytes, rc);
exit(1);
}
nBytes = 0;
{
printf(
"MX_RTU_SerialDataInOutputQueue(%d, %d, %d), Output Queue space = %d\r\n", slot, port, nBytes, (
SERIAL_MAX_OQUEUE_LENGTH - nBytes));
exit(1);
}
{
printf("MX_RTU_SerialDataInOutputQueue(%d, %d, &nBytes), return code = %d\r\n", slot, port, rc);
exit(1);
}
writeBytes = 0;
{
printf("MX_RTU_SerialWrite(%d, %d, %d, buf, &writeBytes), return code = %d\r\n", slot, port, readBytes, rc);
exit(1);
}
{
printf("MX_RTU_SerialWrite(%d, %d, %d, buf, %d), errno = %d\r\n", slot, port, readBytes, writeBytes, errno);
exit(1);
}
printf("Send %d bytes to slot %d and port %d.\r\n", writeBytes, slot, port);
}
return 0;
}