serial: Set ATTN line high on start. Added checks.

This commit is contained in:
Martin Preuss
2023-01-26 18:58:32 +01:00
parent d51eda9604
commit e8e6df0fba

View File

@@ -15,6 +15,7 @@
#include <gwenhywfar/misc.h> #include <gwenhywfar/misc.h>
#include <gwenhywfar/debug.h> #include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <string.h> #include <string.h>
@@ -116,6 +117,9 @@ int AQH_Serial_Open(AQH_SERIAL *sr)
} }
sr->fd=fd; sr->fd=fd;
_attnHigh(sr);
return 0; return 0;
} }
@@ -380,6 +384,7 @@ int _check(const uint8_t *ptr, uint8_t len)
} }
if (x) { if (x) {
DBG_ERROR(NULL, "Bad checksum"); DBG_ERROR(NULL, "Bad checksum");
GWEN_Text_DumpString(ptr, len, 2);
return GWEN_ERROR_GENERIC; return GWEN_ERROR_GENERIC;
} }
@@ -438,7 +443,14 @@ int _readFromFd(AQH_SERIAL *sr)
} }
else { else {
if (sr->bytesToRead==0) { if (sr->bytesToRead==0) {
/* msg received, handle */ /* msg received, check */
if (_check(sr->readBuffer, sr->readPos)<0) {
DBG_INFO(0, "here (%d)", rv);
sr->readPos=0;
sr->bytesToRead=2;
return 0;
}
/* valid msg received, handle */
AQH_Serial_PacketReceived(sr, sr->readBuffer, sr->readPos); AQH_Serial_PacketReceived(sr, sr->readBuffer, sr->readPos);
sr->readPos=0; sr->readPos=0;
sr->bytesToRead=2; sr->bytesToRead=2;
@@ -502,7 +514,7 @@ int AQH_Serial_StartWriting(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
return rv; return rv;
} }
usleep(10); usleep(50);
return 0; return 0;
} }
@@ -516,7 +528,7 @@ int AQH_Serial_Loop(AQH_SERIAL *sr)
struct timeval tv; struct timeval tv;
int rv; int rv;
tv.tv_sec=5; tv.tv_sec=2;
tv.tv_usec=0; tv.tv_usec=0;
if (sr->bytesToWrite) { if (sr->bytesToWrite) {
@@ -528,6 +540,10 @@ int AQH_Serial_Loop(AQH_SERIAL *sr)
rv=select(sr->fd+1, &readSet, (sr->bytesToWrite)?(&writeSet):NULL, NULL, &tv); rv=select(sr->fd+1, &readSet, (sr->bytesToWrite)?(&writeSet):NULL, NULL, &tv);
if (rv<0) { if (rv<0) {
if (errno!=EINTR) {
DBG_ERROR(NULL, "Error on select");
return GWEN_ERROR_IO;
}
} }
else if (rv) { else if (rv) {
if (FD_ISSET(sr->fd, &readSet)) { if (FD_ISSET(sr->fd, &readSet)) {
@@ -545,6 +561,11 @@ int AQH_Serial_Loop(AQH_SERIAL *sr)
} }
} }
} }
else if (rv==0) {
/* timeout */
sr->readPos=0;
sr->bytesToRead=2;
}
return 0; return 0;
} }