X-Git-Url: http://git.smho.de/gw/?p=owTools.git;a=blobdiff_plain;f=src%2Fmain.cpp;h=2e7e6556350eaa43ab8c46a59e609c7dcf675cfe;hp=b6296170925effd40ba15cb04bc442a272c0d19f;hb=8ea05adcfa81d2ca574e6dbe37046607ea671a0e;hpb=c7cb52d06aae49ba200634f348af90414d78cfac diff --git a/src/main.cpp b/src/main.cpp index b629617..2e7e655 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,26 +30,34 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include "owTools.h" +#ifdef LINUX #include #include "mySensorDB.h" -#include -#include -#include #include -#include /* For O_RDWR */ #include #include #include +#endif + +#ifdef WIN +#include "Windows.h" +#include "time.h" +#endif + +#include +#include +#include +#include /* For O_RDWR */ #include -#include "owTools.h" #include "hexfile.h" #include #include #include #include - +#ifdef LINUX #define RB "\e(0\x6a\e(B" // 188 Right Bottom corner #define RT "\e(0\x6b\e(B" // 187 Right Top corner @@ -63,22 +71,50 @@ #define TC "\e(0\x77\e(B" // 203 Top Cross #define VL "\e(0\x78\e(B" // 186 Vertical Line #define SP " " // space string - +#define BLUE_B "\033[1;34m" +#define COLOR_E "\033[0m" +#define C2_B "\033[0;36m" +#define C3_B "\033[3;34m" +#define C4_B "\033[1;33m" +#define C5_B "\033[4;33m" +#endif +#ifdef WIN +#define RB " "// 188 Right Bottom corner +#define RT " "// 187 Right Top corner +#define LT " "// 201 Left Top cornet +#define LB " "// 200 Left Bottom corner +#define MC " " // 206 Midle Cross +#define HL " "// 205 Horizontal Line +#define LC " "// 204 Left Cross +#define RC " "// 185 Right Cross +#define BC " "// 202 Bottom Cross +#define TC " "// 203 Top Cross +#define VL " "// 186 Vertical Line +#define SP " " // space string +#define BLUE_B "" +#define C2_B "" +#define C3_B "" +#define C4_B "" +#define COLOR_E "" +#define C5_B "" + + +#endif void printhelp() { -printf("owTools - Programm for reading und controlling 1-Wire Devices from www.tm3d.de\n\n"); +printf("owTools - Program for reading and controlling 1-Wire Devices from www.tm3d.de\n\n"); printf("run: owTools -a [COMn|USBn|GPIOn] [options]\n\n"); printf(" COMn -> Adapter DS9097 and compatible (e.g. LinkUSB)\n"); printf(" n=1 -> Windows COM1 -> Linux /dev/ttyS1\n"); printf(" USBn -> Adapter DS2490/DS9490 \n"); printf(" n=1 -> first USB-Adapter \n"); -printf(" GPIOn -> port of Raspberry PI (port Name not Pin number)\n\n"); -printf("options:\n"); +printf(" GPIOn -> port of RaspberrPI (port Name not Pin number)\n\n"); +printf("Options:\n"); printf(" -i interactive mode\n"); printf(" select a device and get information about it\n"); printf(" -c read and print all continuous\n"); printf(" -p time in sec between readings\n"); -printf(" -d [config file] put all Data in mysql Database descripted in Config file\n"); +printf(" -d [config file] put all Data in mysql Database descripted in config file\n"); printf(" Config file: \n"); printf(" [server]\n"); printf(" [port] -> 3306\n"); @@ -86,11 +122,15 @@ printf(" [user] \n"); printf(" [password]\n"); printf(" [database]\n"); printf(" [prefix] -> a prefix of all tables \n\n"); -printf(" -r Search everytime for new Devices\n"); -printf(" -f hexfile Flash new\n"); -printf(" -n change id \n"); +printf(" -r Search every time for new Devices\n"); +printf(" -f [hexfile] flash new\n"); +printf(" -n [\"id as 64bit Hex\"] change id \n"); +printf(" example: -n \"5D02160084D9A220\" \n"); +printf(" no argument increments id by 256 (keep family code) \n"); printf(" -g get from server\n"); -printf(" -w [1|2] show Warnings (1) or all Infos (2)\n"); +printf(" -w [1|2] show Warnings (1) or all information (2)\n"); +printf(" -m memory functions\n"); + @@ -137,17 +177,17 @@ int getArgi(std::string flag) { return atoi(getArg(flag).c_str()); } - +#ifdef LINUX int database=0; mySensorDB *sdb=NULL; - - +#endif +/* snum_t getArgsnum(std::string flag) { snum_t snum; snum.num=0; return snum; } - +*/ int findCPU(std::string cpu) { std::ifstream fileInput; std::string line; @@ -192,8 +232,8 @@ void continuous(std::vector *devices,int intervall,int headline,int s for (owDevice* dev :*devices) { if (dev->configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) - printf("\033[1;34m%02X.%02X%02X\033[0m\t",dev->getNum().byte[7],dev->getNum().byte[1],dev->getNum().byte[0]); + if (dev->config->getPropertyID((uint8_t)i)!=0) + printf(BLUE_B "%02X.%02X%02X" COLOR_E "\t",dev->getNum().byte[7],dev->getNum().byte[1],dev->getNum().byte[0]); } } } @@ -201,8 +241,8 @@ void continuous(std::vector *devices,int intervall,int headline,int s for (owDevice* dev :*devices) { if (dev->configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) - printf("\033[0;36m%s\033[0m\t",dev->config->getQuantity(i).substr(0,7).c_str()); + if (dev->config->getPropertyID((uint8_t)i)!=0) + printf(C2_B "%s" COLOR_E "\t",dev->config->getQuantity((uint8_t)i).substr(0,7).c_str()); } } } @@ -210,10 +250,10 @@ void continuous(std::vector *devices,int intervall,int headline,int s for (owDevice* dev :*devices) { if (dev->configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) { - size_t l=utf8_length(dev->config->getUnit(i)); + if (dev->config->getPropertyID((uint8_t)i)!=0) { + size_t l=utf8_length(dev->config->getUnit((uint8_t)i)); std::string ls=" "; - printf("\033[3;34m[%s]%s\033[0m\t",dev->config->getUnit(i).c_str(),ls.substr(0,5-l).c_str()); + printf(C3_B "[%s]%s" COLOR_E "\t",dev->config->getUnit((uint8_t)i).c_str(),ls.substr(0,5-l).c_str()); } } } @@ -221,19 +261,26 @@ void continuous(std::vector *devices,int intervall,int headline,int s } printf("\n"); - +#ifdef LINUX if (database) { //owi->log->setLogLevel(0); for (owDevice* dev : *devices) { sdb->createDeviceTable(dev); } } +#endif } } time_t t=time(NULL); int st=(int)t; +#ifdef LINUX struct tm tm = *localtime(&t); +#endif +#ifdef WIN + struct tm tm; + localtime_s(&tm,&t); +#endif printf("%d-%02d-%02d %02d:%02d:%02d\t", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); for (owDevice* dev :*devices) { for(int k=0;kmaxrepeat;k++){ @@ -244,7 +291,7 @@ void continuous(std::vector *devices,int intervall,int headline,int s if (owi->log->last()configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) { + if (dev->config->getPropertyID((int8_t)i)!=0) { if (dev->values[i]<=-10) printf("%0.2f \t",dev->values[i]); else if (dev->values[i]<0) @@ -261,9 +308,11 @@ void continuous(std::vector *devices,int intervall,int headline,int s } } } +#ifdef LINUX if (database) { sdb->insertValues(dev); } +#endif } else { owi->log->setLogLevel(OWLOG_INFO); owi->log->set(OWLOG_ERROR,"Too many errors, mybee conection is dead."); @@ -273,7 +322,14 @@ void continuous(std::vector *devices,int intervall,int headline,int s } printf("\n"); - while (((int)time(NULL))<(st+intervall)) sleep(1); + while (((int)time(NULL)) < (st + intervall)) { +#ifdef LINUX + sleep(1); +#endif +#ifdef WIN + Sleep(1000); +#endif + } } } @@ -281,7 +337,7 @@ void device_menu(owDevice* d) { for(int i=0;i<70;i++) printf(HL);printf("\n"); printf("Selected Device: "); snum_t snum=d->getNum(); - printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num); + printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num); printf(" \n"); printf("Chip Info: "); std::vector info=d->getFamilyInfo(); @@ -290,14 +346,14 @@ void device_menu(owDevice* d) { printf("Values info: "); int tm3d=d->readConfig(); for (size_t i=0;i<4;i++) { - printf("%s in %s",d->config->getQuantity(i).c_str(),d->config->getUnit(i).c_str()); + printf("%s in %s",d->config->getQuantity((uint8_t)i).c_str(),d->config->getUnit((uint8_t)i).c_str()); if (i<4) printf("; "); } if (tm3d) printf(" (tm3d.de)"); else printf(" (default)"); printf("\n"); d->convertAll(); for (size_t i=0;ivalues.size();i++) { - printf("\033[1;33m%0.4f %s\033[0m ",d->values[i],d->config->getUnit(i).c_str()); + printf(C4_B "%0.4f %s" COLOR_E,d->values[i],d->config->getUnit((uint8_t)i).c_str()); } printf("\n"); for(int i=0;i<70;i++) printf(HL);printf("\n"); @@ -327,7 +383,7 @@ int selectDevice() { i++; snum_t snum=dev->getNum(); printf("%i) ",i); - printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num); + printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num); printf(" "); for (int j=0;j<4;j++) printf("%s ",dev->config->getQuantity(j).c_str()); @@ -341,7 +397,7 @@ int selectDevice() { dnr=atoi(inp.c_str()); if (dnr<=0) return 0; if (dnr<=i) break; - printf("\033[4;33mSelect a number between 0 and %i\033[0m\n",i); + printf(C5_B "Select a number between 0 and %i" COLOR_E "\n",i); } return dnr; @@ -405,11 +461,11 @@ int main(int argc, char *argv[]) { std::transform(adapter.begin(), adapter.end(),adapter.begin(), ::toupper); std::string s; - +#ifdef LINUX if(adapter.find("COM")!=std::string::npos) { owi=new owCOMInterface(); int port=atoi(adapter.substr(adapter.find("COM")+3).c_str()); - printf("Open /dev/ttyS%i\n",port); + printf("Open /dev/ttyUSB%i\n",port); owi->InitAdapter(port); } else if(adapter.find("USB")!=std::string::npos) { @@ -437,13 +493,57 @@ int main(int argc, char *argv[]) { } else { printf("\033[1;31mGPIO works with Raspberry PI only \033[0m\n"); } - } - if (owi==NULL) { - return 0; + } else + if(adapter.find("W1")!=std::string::npos) { + owi=new owW1Interface(); + if (owi->InitAdapter(0)) { + printf("Use W1-Kernel 1-Wire functions on /sys/bus/w1\n"); + } else printf("No W1-Kernel module found!!\n"); + } else +#endif + if (adapter.find("ARDUINO") != std::string::npos) { + owi = new owARDUINOInterface(); + int port = atoi(adapter.substr(adapter.find("ARDUINO") + 7).c_str()); + printf("Open /dev/ttyS%i\n", port); + owi->InitAdapter(port); } +#ifdef WIN + else + if (adapter.find("USB") != std::string::npos) { + printf("USB \n"); + owi = new owTMEXWIN(); + int port = atoi(adapter.substr(adapter.find("USB") + 3).c_str()); + printf("Open the %i. USB Adapter\n", port); + int err; + char cs[20]; + sprintf_s(cs, 20, "{%i,6}", port); + if ((err = owi->InitAdapter(cs))==0) { + printf("ERROR Init USB Adapter\n"); + exit(0); + } + } + else + if (adapter.find("COM") != std::string::npos) { + owi = new owTMEXWIN(); + int port = atoi(adapter.substr(adapter.find("COM") + 3).c_str()); + printf("Open Adapter on COM%i\n", port); + int err; + char cs[20]; + sprintf_s(cs, 20, "{%i,5}", port); + if ((err = owi->InitAdapter(cs))==0) { + printf("ERROR Init Serial Adapter\n"); + exit(0); + } + } +#endif + if (owi == NULL) { + printf("No 1-Wiremaster found\n"); + return 0; + } setLogMode(); - + + if (getArg("i")=="1") { interactive(owi); @@ -458,6 +558,7 @@ int main(int argc, char *argv[]) { if ((s=getArg("p")) !="") { pause=atoi(s.c_str()); } else pause=30; +#ifdef LINUX if ((s=getArg("d")) !="") { reload=1; printf("Use Database\n"); @@ -474,7 +575,8 @@ int main(int argc, char *argv[]) { exit(1); } database=1; - } + } +#endif continuous(&(owi->devices),pause,1,reload); @@ -491,7 +593,7 @@ int main(int argc, char *argv[]) { owi->Find(); for (owDevice* dev :owi->devices) { snum_t snum=dev->getNum(); - printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num); + printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num); printf("\n"); } @@ -510,7 +612,65 @@ int main(int argc, char *argv[]) { cl.push_back(0x1F); dev->Communicate(&cl,4,0); } else + //------------------------------------------------------------------------------------------ + // --------------------- Memory functions + //------------------------------------------------------------------------------------------ + if ((s=getArg("m"))!="") { + int pnr=0; + std::vector mem; + if(s.find("read")!=std::string::npos) { + size_t cla=s.find_first_of("(",0); + size_t page=s.find_first_of("p=",cla); + size_t epage=s.find_first_of(";",page); + if (epage==std::string::npos) epage=s.find_first_of(")",page); + std::string pn=s.substr(page+2,epage-page-2); + printf("%s Read Memory Page %s of Device:\n",s.c_str(),pn.c_str()); + pnr=atoi(pn.c_str()); + + } else + if(s.find("write")!=std::string::npos) { + size_t cla=s.find("(",0); + size_t page=s.find("p=",cla); + size_t epage=s.find(";",page); + if (epage==std::string::npos) epage=s.find_first_of(")",page); + std::string pn=s.substr(page+2,epage-page-2); + size_t data=s.find("d=",cla); + size_t edata=s.find(";",data); + if (edata==std::string::npos) edata=s.find(")",data); + std::string pd=s.substr(data+2,edata-data-2); + std::istringstream pdss(pd); + //std::cout << pd <>std::hex>>v; + mem.push_back(v); + } + printf("Write "); + for(uint8_t v :mem) printf("%02X ",v); + printf("to Memory Page %s of Device:\n",pn.c_str()); + pnr=atoi(pn.c_str()); + + } else exit(1); + int sel=selectDevice(); + if (sel==0) exit(0); + sel-=1; + //snum_t snum=owi->devices[sel]->getNum(); + owDevice* dev=owi->devices[sel]; + if(s.find("read")!=std::string::npos) { + int r=dev->readMemory(pnr,0,8,&mem); + for(int i=0;iwriteMemory(pnr,0,8,&mem); + r=dev->readMemory(pnr,0,8,&mem); + for(int i=0;idevices[sel]->getNum(); + if ((getArg("g"))=="1") { printf("get ID from Server\n"); char s[255]; +#ifdef LINUX sprintf(s,"wget -q http://www.tm3d.de/shop99/owid.php?fam=%02X -O id.txt",snum.byte[0]); +#endif +#ifdef WIN + sprintf_s(s, "wget -q http://www.tm3d.de/shop99/owid.php?fam=%02X -O id.txt", snum.byte[0]); +#endif int err; if ((err=system(s))==0) { printf("OK!\n"); @@ -538,7 +704,7 @@ int main(int argc, char *argv[]) { if (c=='x') br=1; } else { if ((c==',')|(c=='}')) { - isnum.byte[i]=strtol(s, NULL, 16); + isnum.byte[i]=(uint8_t)strtol(s, NULL, 16); //printf("%x\n",strtol(s, NULL, 16)); i++; br=0; @@ -557,12 +723,23 @@ int main(int argc, char *argv[]) { snum.num=isnum.num; } else (printf("ERROR %i\n",err)); } else { - snum.num+=256; + if (s=="1") { + snum.num+=256; + } else { + unsigned long long l=strtol(s.c_str(),NULL,16); + if ((snum.num&0xFF)!=(l&0xFF)) { + printf("ERROR: Family of Device 0x%02X->0x%02X can not be changed\n",(int)(snum.num&0xFF),(int)(l&0xFF)); + exit(1); + } + snum.num=l; + //printf("->%016llX\n",(unsigned long long)l); + } + } printf("New ID: "); - printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num); + printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num); printf("\n"); @@ -572,7 +749,7 @@ int main(int argc, char *argv[]) { owi->Find(); for (owDevice* dev :owi->devices) { snum_t snum=dev->getNum(); - printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num); + printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num); printf("\n"); } @@ -583,7 +760,7 @@ int main(int argc, char *argv[]) { for (owDevice* dev :owi->devices) { printf("_______________________________________________________________________\n"); snum_t snum=dev->getNum(); - printf("\033[1;34m%016llX\033[0m ",(unsigned long long)snum.num); + printf(BLUE_B "%016llX " COLOR_E,(unsigned long long)snum.num); switch (dev->configstate) { case OWDCS_16:printf("old Configcode from www.tm3d.de");break; case OWDCS_24:printf("Configcode from www.tm3d.de");break; @@ -611,7 +788,12 @@ int main(int argc, char *argv[]) { colm.push_back(""); } else { char hs[50]; +#ifdef LINUX sprintf(hs,"%0.3f",dev->values[i]); +#endif +#ifdef WIN + sprintf_s(hs, "%0.3f", dev->values[i]); +#endif colm.push_back(hs); } if (max