RS232_win.c
364 lines
| 7.8 KiB
| text/x-c
|
CLexer
/ src / RS232_win.c
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | #include "rs232config.h" | ||
#include <stdio.h> | ||||
#include <unistd.h> | ||||
#include <fcntl.h> | ||||
#include <string.h> | ||||
#include <errno.h> | ||||
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
|
r33 | #include <malloc.h> | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
#ifdef HAVE_WINDOWS_H | ||||
#include <windows.h> | ||||
#endif | ||||
#include "RS232.h" | ||||
#ifdef HAVE_WINDOWS_H | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
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; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r32 | default: | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return ONESTOPBIT; | ||
break; | ||||
} | ||||
} | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
rs232speed_t rs232cfspeed(unsigned int BaudeRate) | ||||
{ | ||||
if(BaudeRate<123) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_110; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<450) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_300; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<900) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_600; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<1500) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_1200; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<3600) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_2400; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<7200) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_4800; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<14000) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_9600; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<16800) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_14400; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<28800) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_19200; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<48000) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_38400; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<86400) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_57600; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | |||
if(BaudeRate<172800) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_115200; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | else | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232speed_t)CBR_256000; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
rs232port_t rs232open(char* psPortName) | ||||
{ | ||||
rs232port_t fd; | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); | ||
#ifdef debug | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r32 | if(fd==(rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | { | ||
printf("can't open Port\n"); | ||||
return (rs232port_t)badPortValue; | ||||
} | ||||
#endif | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | return fd; | ||
} | ||||
int rs232close(rs232port_t fd) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | CloseHandle((HANDLE)fd); | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
} | ||||
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
|
r34 | rs232portslist_t* rs232getportlist() //here is the very dirty way! | ||
{ | ||||
int i=0; | ||||
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
|
r35 | char devName[]="COM111"; | ||
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
|
r34 | 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; | ||||
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
|
r33 | } | ||
void rs232deleteportlist(rs232portslist_t* list) | ||||
{ | ||||
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
|
r34 | if(list!=NULL) | ||
{ | ||||
if(list->next != NULL) | ||||
rs232deleteportlist(list->next); | ||||
free(list); | ||||
} | ||||
jeandet@pc-de-jeandet3.LAB-LPP.LOCAL
|
r33 | } | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) | ||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate); | ||
dcbSerialParams.ByteSize=ChSize; | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | dcbSerialParams.StopBits=privatedecodestop(NbStop); | ||
dcbSerialParams.Parity=privatedecodeparity(Parity); | ||||
SetCommState((HANDLE)fd, &dcbSerialParams); | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | COMMTIMEOUTS timeouts={0}; | ||
timeouts.ReadIntervalTimeout=100; | ||||
timeouts.ReadTotalTimeoutConstant=100; | ||||
timeouts.ReadTotalTimeoutMultiplier=1; | ||||
timeouts.WriteTotalTimeoutConstant=100; | ||||
timeouts.WriteTotalTimeoutMultiplier=10; | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | SetCommTimeouts((HANDLE)fd, &timeouts); | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
} | ||||
int rs232setbaudrate(rs232port_t fd, int baudrate) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | dcbSerialParams.BaudRate=rs232cfspeed(baudrate); | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | SetCommState((HANDLE)fd, &dcbSerialParams); | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
} | ||||
int rs232setparity(rs232port_t fd, rs232parity Parity) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
dcbSerialParams.Parity = privatedecodeparity(Parity); | ||||
SetCommState((HANDLE)fd, &dcbSerialParams); | ||||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
} | ||||
int rs232setnbstop(rs232port_t fd, rs232stop NbStop) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
dcbSerialParams.StopBits = privatedecodestop(NbStop); | ||||
SetCommState((HANDLE)fd, &dcbSerialParams); | ||||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
} | ||||
int rs232setcsize(rs232port_t fd, int ChSize) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | GetCommState((HANDLE)fd, &dcbSerialParams); | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | dcbSerialParams.ByteSize = ChSize; | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | SetCommState((HANDLE)fd, &dcbSerialParams); | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
} | ||||
int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
DWORD dwBytesWriten = 0; | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | return dwBytesWriten; | ||
} | ||||
} | ||||
int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if (fd == (rs232port_t)INVALID_HANDLE_VALUE) | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | { | ||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return (rs232port_t)badPortValue; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
else | ||||
{ | ||||
DWORD dwBytesRead = 0; | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL); | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
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) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return rs232noerr; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
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) | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | return rs232noerr; | ||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | } | ||
return -1; | ||||
} | ||||
int rs232setRTS(rs232port_t fd) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if(EscapeCommFunction((HANDLE)fd, CLRRTS)) | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | return -1; | ||
} | ||||
int rs232clearRTS(rs232port_t fd) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if(EscapeCommFunction((HANDLE)fd, SETRTS)) | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | return -1; | ||
} | ||||
int rs232setDTR(rs232port_t fd) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if(EscapeCommFunction((HANDLE)fd, CLRDTR)) | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | return -1; | ||
} | ||||
int rs232clearDTR(rs232port_t fd) | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r31 | if(EscapeCommFunction((HANDLE)fd, SETDTR)) | ||
return rs232noerr; | ||||
jeandet@PC-DE-JEANDET.lab-lpp.local
|
r29 | return -1; | ||
} | ||||
#endif //#ifdef HAVE_WINDOWS_H | ||||