|
|
#include "rs232config.h"
|
|
|
#include <stdio.h>
|
|
|
#include <unistd.h>
|
|
|
#include <fcntl.h>
|
|
|
#include <string.h>
|
|
|
#include <errno.h>
|
|
|
#include <malloc.h>
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
#include <windows.h>
|
|
|
#endif
|
|
|
#include "RS232.h"
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
|
|
|
|
|
int privatedecodeparity(rs232parity Parity )
|
|
|
{
|
|
|
switch(Parity)
|
|
|
{
|
|
|
case rs232parityNo:
|
|
|
return NOPARITY;
|
|
|
break;
|
|
|
case rs232parityOdd:
|
|
|
return ODDPARITY;
|
|
|
break;
|
|
|
case rs232parityEven:
|
|
|
return EVENPARITY;
|
|
|
break;
|
|
|
default:
|
|
|
return NOPARITY;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int privatedecodestop(rs232stop NbStop)
|
|
|
{
|
|
|
switch(NbStop)
|
|
|
{
|
|
|
case rs232OneStop:
|
|
|
return ONESTOPBIT;
|
|
|
break;
|
|
|
case rs232One5Stop:
|
|
|
return ONE5STOPBITS;
|
|
|
break;
|
|
|
case rs232TwoStop:
|
|
|
return TWOSTOPBITS;
|
|
|
break;
|
|
|
default:
|
|
|
return ONESTOPBIT;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rs232speed_t rs232cfspeed(unsigned int BaudeRate)
|
|
|
{
|
|
|
|
|
|
if(BaudeRate<123)
|
|
|
return (rs232speed_t)CBR_110;
|
|
|
|
|
|
if(BaudeRate<450)
|
|
|
return (rs232speed_t)CBR_300;
|
|
|
|
|
|
if(BaudeRate<900)
|
|
|
return (rs232speed_t)CBR_600;
|
|
|
|
|
|
if(BaudeRate<1500)
|
|
|
return (rs232speed_t)CBR_1200;
|
|
|
|
|
|
if(BaudeRate<3600)
|
|
|
return (rs232speed_t)CBR_2400;
|
|
|
|
|
|
if(BaudeRate<7200)
|
|
|
return (rs232speed_t)CBR_4800;
|
|
|
|
|
|
if(BaudeRate<14000)
|
|
|
return (rs232speed_t)CBR_9600;
|
|
|
|
|
|
if(BaudeRate<16800)
|
|
|
return (rs232speed_t)CBR_14400;
|
|
|
|
|
|
if(BaudeRate<28800)
|
|
|
return (rs232speed_t)CBR_19200;
|
|
|
|
|
|
if(BaudeRate<48000)
|
|
|
return (rs232speed_t)CBR_38400;
|
|
|
|
|
|
if(BaudeRate<86400)
|
|
|
return (rs232speed_t)CBR_57600;
|
|
|
|
|
|
if(BaudeRate<172800)
|
|
|
return (rs232speed_t)CBR_115200;
|
|
|
else
|
|
|
return (rs232speed_t)CBR_256000;
|
|
|
}
|
|
|
|
|
|
|
|
|
rs232port_t rs232open(char* psPortName)
|
|
|
{
|
|
|
rs232port_t fd;
|
|
|
fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
|
|
|
#ifdef debug
|
|
|
if(fd==(rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
printf("can't open Port\n");
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
#endif
|
|
|
return fd;
|
|
|
}
|
|
|
|
|
|
int rs232close(rs232port_t fd)
|
|
|
{
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
CloseHandle((HANDLE)fd);
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
rs232portslist_t* rs232getportlist() //here is the very dirty way!
|
|
|
{
|
|
|
int i=0;
|
|
|
char devName[]="COM111";
|
|
|
rs232port_t testport;
|
|
|
rs232portslist_t* firstitem=NULL;
|
|
|
rs232portslist_t* previtem=NULL;
|
|
|
for(i=0;i<256;i++)
|
|
|
{
|
|
|
devName[3] = '\0';
|
|
|
devName[4] = '\0';
|
|
|
devName[5] = '\0';
|
|
|
devName[6] = '\0';
|
|
|
sprintf(devName+3,"%d",i);
|
|
|
testport= rs232open(devName);
|
|
|
if(testport != (rs232port_t)badPortValue)
|
|
|
{
|
|
|
rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t));
|
|
|
char* name = (char*)malloc(7);
|
|
|
strcpy(name,devName);
|
|
|
item->name = name;
|
|
|
item->next = NULL;
|
|
|
if(NULL!=previtem)previtem->next = item;
|
|
|
previtem = item;
|
|
|
if(NULL==firstitem)firstitem = item;
|
|
|
}
|
|
|
}
|
|
|
return firstitem;
|
|
|
}
|
|
|
|
|
|
void rs232deleteportlist(rs232portslist_t* list)
|
|
|
{
|
|
|
if(list!=NULL)
|
|
|
{
|
|
|
if(list->next != NULL)
|
|
|
rs232deleteportlist(list->next);
|
|
|
free(list);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop)
|
|
|
{
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DCB dcbSerialParams = {0};
|
|
|
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
|
|
|
GetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate);
|
|
|
dcbSerialParams.ByteSize=ChSize;
|
|
|
dcbSerialParams.StopBits=privatedecodestop(NbStop);
|
|
|
dcbSerialParams.Parity=privatedecodeparity(Parity);
|
|
|
SetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
COMMTIMEOUTS timeouts={0};
|
|
|
timeouts.ReadIntervalTimeout=100;
|
|
|
timeouts.ReadTotalTimeoutConstant=100;
|
|
|
timeouts.ReadTotalTimeoutMultiplier=1;
|
|
|
timeouts.WriteTotalTimeoutConstant=100;
|
|
|
timeouts.WriteTotalTimeoutMultiplier=10;
|
|
|
SetCommTimeouts((HANDLE)fd, &timeouts);
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int rs232setbaudrate(rs232port_t fd, int baudrate)
|
|
|
{
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DCB dcbSerialParams = {0};
|
|
|
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
|
|
|
GetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
dcbSerialParams.BaudRate=rs232cfspeed(baudrate);
|
|
|
SetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int rs232setparity(rs232port_t fd, rs232parity Parity)
|
|
|
{
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DCB dcbSerialParams = {0};
|
|
|
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
|
|
|
GetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
dcbSerialParams.Parity = privatedecodeparity(Parity);
|
|
|
SetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int rs232setnbstop(rs232port_t fd, rs232stop NbStop)
|
|
|
{
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DCB dcbSerialParams = {0};
|
|
|
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
|
|
|
GetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
dcbSerialParams.StopBits = privatedecodestop(NbStop);
|
|
|
SetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
int rs232setcsize(rs232port_t fd, int ChSize)
|
|
|
{
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DCB dcbSerialParams = {0};
|
|
|
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
|
|
|
GetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
dcbSerialParams.ByteSize = ChSize;
|
|
|
SetCommState((HANDLE)fd, &dcbSerialParams);
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize)
|
|
|
{
|
|
|
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DWORD dwBytesWriten = 0;
|
|
|
WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL);
|
|
|
return dwBytesWriten;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize)
|
|
|
{
|
|
|
|
|
|
if (fd == (rs232port_t)INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
return (rs232port_t)badPortValue;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DWORD dwBytesRead = 0;
|
|
|
ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL);
|
|
|
return dwBytesRead;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
int rs232saferead(rs232port_t fd,char* data,int count )
|
|
|
{
|
|
|
int read=0;
|
|
|
int i=0;
|
|
|
for(i=0;i<100;i++)
|
|
|
{
|
|
|
read = rs232read(fd,data,count);
|
|
|
count -=read;
|
|
|
data+=read;
|
|
|
if(count==0)
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int rs232safewrite(rs232port_t fd,char* data,int count)
|
|
|
{
|
|
|
int written=0;
|
|
|
int i=0;
|
|
|
for(i=0;i<1000;i++)
|
|
|
{
|
|
|
written = rs232write(fd,data+written,count);
|
|
|
count-=written;
|
|
|
data+=written;
|
|
|
if(count==0)
|
|
|
return rs232noerr;
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
|
|
|
int rs232setRTS(rs232port_t fd)
|
|
|
{
|
|
|
if(EscapeCommFunction((HANDLE)fd, CLRRTS))
|
|
|
return rs232noerr;
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
int rs232clearRTS(rs232port_t fd)
|
|
|
{
|
|
|
if(EscapeCommFunction((HANDLE)fd, SETRTS))
|
|
|
return rs232noerr;
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
int rs232setDTR(rs232port_t fd)
|
|
|
{
|
|
|
if(EscapeCommFunction((HANDLE)fd, CLRDTR))
|
|
|
return rs232noerr;
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
|
|
|
int rs232clearDTR(rs232port_t fd)
|
|
|
{
|
|
|
if(EscapeCommFunction((HANDLE)fd, SETDTR))
|
|
|
return rs232noerr;
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
#endif //#ifdef HAVE_WINDOWS_H
|
|
|
|