Index: trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 9435)
+++ trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 9439)
@@ -104,5 +104,6 @@
     cout << "Zd/Az: " << zd << "/" << az << "  ";
     cout << "Ra/Dec: " << ra/15 << "h/" << dec << "  ";
-    cout << "SolRad: " << solar << "W/m²" << endl;
+    //cout << "SolRad: " << solar << "W/m²";
+    cout << endl;
 
     fComStat = kCmdReceived;
@@ -122,22 +123,22 @@
         return InterpreteReport(str);
 
-    const bool rc = InterpreteCmd(cmd, str.Strip(TString::kBoth));
-    fComStat = rc ? kCmdReceived : kComProblem;
-    return rc;
+    return InterpreteCmd(cmd, str.Strip(TString::kBoth));
 }
 
 bool MCeCoCom::SendRep(const char *cmd, const char *str, bool force)
 {
-    MTime t;
-    t.Now();
 
     UShort_t y1, y2, ms1, ms2;
     Byte_t mon1, mon2, d1, d2, h1, h2, m1, m2, s1, s2;
 
+    fT.GetDate(y2, mon2, d2);
+    fT.GetTime(h2, m2, s2, ms2);
+
+    MTime t(-1);
     t.GetDate(y1, mon1, d1);
     t.GetTime(h1, m1, s1, ms1);
 
-    fT.GetDate(y2, mon2, d2);
-    fT.GetTime(h2, m2, s2, ms2);
+    if (t-fT>20)
+        fComStat = kComProblem;
 
     const TString msg =
@@ -152,5 +153,4 @@
     // Send report to CC
     const bool rc = Send(msg.Data(), msg.Length());
-    fComStat = rc ? kNoCmdReceived : kComProblem;
 
     // Write report to stream
Index: trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 9435)
+++ trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 9439)
@@ -19,7 +19,6 @@
     enum ComStatus_t
     {
-        kCmdReceived,
-        kNoCmdReceived,
-        kComProblem
+        kCmdReceived = 1,
+        kComProblem  = 2
     };
 
@@ -52,5 +51,5 @@
     MCeCoCom(const char *addr, const int tx=7304, const int rx=7404, MLog *out=NULL)
         : MTcpIpIO(addr, tx, rx), fOut(out), fStatus(0),
-        fTelescope(1), fComStat(kNoCmdReceived),
+        fTelescope(1), fComStat(kComProblem),
         fHumidity(0), fTemperature(0), fWindSpeed(0), fSolarRadiation(-1),
         fAlarmCounter(0)
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 9435)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 9439)
@@ -7,4 +7,8 @@
 #include "MString.h"
 #include "Ring.h"
+#include "Led.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
 
 using namespace std;
