RS232_win.c
223 lines
| 4.2 KiB
| text/x-c
|
CLexer
/ src / RS232_win.c
yannic
|
r7 | #include "rs232config.h" | ||
#include <stdio.h> | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | #include <unistd.h> | ||
#include <fcntl.h> | ||||
#include <string.h> | ||||
#include <errno.h> | ||||
yannic
|
r7 | |||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | #ifdef HAVE_WINDOWS_H | ||
#include <windows.h> | ||||
#endif | ||||
#include "RS232.h" | ||||
#ifdef HAVE_WINDOWS_H | ||||
rs232speed_t rs232cfspeed(unsigned int BaudeRate) | ||||
{ | ||||
if(BaudeRate<123) | ||||
return CBR_110; | ||||
if(BaudeRate<450) | ||||
return CBR_300; | ||||
if(BaudeRate<900) | ||||
return CBR_600; | ||||
if(BaudeRate<1500) | ||||
return CBR_1200; | ||||
if(BaudeRate<3600) | ||||
return CBR_2400; | ||||
if(BaudeRate<7200) | ||||
return CBR_4800; | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if(BaudeRate<14000) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | return CBR_9600; | ||
if(BaudeRate<16800) | ||||
return CBR_14400; | ||||
if(BaudeRate<28800) | ||||
return CBR_19200; | ||||
if(BaudeRate<48000) | ||||
return CBR_38400; | ||||
if(BaudeRate<86400) | ||||
return CBR_57600; | ||||
if(BaudeRate<172800) | ||||
return CBR_115200; | ||||
else | ||||
return CBR_256000; | ||||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | rs232port_t rs232open(char* psPortName) | ||
{ | ||||
rs232port_t fd; | ||||
fd = CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); | ||||
return fd; | ||||
} | ||||
int rs232close(rs232port_t fd) | ||||
{ | ||||
if (fd == badPortValue) | ||||
{ | ||||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
CloseHandle(fd); | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) | ||||
{ | ||||
if (fd == badPortValue) | ||||
{ | ||||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
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=10; | ||||
timeouts.ReadTotalTimeoutConstant=10; | ||||
timeouts.ReadTotalTimeoutMultiplier=1; | ||||
timeouts.WriteTotalTimeoutConstant=50; | ||||
timeouts.WriteTotalTimeoutMultiplier=10; | ||||
SetCommTimeouts(fd, &timeouts); | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232setbaudrate(rs232port_t fd, int baudrate) | ||||
{ | ||||
if (fd == badPortValue) | ||||
{ | ||||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
GetCommState(fd, &dcbSerialParams); | ||||
dcbSerialParams.BaudRate=rs232cfspeed(baudrate); | ||||
SetCommState(fd, &dcbSerialParams); | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232setparity(rs232port_t fd, rs232parity Parity) | ||||
{ | ||||
if (fd == badPortValue) | ||||
{ | ||||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
GetCommState(fd, &dcbSerialParams); | ||||
dcbSerialParams.Parity = Parity; | ||||
SetCommState(fd, &dcbSerialParams); | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232setnbstop(rs232port_t fd, rs232stop NbStop) | ||||
{ | ||||
if (fd == badPortValue) | ||||
{ | ||||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
GetCommState(fd, &dcbSerialParams); | ||||
switch(NbStop) | ||||
{ | ||||
case 2: | ||||
dcbSerialParams.StopBits = 2; | ||||
break; | ||||
default: | ||||
dcbSerialParams.StopBits = 1; | ||||
break; | ||||
} | ||||
SetCommState(fd, &dcbSerialParams); | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232setcsize(rs232port_t fd, int ChSize) | ||||
{ | ||||
if (fd == badPortValue) | ||||
{ | ||||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
DCB dcbSerialParams = {0}; | ||||
dcbSerialParams.DCBlength=sizeof(dcbSerialParams); | ||||
GetCommState(fd, &dcbSerialParams); | ||||
dcbSerialParams.ByteSize = ChSize; | ||||
SetCommState(fd, &dcbSerialParams); | ||||
return 0; | ||||
} | ||||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) | ||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if (fd == badPortValue) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
DWORD dwBytesWriten = 0; | ||||
WriteFile(fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); | ||||
return dwBytesWriten; | ||||
} | ||||
} | ||||
int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if (fd == badPortValue) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
DWORD dwBytesRead = 0; | ||||
ReadFile(fd, psRead, ReadBufferSize, &dwBytesRead, NULL); | ||||
return dwBytesRead; | ||||
} | ||||
} | ||||
yannic
|
r7 | #endif //#ifdef HAVE_WINDOWS_H | ||