Skip to content

Commit 694e6dc

Browse files
committed
edit serialib to work with cygwin
1 parent cda5c17 commit 694e6dc

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

MIDAS/lib/serialib/serialib.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This is a licence-free software, it can be used by anyone who try to build a bet
1717

1818
#include "serialib.h"
1919

20+
#include <iostream>
21+
2022

2123

2224
//_____________________________________
@@ -34,7 +36,7 @@ serialib::serialib()
3436
currentStateDTR=true;
3537
hSerial = INVALID_HANDLE_VALUE;
3638
#endif
37-
#if defined (__linux__) || defined(__APPLE__)
39+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
3840
fd = -1;
3941
#endif
4042
}
@@ -237,7 +239,7 @@ char serialib::openDevice(const char *Device, const unsigned int Bauds,
237239
// Opening successfull
238240
return 1;
239241
#endif
240-
#if defined (__linux__) || defined(__APPLE__)
242+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
241243
// Structure with the device's options
242244
struct termios options;
243245

@@ -360,7 +362,7 @@ bool serialib::isDeviceOpen()
360362
#if defined (_WIN32) || defined( _WIN64)
361363
return hSerial != INVALID_HANDLE_VALUE;
362364
#endif
363-
#if defined (__linux__) || defined(__APPLE__)
365+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
364366
return fd >= 0;
365367
#endif
366368
}
@@ -374,7 +376,7 @@ void serialib::closeDevice()
374376
CloseHandle(hSerial);
375377
hSerial = INVALID_HANDLE_VALUE;
376378
#endif
377-
#if defined (__linux__) || defined(__APPLE__)
379+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
378380
close (fd);
379381
fd = -1;
380382
#endif
@@ -405,7 +407,7 @@ int serialib::writeChar(const char Byte)
405407
// Write operation successfull
406408
return 1;
407409
#endif
408-
#if defined (__linux__) || defined(__APPLE__)
410+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
409411
// Write the char
410412
if (write(fd,&Byte,1)!=1) return -1;
411413

