LQ Newbie
Registered: Feb 2010
Posts: 11
Original Poster
Rep:
|
Ok, here is the functions I guess is relevant:
int16_t RS232Init (const char* device, struct comDefine config)
{
int16_t fd;
struct termios newtio;
/* open the device to be non-blocking (read will return immediatly) */
fd = open(device, O_RDWR | O_NOCTTY);
if (fd < 0)
{
perror(device);
goto Error;
}
/* install the signal handler before making the device asynchronous */
// Several code which are not used
/* Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
//fcntl(fd, F_SETFL, FASYNC);
/* Define baud rate */
switch (config.BaudRate)
{
case 19200:
BAUD = B19200;
break;
case 9600:
default:
BAUD = B9600;
break;
case 4800:
BAUD = B4800;
break;
case 2400:
BAUD = B2400;
break;
case 1200:
BAUD = B1200;
break;
} //end of switch BaudRate
switch (config.DataBits)
{
case 8:
default:
DATABITS = CS8;
break;
} //end of switch DataBits
switch (config.StopBits)
{
case 1:
default:
STOPBITS = 0;
break;
case 2:
STOPBITS = CSTOPB;
break;
} //end of switch StopBits
switch (config.Parity)
{
case 0:
default: //none
PARITYON = 0;
PARITY = 0;
break;
} //end of switch Parity
newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
newtio.c_iflag = IGNPAR; // | ICRNL;
/* Raw output. */
newtio.c_oflag = 0;
/*
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
newtio.c_lflag = 0; //ICANON;
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
return fd;
Error:
return -1;
}
int16_t CreateTCPSystemControllerSocket(uint16_t port)
{
int16_t createdSocket; // socket to create
int16_t bindResult; // Result from binding
int16_t listenResult; // Result from listening
uint8_t on, ret;
struct sockaddr_in systemControllerAddress; // Local address
createdSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket for incoming connections
if (createdSocket < 0) {
perror("socket() failed");
goto Error;
}
/* Enable address reuse */
on = 1;
ret = setsockopt( createdSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
/* Construct local address structure */
memset(&systemControllerAddress, 0, sizeof(systemControllerAddress)); // Zero out structure
systemControllerAddress.sin_family = AF_INET; // Internet address family
systemControllerAddress.sin_addr.s_addr = htonl(INADDR_ANY); // Any incoming interface
systemControllerAddress.sin_port = htons(port); // Local port
/* Bind to the local address */
bindResult = bind(createdSocket, (struct sockaddr *) &systemControllerAddress, sizeof(systemControllerAddress));
if (bindResult < 0) {
perror("bind() failed");
if (errno == EADDRINUSE) {
DEBUG_PRINT("Address in Use\n");
}
goto Error;
}
/* Mark the socket so it will listen for incoming connections */
listenResult = listen(createdSocket, MAXPENDING);
if (listenResult < 0) {
perror("listen() failed");
goto Error;
}
return createdSocket;
Error:
return -1;
}
int16_t AcceptTCPConnection(int16_t systemControllerSocket)
{
int32_t clientSock; /* Socket descriptor for client */
struct sockaddr_in clientAddress; /* Client address */
uint32_t clientLength; /* Length of client address data structure */
/* Set the size of the in-out parameter */
clientLength = sizeof(clientAddress);
/* Wait for a client to connect */
clientSock = accept(systemControllerSocket, (struct sockaddr *) &clientAddress, &clientLength);
if (clientSock < 0) {
perror("accept() failed");
goto Error;
}
/* clientSock is connected to a client! */
DEBUG_PRINT("Handling client %s\n", inet_ntoa(clientAddress.sin_addr));
return clientSock;
Error:
return -1;
}
int16_t InitRS232(const EquipmentList_t Eq)
{
uint8_t retValue = FALSE;
struct comDefine CRComDefaultValue;
int16_t rs232Fd;
EqStatus_t tmpCR;
retValue = GetEqStatus(Eq, &tmpCR);
// Initialize
// Default settings of RS232 communication port
// 9600 baud, 8 data bits, 1 stop bit, no parity
CRComDefaultValue.BaudRate = 9600;
CRComDefaultValue.DataBits = 8;
CRComDefaultValue.StopBits = 1;
CRComDefaultValue.Parity = 0;
rs232Fd = RS232Init(tmpCR.Device, CRComDefaultValue);
if(rs232Fd < 0)
{
DEBUG_PRINT("Error wrong file descriptor, %d\n", rs232Fd);
goto Error;
}
retValue = PutEqClientFD(tmpCR.Equipment, rs232Fd);
return retValue;
Error:
return -1;
}
int16_t InitTCPIP(const EquipmentList_t Eq)
{
uint8_t retValue = FALSE;
int16_t socketFd;
EqStatus_t tmpCR;
retValue = GetEqStatus(Eq, &tmpCR);
// Initialize
// Default settings of BO communication port
// Port: 2101
socketFd = CreateTCPSystemControllerSocket(2101);
if(socketFd < 0)
{
DEBUG_PRINT("Error wrong file descriptor, %d\n", socketFd);
goto Error;
}
retValue = PutEqServerFD(tmpCR.Equipment, socketFd);
return retValue;
Error:
return -1;
}
uint8_t CheckMessage(const EquipmentList_t eq)
{
int16_t res;
int16_t nfds;
fd_set readfs; /* file descriptor set */
struct timeval Timeout;
EqStatus_t eqStatus;
BYTE buf[EQUIPMENT_BUFFER_SIZE] = "";
int16_t rest;
uint8_t dataLength;
int16_t clientSocket;
GetEqStatus(eq, &eqStatus);
FD_ZERO(&readfs);
FD_SET(eqStatus.Client_File_Descriptor, &readfs);
FD_SET(eqStatus.Server_File_Descriptor, &readfs);
if(eqStatus.Client_File_Descriptor > eqStatus.Server_File_Descriptor) {
nfds = eqStatus.Client_File_Descriptor + 1; // Highest fd in the set, plus 1.
}
else {
nfds = eqStatus.Server_File_Descriptor + 1; // Highest fd in the set, plus 1.
}
/* set timeout value within input loop */
Timeout.tv_usec = 0; /* milliseconds */
Timeout.tv_sec = 0; /* seconds */
res = select(nfds, &readfs, NULL, NULL, &Timeout);
if(res < 0)
{
DEBUG_PRINT("Error in select command.\n"); // select returned -1, thus an error occurred
goto Error;
}
else if (res > 0)
{
if (FD_ISSET(eqStatus.Client_File_Descriptor, &readfs)) // input from source 1 available
{
rest = read(eqStatus.Client_File_Descriptor, buf, EQUIPMENT_BUFFER_SIZE -1);
buf[rest] = 0;
dataLength = rest;
AppendBuffer(eq, buf, dataLength);
//DEBUG_SL_HEX(buf, dataLength, "A:");
if (eq == BackOffice) {
// Still in place for CashRegister handling. No switch yet.
if(dataLength == 0) {
close(eqStatus.Client_File_Descriptor);
PutEqClientFD(eqStatus.Equipment, 0);
}
else {
//DEBUG_CONTROL("CheckMessage(): DataRcvd set\n");
PutEqDataRcvd(eq, TRUE);
}
}
else if(eq == SlavePort){// || (eq == SlavePort2) || (eq == SlavePort3) || (eq == SlavePort4)) {
if (SL_IsMessageComplete(eq)) {
PutEqDataRcvd(eq, TRUE);
}
}
else if(eq == CashRegister) {
// Still in place for CashRegister handling. No switch yet.
if(CRBiStd_IsCompleteMessage(eq)) {
//DEBUG_BO_HEX(buf, dataLength, "Message received from CR: ")
PutEqDataRcvd(eq, TRUE);
}
}
}
else if(FD_ISSET(eqStatus.Server_File_Descriptor, &readfs)) /* input from source 1 available */
{
if (eq == BackOffice) {
// Still in place for CashRegister handling. No switch yet.
clientSocket = AcceptTCPConnection(eqStatus.Server_File_Descriptor);
PutEqClientFD(eqStatus.Equipment, clientSocket);
}
}
}
return OK;
Error:
return -1;
}
|