RS232_win.c
395 lines
| 8.6 KiB
| text/x-c
|
CLexer
/ src / RS232_win.c
|
r29 | #include <stdio.h> | ||
#include <unistd.h> | ||||
#include <fcntl.h> | ||||
#include <string.h> | ||||
#include <errno.h> | ||||
|
r33 | #include <malloc.h> | ||
|
r29 | |||
|
r44 | #include <windows.h> | ||
|
r29 | #include "RS232.h" | ||
|
r46 | #include <locale.h> | ||
|
r44 | |||
|
r29 | |||
|
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; | ||||
|
r32 | default: | ||
|
r31 | 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; | ||
|
r42 | |||
if(BaudeRate<345600) | ||||
return (rs232speed_t)230400; | ||||
if(BaudeRate<345600) | ||||
return (rs232speed_t)460800; | ||||
if(BaudeRate<748800) | ||||
return (rs232speed_t)576000; | ||||
if(BaudeRate<1210800) | ||||
return (rs232speed_t)921600; | ||||
if(BaudeRate<1750000) | ||||
return (rs232speed_t)1500000; | ||||
if(BaudeRate<2250000) | ||||
return (rs232speed_t)2000000; | ||||
if(BaudeRate<2750000) | ||||
return (rs232speed_t)2500000; | ||||
|
r29 | else | ||
|
r42 | return (rs232speed_t)3000000; | ||
|
r29 | } | ||
rs232port_t rs232open(char* psPortName) | ||||
{ | ||||
rs232port_t fd; | ||||
|
r46 | /* Char to Wchar conversion*/ | ||
|
r47 | int neededSize = MultiByteToWideChar(CP_ACP, 0, psPortName, -1, 0, 0); | ||
|
r48 | wchar_t* wcstr = (wchar_t*)malloc(sizeof(wchar_t)*neededSize); | ||
MultiByteToWideChar(CP_ACP, 0, psPortName, -1, wcstr, neededSize); | ||||
|
r46 | fd = (rs232port_t)CreateFile(wcstr,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); | ||
|
r48 | free(wcstr); | ||
|
r47 | #ifdef RS232_debug | ||
|
r32 | if(fd==(rs232port_t)INVALID_HANDLE_VALUE) | ||
|
r31 | { | ||
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 | } | ||
} | ||||
|
r34 | rs232portslist_t* rs232getportlist() //here is the very dirty way! | ||
{ | ||||
int i=0; | ||||
|
r35 | char devName[]="COM111"; | ||
|
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; | ||||
r39 | rs232close(testport); | |||
|
r34 | } | ||
} | ||||
return firstitem; | ||||
|
r33 | } | ||
void rs232deleteportlist(rs232portslist_t* list) | ||||
{ | ||||
|
r34 | if(list!=NULL) | ||
{ | ||||
if(list->next != NULL) | ||||
rs232deleteportlist(list->next); | ||||
free(list); | ||||
} | ||||
|
r33 | } | ||
|
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; | ||
} | ||||
} | ||||
r49 | int rs232availablebytes(rs232port_t fd) | |||
{ | ||||
DWORD dwErrorFlags; | ||||
COMSTAT ComStat; | ||||
ClearCommError(m_hIDComDev, &dwErrorFlags, &ComStat); | ||||
return((int)ComStat.cbInQue); | ||||
} | ||||
|
r29 | |||
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; | ||
} | ||||
|
r44 | |||