@@ -438,7 +440,7 @@ int serialib::writeString(const char *receivedString)
438440
// Write operation successfull
439441
return 1;
440442
#endif
441-
#if defined (__linux__) || defined(__APPLE__)
443+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
442444
// Lenght of the string
443445
int Lenght=strlen(receivedString);
444446
// Write the string
@@ -470,7 +472,7 @@ int serialib::writeBytes(const void *Buffer, const unsigned int NbBytes, unsigne
470472
// Write operation successfull
471473
return 1;
472474
#endif
473-
#if defined (__linux__) || defined(__APPLE__)
475+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
474476
// Write data
475477
*NbBytesWritten = write (fd,Buffer,NbBytes);
476478
if (*NbBytesWritten !=(ssize_t)NbBytes) return -1;
@@ -516,7 +518,7 @@ int serialib::readChar(char *pByte,unsigned int timeOut_ms)
516518
// The byte is read
517519
return 1;
518520
#endif
519-
#if defined (__linux__) || defined(__APPLE__)
521+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
520522
// Timer used for timeout
521523
timeOut timer;
522524
// Initialise the timer
@@ -691,7 +693,7 @@ int serialib::readBytes (void *buffer,unsigned int maxNbBytes,unsigned int timeO
691693
// Return the byte read
692694
return dwBytesRead;
693695
#endif
694-
#if defined (__linux__) || defined(__APPLE__)
696+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
695697
// Timer used for timeout
696698
timeOut timer;
697699
// Initialise the timer
@@ -744,7 +746,7 @@ char serialib::flushReceiver()
744746
// Purge receiver
745747
return PurgeComm (hSerial, PURGE_RXCLEAR);
746748
#endif
747-
#if defined (__linux__) || defined(__APPLE__)
749+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
748750
// Purge receiver
749751
tcflush(fd,TCIFLUSH);
750752
return true;
@@ -769,7 +771,7 @@ int serialib::available()
769771
// Return the number of pending bytes
770772
return commStatus.cbInQue;
771773
#endif
772-
#if defined (__linux__) || defined(__APPLE__)
774+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
773775
int nBytes=0;
774776
// Return number of pending bytes in the receiver
775777
ioctl(fd, FIONREAD, &nBytes);
@@ -816,7 +818,7 @@ bool serialib::setDTR()
816818
currentStateDTR=true;
817819
return EscapeCommFunction(hSerial,SETDTR);
818820
#endif
819-
#if defined (__linux__) || defined(__APPLE__)
821+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
820822
// Set DTR
821823
int status_DTR=0;
822824
ioctl(fd, TIOCMGET, &status_DTR);
@@ -839,7 +841,7 @@ bool serialib::clearDTR()
839841
currentStateDTR=false;
840842
return EscapeCommFunction(hSerial,CLRDTR);
841843
#endif
842-
#if defined (__linux__) || defined(__APPLE__)
844+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
843845
// Clear DTR
844846
int status_DTR=0;
845847
ioctl(fd, TIOCMGET, &status_DTR);
@@ -884,7 +886,7 @@ bool serialib::setRTS()
884886
currentStateRTS=true;
885887
return EscapeCommFunction(hSerial,SETRTS);
886888
#endif
887-
#if defined (__linux__) || defined(__APPLE__)
889+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
888890
// Set RTS
889891
int status_RTS=0;
890892
ioctl(fd, TIOCMGET, &status_RTS);
@@ -909,7 +911,7 @@ bool serialib::clearRTS()
909911
currentStateRTS=false;
910912
return EscapeCommFunction(hSerial,CLRRTS);
911913
#endif
912-
#if defined (__linux__) || defined(__APPLE__)
914+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
913915
// Clear RTS
914916
int status_RTS=0;
915917
ioctl(fd, TIOCMGET, &status_RTS);
@@ -934,7 +936,7 @@ bool serialib::isCTS()
934936
GetCommModemStatus(hSerial, &modemStat);
935937
return modemStat & MS_CTS_ON;
936938
#endif
937-
#if defined (__linux__) || defined(__APPLE__)
939+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
938940
int status=0;
939941
//Get the current status of the CTS bit
940942
ioctl(fd, TIOCMGET, &status);
@@ -956,7 +958,7 @@ bool serialib::isDSR()
956958
GetCommModemStatus(hSerial, &modemStat);
957959
return modemStat & MS_DSR_ON;
958960
#endif
959-
#if defined (__linux__) || defined(__APPLE__)
961+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
960962
int status=0;
961963
//Get the current status of the DSR bit
962964
ioctl(fd, TIOCMGET, &status);
@@ -982,7 +984,7 @@ bool serialib::isDCD()
982984
GetCommModemStatus(hSerial, &modemStat);
983985
return modemStat & MS_RLSD_ON;
984986
#endif
985-
#if defined (__linux__) || defined(__APPLE__)
987+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
986988
int status=0;
987989
//Get the current status of the DCD bit
988990
ioctl(fd, TIOCMGET, &status);
@@ -1003,7 +1005,7 @@ bool serialib::isRI()
10031005
GetCommModemStatus(hSerial, &modemStat);
10041006
return modemStat & MS_RING_ON;
10051007
#endif
1006-
#if defined (__linux__) || defined(__APPLE__)
1008+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
10071009
int status=0;
10081010
//Get the current status of the RING bit
10091011
ioctl(fd, TIOCMGET, &status);
@@ -1023,7 +1025,7 @@ bool serialib::isDTR()
10231025
#if defined (_WIN32) || defined( _WIN64)
10241026
return currentStateDTR;
10251027
#endif
1026-
#if defined (__linux__) || defined(__APPLE__)
1028+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
10271029
int status=0;
10281030
//Get the current status of the DTR bit
10291031
ioctl(fd, TIOCMGET, &status);
@@ -1044,7 +1046,7 @@ bool serialib::isRTS()
10441046
#if defined (_WIN32) || defined(_WIN64)
10451047
return currentStateRTS;
10461048
#endif
1047-
#if defined (__linux__) || defined(__APPLE__)
1049+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
10481050
int status=0;
10491051
//Get the current status of the CTS bit
10501052
ioctl(fd, TIOCMGET, &status);

MIDAS/lib/serialib/serialib.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is a licence-free software, it can be used by anyone who try to build a bet
2222
#if defined(__CYGWIN__)
2323
// This is Cygwin special case
2424
#include <sys/time.h>
25+
#define FIONREAD TIOCINQ
2526
#endif
2627

2728
// Include for windows
@@ -38,7 +39,7 @@ This is a licence-free software, it can be used by anyone who try to build a bet
3839
#endif
3940

4041
// Include for Linux
41-
#if defined (__linux__) || defined(__APPLE__)
42+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
4243
#include <stdlib.h>
4344
#include <sys/types.h>
4445
#include <sys/shm.h>
@@ -231,7 +232,7 @@ class serialib
231232
// For setting serial port timeouts
232233
COMMTIMEOUTS timeouts;
233234
#endif
234-
#if defined (__linux__) || defined(__APPLE__)
235+
#if defined (__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
235236
int fd;
236237
#endif
237238

MIDAS/src/hilsim/stream/main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ int main(int argc, char** argv) {
196196
setup_ssizes();
197197

198198
serialib Serial;
199-
bool _ser_open = false;
200199

201200
printf("Awaiting commands from stdin...\n");
202201

@@ -215,7 +214,7 @@ int main(int argc, char** argv) {
215214
while (true) {
216215

217216
// handle serial output
218-
if(_ser_open) {
217+
if(Serial.isDeviceOpen()) {
219218
// do something
220219

221220
// TBD
@@ -235,14 +234,11 @@ int main(int argc, char** argv) {
235234
{
236235
char sbuf[128];
237236
sscanf(_inbuf + 1, " %s", &sbuf);
238-
239237
char serial_open_err = Serial.openDevice(sbuf, 115200);
240238
if (serial_open_err != 1) {
241239
printf(".SERIAL_BAD\n");
242-
_ser_open = false;
243240
} else {
244241
printf(".SERIAL_OK\n");
245-
_ser_open = true;
246242
}
247243
fflush(stdout);
248244
}
@@ -343,7 +339,7 @@ int main(int argc, char** argv) {
343339
break;
344340
}
345341

346-
if (!_ser_open) {
342+
if (!Serial.isDeviceOpen()) {
347343
std::cerr << "No serial open" << std::endl;
348344
printf(".ERR no_ser\n");
349345
break;

0 commit comments

Comments
 (0)