@@ -45,14 +49,14 @@
     if (!ReadPosition(str, ra, dec))
     {
-        cout << "ERROR - Reading position from RADEC" << endl;
+        gLog << err << "ERROR - Reading position from RADEC" << endl;
         return false;
     }
     if (!str.IsNull())
     {
-        cout << "ERROR - Too many bytes in command RADEC" << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
+        gLog << err << "ERROR - Too many bytes in command RADEC" << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
 
     ra *= 15; // h -> deg
@@ -72,14 +76,14 @@
     if (!ReadPosition(str, ra, dec))
     {
-        cout << "ERROR - Reading position from GRB" << endl;
+        gLog << err << "ERROR - Reading position from GRB" << endl;
         return false;
     }
     if (!str.IsNull())
     {
-        cout << "ERROR - Too many bytes in command GRB" << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
+        gLog << err << "ERROR - Too many bytes in command GRB" << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
 
     ra *= 15; // h -> deg
@@ -99,5 +103,5 @@
     if (!ReadPosition(str, zd, az))
     {
-        cout << "ERROR - Reading position from ZDAZ" << endl;
+        gLog << err << "ERROR - Reading position from ZDAZ" << endl;
         return false;
     }
@@ -105,9 +109,9 @@
     if (!str.IsNull())
     {
-        cout << "ERROR - Too many bytes in command ZDAZ" << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
+        gLog << err << "ERROR - Too many bytes in command ZDAZ" << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
 
     ZdAz za(zd, az);
@@ -125,10 +129,10 @@
     if (str.IsNull())
     {
-        cout << "ERROR - No identifier for preposition (PREPS) given." << endl;
+        gLog << err << "ERROR - No identifier for preposition (PREPS) given." << endl;
         return false;
     }
     if (str.First(' ')>=0)
     {
-        cout << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
+        gLog << err << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
         return false;
     }
@@ -136,5 +140,5 @@
     str.ToLower();
 
-    cout << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
+    gLog << all << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
 
     //cout << "MDriveCom - TRACK... start." << endl;
@@ -147,8 +151,19 @@
 bool MDriveCom::CommandTPOINT(TString &str)
 {
-    cout << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
-
-    if (fQueue)
-        fQueue->PostMsg(WM_TPOINT);//, (void*)str.Data(), str.Length()+1);
+    gLog << all << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
+
+    TObjArray *arr = str.Tokenize(' ');
+
+    if (arr->GetEntries()!=2)
+    {
+        delete arr;
+        gLog << err << "ERROR - Wrong number of arguments in TPOIN command" << endl;
+        return false;
+    }
+
+    delete arr;
+
+    if (fQueue)
+        fQueue->PostMsg(WM_STARGTPOINT, (void*)str.Data(), str.Length()+1);//, (void*)str.Data(), str.Length()+1);
 
     return true;
@@ -157,8 +172,10 @@
 bool MDriveCom::CommandSTGMD(TString &str)
 {
-    cout << "CC-COMMAND " << MTime(-1) << "STGMD" << endl;
-
-    if (fQueue)
-        fQueue->PostMsg(WM_STARGMODE);//, (void*)str.Data(), str.Length()+1);
+    gLog << all << "CC-COMMAND " << MTime(-1) << " STGMD " << str << endl;
+
+    bool on = str=="ON";
+
+    if (fQueue)
+        fQueue->PostMsg(WM_STARGMODE, &on, sizeof(bool));//, (void*)str.Data(), str.Length()+1);
 
     return true;
@@ -170,10 +187,10 @@
     if (str.IsNull())
     {
-        cout << "ERROR - No identifier for ARM command." << endl;
+        gLog << err << "ERROR - No identifier for ARM command." << endl;
         return false;
     }
     if (str.First(' ')>=0)
     {
-        cout << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
+        gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
         return false;
     }
@@ -182,9 +199,9 @@
     if (str!="lock" && str!="unlock")
     {
-        cout << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
+        gLog << err << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
 
     bool lock = str=="lock";
@@ -200,5 +217,5 @@
     {
         //cout << "MDriveCom - WAIT... start." << endl;
-        cout << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
+        gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
         if (fQueue)
             fQueue->PostMsg(WM_WAIT);
@@ -210,5 +227,5 @@
     {
         //cout << "MDriveCom - STOP!... start." << endl;
-        cout << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
+        gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
         if (fQueue)
             fQueue->PostMsg(WM_STOP);
@@ -240,9 +257,9 @@
     if (cmd.IsNull() && str.IsNull())
     {
-        cout << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
+        gLog << all << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
+        return false;
+    }
+
+    gLog << err << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
     return false;
 }
@@ -258,5 +275,5 @@
 }
 
-bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed)
+bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd)
 {
     // rd [rad]
@@ -294,4 +311,5 @@
     str += MString::Format("%08.3f ", er.Az());
     str += armed ? "1 " : "0 ";
+    str += MString::Format("%d ", stargmd);           // Starguider mode: 0=none, 1=starguider, 2=starguider off
 
     return SendRep("DRIVE-REPORT", str.Data(), kFALSE);
@@ -336,25 +354,41 @@
 }
 
-bool MDriveCom::SendTPoint(UInt_t stat)
-{
-    //const MTime t(-1);
-
+bool MDriveCom::SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
+                           const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
+                           const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
+                           Int_t numstars, Int_t numcor, Float_t bright)
+{
     SetStatus(stat);
-    /*
-     TString str;
-     str += name;  // star name
-     str += " ";
-     str += nominalaz;
-     str += nominalel;
-     str += realaz;
-     str += realel;
-     str += nomra;
-     str += nomdec;
-     str += diffaz;
-     str += diffel;
-     str += mjd;
-     str += numleds;
-     str += artmag;
-     */
-    return SendRep("TPOINT-REPORT", "", kTRUE);
-}
+
+    TString str = type;
+    str += MString::Format(" %8.4f ", za0.Az());
+    str += MString::Format("%8.4f ", za0.Alt());
+    str += MString::Format("%8.4f ", fmod(za1.Az()+360, 360));
+    str += MString::Format("%8.4f ", 90-za1.Zd());
+    str += MString::Format("%8.4f ", xy.X());
+    str += MString::Format("%8.4f ", xy.Y());
+    str += MString::Format("%8.4f ", dzd);
+    str += MString::Format("%8.4f ", daz);
+    str += MString::Format("%12.6f ", t.GetMjd());
+    str += MString::Format("%f ", center.GetMag());
+    str += MString::Format("%f ", star.GetMag());
+    str += MString::Format("%f ", center.GetX());
+    str += MString::Format("%f ", center.GetY());
+    str += MString::Format("%f ", star.GetX());
+    str += MString::Format("%f ", star.GetY());
+    str += numleds;
+    str += " ";
+    str += numrings;
+    str += " ";
+    str += numstars;
+    str += " ";
+    str += numcor;
+    str += " ";
+    str += bright;
+    str += " ";
+    str += mag;
+    str += " ";
+    str += name;
+
+    return SendRep("TPOINT-REPORT", str, kTRUE);
+}
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 9435)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 9439)
@@ -7,8 +7,12 @@
 
 class RaDec;
+class AltAz;
 class ZdAz;
 class MsgQueue;
 class Ring;
+class Led;
 class MLog;
+class MTime;
+class TVector2;
 
 class MDriveCom : public MCeCoCom
@@ -49,7 +53,11 @@
 
 
-    bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed);
+    bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd);
     bool SendStargReport(UInt_t stat, ZdAz miss, ZdAz nompos, Ring center, Int_t num, Int_t n, Double_t bright, Double_t mjd, Int_t numleds, Int_t numrings);
