|
|
// RS232.c - Function to communicate on Serial Port
|
|
|
|
|
|
#include <stdio.h>
|
|
|
#include <unistd.h>
|
|
|
#include <fcntl.h>
|
|
|
#include <string.h>
|
|
|
#include <errno.h>
|
|
|
#include "config.h"
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
#include <termios.h>
|
|
|
#endif
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
#include <windows.h>
|
|
|
#endif
|
|
|
#include "RS232.h"
|
|
|
|
|
|
/*
|
|
|
rs232port_t rs232open(char* psPortName)
|
|
|
{
|
|
|
rs232port_t fd;
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY);
|
|
|
#endif
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
fd = CreateFile(psPortName,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
|
|
|
#endif
|
|
|
|
|
|
return fd;
|
|
|
} */
|
|
|
|
|
|
/*
|
|
|
int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, int NbStop)
|
|
|
{
|
|
|
if (fd == -1)
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
struct termios terminos;
|
|
|
tcgetattr(fd, &terminos);
|
|
|
|
|
|
cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
|
|
|
cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
|
|
|
|
|
|
terminos.c_cflag |= (CLOCAL | CREAD);
|
|
|
rs232cfparity(fd, &terminos, Parity);
|
|
|
rs232cfnbstop(fd, &terminos, NbStop);
|
|
|
rs232cfcsize(fd, &terminos, ChSize);
|
|
|
|
|
|
|
|
|
tcsetattr(fd, TCSANOW, &terminos);
|
|
|
//tcgetattr(fd, &terminos);
|
|
|
#endif
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
DCB dcbSerialParams = {0};
|
|
|
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
|
|
|
GetCommState(fd, &dcbSerialParams);
|
|
|
dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate);
|
|
|
dcbSerialParams.ByteSize=ChSize;
|
|
|
dcbSerialParams.StopBits=NbStop;
|
|
|
dcbSerialParams.Parity=Parity;
|
|
|
SetCommState(fd, &dcbSerialParams);
|
|
|
COMMTIMEOUTS timeouts={0};
|
|
|
timeouts.ReadIntervalTimeout=50;
|
|
|
timeouts.ReadTotalTimeoutConstant=50;
|
|
|
timeouts.ReadTotalTimeoutMultiplier=10;
|
|
|
timeouts.WriteTotalTimeoutConstant=50;
|
|
|
timeouts.WriteTotalTimeoutMultiplier=10;
|
|
|
SetCommTimeouts(fd, &timeouts);
|
|
|
#endif
|
|
|
return 0;
|
|
|
}
|
|
|
}*/
|
|
|
|
|
|
/*
|
|
|
int rs232write(rs232port_t fd,char *psWrite)
|
|
|
{
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
int Len_psWrite = (int)strlen(psWrite);
|
|
|
|
|
|
if (fd == -1)
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return write(fd, psWrite, Len_psWrite);
|
|
|
}
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
|
|
|
int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize)
|
|
|
{
|
|
|
|
|
|
if (fd == -1)
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
return read(fd, psReadHex, ReadBufferSize);
|
|
|
#endif
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
DWORD dwBytesRead = 0;
|
|
|
ReadFile(fd, psReadHex, ReadBufferSize, &dwBytesRead, NULL);
|
|
|
return dwBytesRead;
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
}
|
|
|
*/
|
|
|
/*
|
|
|
int rs232close(rs232port_t fd)
|
|
|
{
|
|
|
if (fd == -1)
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
close(fd);
|
|
|
#endif
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
CloseHandle(fd);
|
|
|
#endif
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
*/
|