RS232_win.c
322 lines
| 6.8 KiB
| text/x-c
|
CLexer
/ src / RS232_win.c
|
r29 | #include "rs232config.h" | ||
#include <stdio.h> | ||||
#include <unistd.h> | ||||
#include <fcntl.h> | ||||
#include <string.h> | ||||
#include <errno.h> | ||||
#ifdef HAVE_WINDOWS_H | ||||
#include <windows.h> | ||||
#endif | ||||
#include "RS232.h" | ||||
#ifdef HAVE_WINDOWS_H | ||||
|
r31 | 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; | ||||
} | ||||
} | ||||
|
r29 | |||
rs232speed_t rs232cfspeed(unsigned int BaudeRate) | ||||
{ | ||||
if(BaudeRate<123) | ||||
|
r31 | return (rs232speed_t)CBR_110; | ||
|
r29 | |||
if(BaudeRate<450) | ||||
|
r31 | return (rs232speed_t)CBR_300; | ||
|
r29 | |||
if(BaudeRate<900) | ||||
|
r31 | return (rs232speed_t)CBR_600; | ||
|
r29 | |||
if(BaudeRate<1500) | ||||
|
r31 | return (rs232speed_t)CBR_1200; | ||
|
r29 | |||
if(BaudeRate<3600) | ||||
|
r31 | return (rs232speed_t)CBR_2400; | ||
|
r29 | |||
if(BaudeRate<7200) | ||||
|
r31 | return (rs232speed_t)CBR_4800; | ||
|
r29 | |||
if(BaudeRate<14000) | ||||
|
r31 | return (rs232speed_t)CBR_9600; | ||
|
r29 | |||
if(BaudeRate<16800) | ||||
|
r31 | return (rs232speed_t)CBR_14400; | ||
|
r29 | |||
if(BaudeRate<28800) | ||||
|
r31 | return (rs232speed_t)CBR_19200; | ||
|
r29 | |||
if(BaudeRate<48000) | ||||
|
r31 | return (rs232speed_t)CBR_38400; | ||
|
r29 | |||
if(BaudeRate<86400) | ||||
|
r31 | return (rs232speed_t)CBR_57600; | ||
|
r29 | |||
if(BaudeRate<172800) | ||||
|
r31 | return (rs232speed_t)CBR_115200; | ||
|
r29 | else | ||
|
r31 | return (rs232speed_t)CBR_256000; | ||
|
r29 | } | ||
rs232port_t rs232open(char* psPortName) | ||||
{ | ||||
rs232port_t fd; | ||||
|
r31 | fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); | ||
#ifdef debug | ||||
if(fd==INVALID_HANDLE_VALUE) | ||||
{ | ||||
printf("can't open Port\n"); | ||||
return (rs232port_t)badPortValue; | ||||
} | ||||
#endif | ||||
|
r29 | return fd; | ||
} | ||||
int rs232close(rs232port_t fd) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
|
r31 | CloseHandle((HANDLE)fd); | ||
return rs232noerr; | ||||
|
r29 | } | ||
} | ||||
int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
|
r29 | dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate); | ||
dcbSerialParams.ByteSize=ChSize; | ||||
|
r31 | dcbSerialParams.StopBits=privatedecodestop(NbStop); | ||
dcbSerialParams.Parity=privatedecodeparity(Parity); | ||||
SetCommState((HANDLE)fd, &dcbSerialParams); | ||||
|
r29 | COMMTIMEOUTS timeouts={0}; | ||
timeouts.ReadIntervalTimeout=100; | ||||
timeouts.ReadTotalTimeoutConstant=100; | ||||
timeouts.ReadTotalTimeoutMultiplier=1; | ||||
timeouts.WriteTotalTimeoutConstant=100; | ||||
timeouts.WriteTotalTimeoutMultiplier=10; | ||||
|
r31 | SetCommTimeouts((HANDLE)fd, &timeouts); | ||
return rs232noerr; | ||||
|
r29 | } | ||
} | ||||
int rs232setbaudrate(rs232port_t fd, int baudrate) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
|
r29 | dcbSerialParams.BaudRate=rs232cfspeed(baudrate); | ||
|
r31 | SetCommState((HANDLE)fd, &dcbSerialParams); | ||
return rs232noerr; | ||||
|
r29 | } | ||
} | ||||
int rs232setparity(rs232port_t fd, rs232parity Parity) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
dcbSerialParams.Parity = privatedecodeparity(Parity); | ||||
SetCommState((HANDLE)fd, &dcbSerialParams); | ||||
return rs232noerr; | ||||
|
r29 | } | ||
} | ||||
int rs232setnbstop(rs232port_t fd, rs232stop NbStop) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
dcbSerialParams.StopBits = privatedecodestop(NbStop); | ||||
SetCommState((HANDLE)fd, &dcbSerialParams); | ||||
return rs232noerr; | ||||
|
r29 | } | ||
} | ||||
int rs232setcsize(rs232port_t fd, int ChSize) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
|
r29 | dcbSerialParams.ByteSize = ChSize; | ||
|
r31 | SetCommState((HANDLE)fd, &dcbSerialParams); | ||
return rs232noerr; | ||||
|
r29 | } | ||
} | ||||
int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
DWORD dwBytesWriten = 0; | ||||
|
r31 | WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); | ||
|
r29 | return dwBytesWriten; | ||
} | ||||
} | ||||
int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize) | ||||
{ | ||||
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r29 | { | ||
|
r31 | return (rs232port_t)badPortValue; | ||
|
r29 | } | ||
else | ||||
{ | ||||
DWORD dwBytesRead = 0; | ||||
|
r31 | ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL); | ||
|
r29 | 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) | ||||
|
r31 | return rs232noerr; | ||
|
r29 | } | ||
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) | ||||
|
r31 | return rs232noerr; | ||
|
r29 | } | ||
return -1; | ||||
} | ||||
int rs232setRTS(rs232port_t fd) | ||||
{ | ||||
|
r31 | if(EscapeCommFunction((HANDLE)fd, CLRRTS)) | ||
return rs232noerr; | ||||
|
r29 | return -1; | ||
} | ||||
int rs232clearRTS(rs232port_t fd) | ||||
{ | ||||
|
r31 | if(EscapeCommFunction((HANDLE)fd, SETRTS)) | ||
return rs232noerr; | ||||
|
r29 | return -1; | ||
} | ||||
int rs232setDTR(rs232port_t fd) | ||||
{ | ||||
|
r31 | if(EscapeCommFunction((HANDLE)fd, CLRDTR)) | ||
return rs232noerr; | ||||
|
r29 | return -1; | ||
} | ||||
int rs232clearDTR(rs232port_t fd) | ||||
{ | ||||
|
r31 | if(EscapeCommFunction((HANDLE)fd, SETDTR)) | ||
return rs232noerr; | ||||
|
r29 | return -1; | ||
} | ||||
#endif //#ifdef HAVE_WINDOWS_H | ||||