-    bool SendTPoint(UInt_t stat);
+//    bool SendTPoint(char type);
+    bool SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
+                    const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
+                    const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
+                    Int_t numstars=0, Int_t numcor=0, Float_t bright=0);
     bool SendStatus(const char *stat);
 };
Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 9435)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 9439)
@@ -43,10 +43,17 @@
 MTcpIpO::MTcpIpO(const char *addr, Int_t tx) : fPortTx(tx)
 {
-    gLog << inf2 << "- Open send socket to " << addr << ":" << tx << endl;
+    // was "inf2" but nobody knows what the prg is doing if it times out
+    gLog << all << "- Open send socket to " << addr << ":" << tx << endl;
+
     fTxSocket = new TSocket(addr, tx);
+    fTxSocket->SetOption(kNoBlock, 1);
+
+    MTcpIpO::RunThread();
 }
 
 MTcpIpO::~MTcpIpO()
 {
+    CancelThread();
+
     // Now delete all TCP/IP objects
     delete fTxSocket;
@@ -55,5 +62,5 @@
 MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx) : MTcpIpI(rx), MTcpIpO(addr, tx)
 {
-    RunThread();
+    MTcpIpI::RunThread();
 }
 
@@ -74,16 +81,15 @@
 }
 
+/*
 TString MTcpIpO::GetSocketAddress() const
 {
+    TLockGuard guard(const_cast<TMutex*>(&fMutex));
     return GetSocketAddress(*fTxSocket);
 }
-
+*/
 bool MTcpIpO::SendFrame(TSocket &tx, const char *msg, int len)
 {
     if (!tx.IsValid())
-    {
-        //gLog << warn << "WARNING - No transmission to " << GetSocketAddress(tx) << " possible." << endl;
         return false;
-    }
 
 #ifdef DEBUG
@@ -91,5 +97,14 @@
 #endif
 
-    const Int_t l = tx.SendRaw(msg, len);
+    const Int_t l = tx.SendRaw(msg, len, kDontBlock);
+
+    // Frame not sent, EWOULDBLOCK
+    if (l==-4)
+    {
+        gLog << err << "ERROR - Sending to " << GetSocketAddress(tx) << " would block." << endl;
+/*--->*/        tx.Close(); // ?????
+        return false;
+    }
+
     if (l<0)
     {
@@ -100,5 +115,5 @@
     if (l!=len)
     {
-        gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << GetSocketAddress(tx) << endl;
+        gLog << err << "ERROR - Could only sent " << l << " out of "  << len << " bytes to " << GetSocketAddress(tx) << endl;
         return false;
     }
@@ -109,5 +124,4 @@
 bool MTcpIpO::SendFrame(const char *addr, int port, const char *msg, int len)
 {
-    //    R__LOCKGUARD2(myMutex);
 #ifdef DEBUG
     cout << "Connecting to " << addr << ":" << port << endl;
@@ -117,49 +131,68 @@
     TSocket tx(addr, port);
     return SendFrame(tx, msg, len);
-/*
-    if (!tx.IsValid())
-    {
-        gLog << warn << "WARNING - No transmission to " << addr << ":" << port << " possible." << endl;
+}
+
+bool MTcpIpO::Send(const char *msg, Int_t len)
+{
+    const Int_t mtx = fMutex.TryLock();
+    if (mtx==13)
+        gLog << warn << "MTcpIpO::Send - mutex is already locked by this thread." << endl;
+
+    // If Mutex cannot be locked, i.e. we are currently reopening
+    // the send socket we cannot wait, because we would block the
+    // executing threrad.
+    if (mtx)
         return false;
-    }
-
-    gLog << dbg << "Sending to " << addr << ":" << port << endl;
-
-    const Int_t l = tx.SendRaw(msg, len, kDontBlock);
-    if (l<0)
-    {
-        gLog << err << "ERROR - Sending TCP/IP frame to " << addr << ":" << port << endl;
-        return false;
-    }
-
-    if (l!=len)
-    {
-        gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << addr << ":" << port << endl;
-        return false;
-    }
-
-#ifdef DEBUG
-    cout << "Tx: " << msg << flush;
-#endif
-
-    return true;
-    */
-}
-
-bool MTcpIpO::Send(const char *msg, Int_t len)
-{
-    if (!fTxSocket->IsValid())
-    {
-        const TInetAddress &a = fTxSocket->GetInetAddress();
-        if (!a.IsValid())
-            return false;
+
+    const bool rc = SendFrame(*fTxSocket, msg, len);
+
+    fMutex.UnLock();
+
+    return rc;
+}
+
+Int_t MTcpIpO::Thread()
+{
+    const TInetAddress &a = fTxSocket->GetInetAddress();
+    if (!a.IsValid())
+    {
+        gLog << err << "ERROR: Send socket address invalid." << endl;
+        return 0;
+    }
+
+    Int_t wait = 1000; /// Wait a ms
+
+    while (!IsThreadCanceled())
+    {
+        if (fTxSocket->IsValid())
+        {
+            MThread::Sleep(1000); // Wait a ms
+            wait = 1000;
+            continue;
+        }
+
+
 #ifdef DEBUG
         cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl;
 #endif
+
+        fMutex.Lock();
+
         delete fTxSocket;
+
         fTxSocket = new TSocket(a.GetHostAddress(), fPortTx);
-    }
-
-    return SendFrame(*fTxSocket, msg, len);
+        fTxSocket->SetOption(kNoBlock, 1);
+
+        fMutex.UnLock();
+
+        if (fTxSocket->IsValid())
+            continue;
+
+        MThread::Sleep(wait); // Wait a ms
+        if (wait<30000000) // 30s
+            wait *= 2;
+    }
+
+    return 1;
 }
 
@@ -174,5 +207,5 @@
     TString str;
 
-    while (!IsThreadCanceled())
+    while (!MTcpIpI::IsThreadCanceled())
     {
         char c;
Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 9435)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 9439)
@@ -2,9 +2,12 @@
 #define COSY_MTcpIpIO
 
-#ifndef MARS_MThread
-#include "MThread.h"
+#ifndef COSY_MsgQueue
+#include "msgqueue.h"
 #endif
 #ifndef MARS_MTime
 #include "MTime.h"
+#endif
+#ifndef ROOT_TMutex
+#include <TMutex.h>
 #endif
 
@@ -22,4 +25,6 @@
 
     Bool_t fConnectionEstablished;
+
+    MTime  fLast;
 
     Bool_t WaitForData(TSocket &sock);
@@ -39,10 +44,13 @@
 
 // A generalized class for sending over tcp/ip
-class MTcpIpO
+class MTcpIpO : public MThread
 {
 private:
     TSocket *fTxSocket;
+    TMutex   fMutex;
 
     Int_t fPortTx;
+
+    Int_t Thread();
 
 public:
@@ -54,6 +62,5 @@
     static bool    SendFrame(const char *addr, int port, const char *msg, int len);
 
-    TString GetSocketAddress() const;
-    bool    Send(const char *msg, int len);
+    bool Send(const char *msg, int len);
 };
 
