Fixed some troubles
This commit is contained in:
129
serial.c
129
serial.c
@@ -49,7 +49,11 @@ static pthread_mutex_t mntmutex = PTHREAD_MUTEX_INITIALIZER,
|
||||
// encoders thread and mount thread
|
||||
static pthread_t encthread, mntthread;
|
||||
// max timeout for 1.5 bytes of encoder and 2 bytes of mount - for `select`
|
||||
static struct timeval encRtmout = {.tv_sec = 0, .tv_usec = 100}, mntRtmout = {.tv_sec = 0, .tv_usec = 50000};
|
||||
// this values will be modified later
|
||||
static struct timeval encRtmout = {.tv_sec = 0, .tv_usec = 100}, // encoder reading timeout
|
||||
mnt1Rtmout = {.tv_sec = 0, .tv_usec = 200000}, // first reading
|
||||
mntRtmout = {.tv_sec = 0, .tv_usec = 50000}; // next readings
|
||||
|
||||
// encoders raw data
|
||||
typedef struct __attribute__((packed)){
|
||||
uint8_t magick;
|
||||
@@ -231,43 +235,60 @@ static int getencbyte(){
|
||||
}while(1);
|
||||
return (int)byte;
|
||||
}
|
||||
// read 1 byte from mount; return -1 if nothing to read, -2 if disconnected
|
||||
static int getmntbyte(){
|
||||
if(mntfd < 0) return -1;
|
||||
uint8_t byte;
|
||||
|
||||
/**
|
||||
* @brief readmntdata - read data
|
||||
* @param buffer - input buffer
|
||||
* @param maxlen - maximal buffer length
|
||||
* @return amount of bytes read or -1 in case of error
|
||||
*/
|
||||
static int readmntdata(uint8_t *buffer, int maxlen){
|
||||
if(mntfd < 0){
|
||||
DBG("mntfd non opened");
|
||||
return -1;
|
||||
}
|
||||
if(!buffer || maxlen < 1) return 0;
|
||||
DBG("ask for %d bytes", maxlen);
|
||||
int got = 0;
|
||||
fd_set rfds;
|
||||
/* ssize_t l = read(mntfd, &byte, 1);
|
||||
//DBG("MNT read=%zd byte=0x%X", l, byte);
|
||||
if(l == 0) return -1;
|
||||
if(l != 1) return -2; // disconnected ??
|
||||
return (int) byte;*/
|
||||
struct timeval tv = mnt1Rtmout;
|
||||
do{
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(mntfd, &rfds);
|
||||
struct timeval tv = mntRtmout;
|
||||
DBG("select");
|
||||
int retval = select(mntfd + 1, &rfds, NULL, NULL, &tv);
|
||||
DBG("returned %d", retval);
|
||||
if(retval < 0){
|
||||
if(errno == EINTR) continue;
|
||||
DBG("Error in select()");
|
||||
return -1;
|
||||
}
|
||||
//DBG("FD_ISSET = %d", FD_ISSET(mntfd, &rfds));
|
||||
if(FD_ISSET(mntfd, &rfds)){
|
||||
ssize_t l = read(mntfd, &byte, 1);
|
||||
//DBG("MNT read=%zd byte=0x%X", l, byte);
|
||||
if(l != 1){
|
||||
ssize_t l = read(mntfd, buffer, maxlen);
|
||||
if(l == 0){
|
||||
DBG("read ZERO");
|
||||
continue;
|
||||
}
|
||||
if(l < 0){
|
||||
DBG("Mount disconnected?");
|
||||
return -2; // disconnected ??
|
||||
}
|
||||
buffer += l;
|
||||
maxlen -= l;
|
||||
got += l;
|
||||
}else{
|
||||
DBG("no new data after %d bytes (%s)", got, buffer - got);
|
||||
break;
|
||||
} else return -1;
|
||||
}while(1);
|
||||
return (int)byte;
|
||||
}
|
||||
tv = mntRtmout;
|
||||
}while(maxlen);
|
||||
return got;
|
||||
}
|
||||
|
||||
// clear data from input buffer
|
||||
static void clrmntbuf(){
|
||||
if(mntfd < 0) return;
|
||||
uint8_t byte;
|
||||
uint8_t bytes[256];
|
||||
fd_set rfds;
|
||||
do{
|
||||
FD_ZERO(&rfds);
|
||||
@@ -280,9 +301,10 @@ static void clrmntbuf(){
|
||||
break;
|
||||
}
|
||||
if(FD_ISSET(mntfd, &rfds)){
|
||||
ssize_t l = read(mntfd, &byte, 1);
|
||||
if(l != 1) break;
|
||||
} else break;
|
||||
ssize_t l = read(mntfd, &bytes, 256);
|
||||
if(l < 1) break;
|
||||
DBG("clr got %zd bytes: %s", l, bytes);
|
||||
}else break;
|
||||
}while(1);
|
||||
}
|
||||
|
||||
@@ -426,15 +448,19 @@ static void *encoderthread2(void _U_ *u){
|
||||
if(curt - t0[i] >= Conf.EncoderReqInterval){ // get last records
|
||||
if(curt - mtlast[i] < 1.5*Conf.EncoderReqInterval){
|
||||
pthread_mutex_lock(&datamutex);
|
||||
double pos = (double)msrlast[i];
|
||||
//DBG("pos[%d]=%g", i, pos);
|
||||
;;
|
||||
//DBG("Got med: %g", pos);
|
||||
if(i == 0){
|
||||
mountdata.encXposition.val = Xenc2rad((double)msrlast[i]);
|
||||
mountdata.encXposition.val = Xenc2rad(pos);
|
||||
curtime(&mountdata.encXposition.t);
|
||||
/*DBG("msrlast=%ld, Xpos.val=%g, t=%zd; XEzero=%d, SPR=%g",
|
||||
msrlast[i], mountdata.encXposition.val, mountdata.encXposition.t.tv_sec,
|
||||
X_ENC_ZERO, X_ENC_STEPSPERREV);*/
|
||||
getXspeed();
|
||||
}else{
|
||||
mountdata.encYposition.val = Yenc2rad((double)msrlast[i]);
|
||||
mountdata.encYposition.val = Yenc2rad(pos);
|
||||
curtime(&mountdata.encYposition.t);
|
||||
getYspeed();
|
||||
}
|
||||
@@ -496,7 +522,7 @@ static void chkModStopped(double *prev, double cur, int *nstopped, axis_status_t
|
||||
// main mount thread
|
||||
static void *mountthread(void _U_ *u){
|
||||
int errctr = 0;
|
||||
uint8_t buf[2*sizeof(SSstat)];
|
||||
uint8_t buf[sizeof(SSstat)];
|
||||
SSstat *status = (SSstat*) buf;
|
||||
bzero(&mountdata, sizeof(mountdata));
|
||||
double t0 = timefromstart(), tstart = t0, tcur = t0;
|
||||
@@ -616,15 +642,18 @@ static int ttyopen(const char *path, speed_t speed){
|
||||
|
||||
// return FALSE if failed
|
||||
int openEncoder(){
|
||||
// TODO: open real devices in "model" mode too!
|
||||
if(Conf.RunModel) return TRUE;
|
||||
if(!Conf.SepEncoder) return FALSE; // try to open separate encoder when it's absent
|
||||
/*
|
||||
encRtmout.tv_sec = 0;
|
||||
encRtmout.tv_usec = 100000000 / Conf.EncoderDevSpeed; // 10 bytes
|
||||
*/
|
||||
if(Conf.SepEncoder == 1){ // only one device
|
||||
DBG("One device");
|
||||
if(encfd[0] > -1) close(encfd[0]);
|
||||
encfd[0] = ttyopen(Conf.EncoderDevPath, (speed_t) Conf.EncoderDevSpeed);
|
||||
if(encfd[0] < 0) return FALSE;
|
||||
encRtmout.tv_sec = 0;
|
||||
encRtmout.tv_usec = 100000000 / Conf.EncoderDevSpeed; // 10 bytes
|
||||
if(pthread_create(&encthread, NULL, encoderthread1, NULL)){
|
||||
close(encfd[0]);
|
||||
encfd[0] = -1;
|
||||
@@ -638,8 +667,6 @@ int openEncoder(){
|
||||
encfd[i] = ttyopen(paths[i], (speed_t) Conf.EncoderDevSpeed);
|
||||
if(encfd[i] < 0) return FALSE;
|
||||
}
|
||||
encRtmout.tv_sec = 0;
|
||||
encRtmout.tv_usec = 100000000 / Conf.EncoderDevSpeed;
|
||||
if(pthread_create(&encthread, NULL, encoderthread2, NULL)){
|
||||
for(int i = 0; i < 2; ++i){
|
||||
close(encfd[i]);
|
||||
@@ -654,6 +681,7 @@ int openEncoder(){
|
||||
|
||||
// return FALSE if failed
|
||||
int openMount(){
|
||||
// TODO: open real devices in "model" mode too!
|
||||
if(Conf.RunModel) goto create_thread;
|
||||
if(mntfd > -1) close(mntfd);
|
||||
DBG("Open mount %s @ %d", Conf.MountDevPath, Conf.MountDevSpeed);
|
||||
@@ -661,16 +689,13 @@ int openMount(){
|
||||
if(mntfd < 0) return FALSE;
|
||||
DBG("mntfd=%d", mntfd);
|
||||
// clear buffer
|
||||
while(getmntbyte() > -1);
|
||||
/*int g = write(mntfd, "XXS\r", 4);
|
||||
DBG("Written %d", g);
|
||||
uint8_t buf[100];
|
||||
do{
|
||||
ssize_t l = read(mntfd, buf, 100);
|
||||
DBG("got %zd", l);
|
||||
}while(1);*/
|
||||
clrmntbuf();
|
||||
/*
|
||||
mnt1Rtmout.tv_sec = 0;
|
||||
mnt1Rtmout.tv_usec = 500000000 / Conf.MountDevSpeed; // 50 bytes * 10bits / speed
|
||||
mntRtmout.tv_sec = 0;
|
||||
mntRtmout.tv_usec = 500000000 / Conf.MountDevSpeed; // 50 bytes * 10bits / speed
|
||||
mntRtmout.tv_usec = mnt1Rtmout.tv_usec / 50;
|
||||
*/
|
||||
create_thread:
|
||||
if(pthread_create(&mntthread, NULL, mountthread, NULL)){
|
||||
DBG("Can't create mount thread");
|
||||
@@ -686,8 +711,7 @@ create_thread:
|
||||
|
||||
// close all opened serial devices and quit threads
|
||||
void closeSerial(){
|
||||
// TODO: close devices in "model" mode too!
|
||||
if(Conf.RunModel) return;
|
||||
DBG("CLOSE all devices");
|
||||
if(mntfd > -1){
|
||||
DBG("Cancel mount thread");
|
||||
pthread_cancel(mntthread);
|
||||
@@ -737,26 +761,29 @@ static int wr(const data_t *out, data_t *in, int needeol){
|
||||
DBG("Wrong arguments or no mount fd");
|
||||
return FALSE;
|
||||
}
|
||||
DBG("clrbuf");
|
||||
clrmntbuf();
|
||||
if(out){
|
||||
DBG("write %zd bytes (%s)", out->len, out->buf);
|
||||
if(out->len != (size_t)write(mntfd, out->buf, out->len)){
|
||||
DBG("written bytes not equal to need");
|
||||
return FALSE;
|
||||
}
|
||||
//DBG("eol, mntfd=%d", mntfd);
|
||||
if(needeol){
|
||||
int g = write(mntfd, "\r", 1); // add EOL
|
||||
(void) g;
|
||||
}
|
||||
usleep(50000); // add little pause so that the idiot has time to swallow
|
||||
//usleep(50000); // add little pause so that the idiot has time to swallow
|
||||
}
|
||||
if(!in) return TRUE;
|
||||
in->len = 0;
|
||||
for(size_t i = 0; i < in->maxlen; ++i){
|
||||
int b = getmntbyte();
|
||||
if(b < 0) break; // nothing to read -> go out
|
||||
in->buf[in->len++] = (uint8_t) b;
|
||||
if(!in || in->maxlen < 1) return TRUE;
|
||||
int got = readmntdata(in->buf, in->maxlen);
|
||||
if(got < 0){
|
||||
DBG("Error reading mount data!");
|
||||
in->len = 0;
|
||||
return FALSE;
|
||||
}
|
||||
while(getmntbyte() > -1);
|
||||
in->len = got;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -767,15 +794,17 @@ static int wr(const data_t *out, data_t *in, int needeol){
|
||||
* @return FALSE if failed
|
||||
*/
|
||||
int MountWriteRead(const data_t *out, data_t *in){
|
||||
if(Conf.RunModel) return -1;
|
||||
if(Conf.RunModel) return FALSE;
|
||||
double t0 = timefromstart();
|
||||
pthread_mutex_lock(&mntmutex);
|
||||
int ret = wr(out, in, 1);
|
||||
pthread_mutex_unlock(&mntmutex);
|
||||
DBG("Got %gus", (timefromstart()-t0)*1e6);
|
||||
return ret;
|
||||
}
|
||||
// send binary data - without EOL
|
||||
int MountWriteReadRaw(const data_t *out, data_t *in){
|
||||
if(Conf.RunModel) return -1;
|
||||
if(Conf.RunModel) return FALSE;
|
||||
pthread_mutex_lock(&mntmutex);
|
||||
int ret = wr(out, in, 0);
|
||||
pthread_mutex_unlock(&mntmutex);
|
||||
|
||||
Reference in New Issue
Block a user