X-Git-Url: https://git.smho.de/gw/?a=blobdiff_plain;f=src%2Fmain.cpp;h=51fd6c092595231f9b132401db92063751aba7ce;hb=HEAD;hp=5595980e733d65d5f3ff82bde46bca3a802d6328;hpb=df91f0af6eb853d1fd572da66c7fee753a8ebd64;p=owTools.git diff --git a/src/main.cpp b/src/main.cpp old mode 100755 new mode 100644 index 5595980..51fd6c0 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,6 +44,8 @@ #ifdef WIN #include "Windows.h" #include "time.h" + + #endif #include @@ -57,6 +59,7 @@ #include #include + #ifdef LINUX #define RB "\e(0\x6a\e(B" // 188 Right Bottom corner @@ -102,19 +105,21 @@ #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(" -s \"id as 64bit Hex\" try to skip select menu with this id \n"); +printf(" example: -s \"5D02160084D9A220\" \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"); @@ -122,12 +127,18 @@ 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(" -t [text_file] append comma separadet to text-file\n"); +printf(" -j Use 0xCC 0x44 to send convert temperature to all DS18B20 and DS2438\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"); +printf(" -x write direct 1-Wire\n"); + @@ -178,6 +189,10 @@ int getArgi(std::string flag) { int database=0; mySensorDB *sdb=NULL; #endif +int textfile=0; +std::string textfilename; + + /* snum_t getArgsnum(std::string flag) { snum_t snum; @@ -215,9 +230,25 @@ void setLogMode() { } +int appendTextFile(std::string line) { + std::ofstream file; + file.open(textfilename, std::ios::out | std::ios::app); + if (file.fail()) { + owi->log->set(OWLOG_ERROR,"Can not open outputfile for text output"); + return -1; + } + + + file << line << std::endl; + file.close(); + return 0; +} + void continuous(std::vector *devices,int intervall,int headline,int searchrom) { int first=1; + char valbuf[100]; + std::string tfl; while (1) { if (searchrom) { owi->log->clear(); @@ -278,9 +309,23 @@ void continuous(std::vector *devices,int intervall,int headline,int s 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); + if (textfile) { + snprintf(valbuf,100,"%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + tfl=valbuf; + } + + for (owDevice* dev :*devices) { for(int k=0;kmaxrepeat;k++){ + if (owCC_44_Temp>0) { + std::vector data; + data.push_back(0xCC); + data.push_back(0x44); + owi->Reset(); + owi->Communicate(&data, 2, 0); + } dev->convertAll(); if (owi->log->last()log->clear(); @@ -302,6 +347,10 @@ void continuous(std::vector *devices,int intervall,int headline,int s else printf("%0.1f \t",dev->values[i]); fflush(stdout); + if (textfile) { + snprintf(valbuf,100,",%0.4f",dev->values[i]); + tfl.append(valbuf); + } } } } @@ -319,6 +368,9 @@ void continuous(std::vector *devices,int intervall,int headline,int s } printf("\n"); + if (textfile) { + appendTextFile(tfl); + } while (((int)time(NULL)) < (st + intervall)) { #ifdef LINUX sleep(1); @@ -374,8 +426,23 @@ void device_menu(owDevice* d) { } int selectDevice() { + std::string s; owi->Find(); - int i=0; + int i = 0; + char *end; + unsigned long long l = 0; + if ((s = getArg("s")) != "") { + l = strtoull(s.c_str(), &end, 16); + for (owDevice* dev : owi->devices) { + i++; + snum_t snum = dev->getNum(); + if (l == snum.num) return i; + + } + printf("Number not found -> Select one or exit!\n"); + } + + i=0; for (owDevice* dev :owi->devices) { i++; snum_t snum=dev->getNum(); @@ -462,7 +529,7 @@ int main(int argc, char *argv[]) { 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) { @@ -546,6 +613,10 @@ int main(int argc, char *argv[]) { interactive(owi); } else if (getArg("c")=="1") { + if (getArg("j")=="1") { + printf("Use 1-Wire command 0xCC 0x44 to send convert temperature to all DS18B20 and DS2438\n"); + owCC_44_Temp=1; + } int reload=(getArg("r")=="1"); owi->Find(); printf("\n"); @@ -574,7 +645,11 @@ int main(int argc, char *argv[]) { database=1; } #endif - + if ((s=getArg("t")) !="") { + textfile=1; + printf("Write values to textfile %s\n",s.c_str()); + textfilename=s; + } continuous(&(owi->devices),pause,1,reload); } else @@ -609,6 +684,42 @@ int main(int argc, char *argv[]) { cl.push_back(0x1F); dev->Communicate(&cl,4,0); } else + //------------------------------------------------------------------------------------------ + // --------------------- Direct Write + //------------------------------------------------------------------------------------------ + + if ((s=getArg("x"))!="") { + std::vector v; + int sp=0; + std::string ts=""; + for ( std::string::iterator it = s.begin() ; it < s.end(); it++ ,sp++) { + if (isxdigit(*it)) { + char c=(*it); + ts.append(1,c); + if (ts.length()>=2) { + v.push_back(strtoul(ts.c_str(),NULL, 16)); + ts=""; + } + } + } + if (ts.length()!=0) { + v.push_back(strtoul(ts.c_str(),NULL, 16)); + } + printf("Send "); + for (uint8_t val :v) printf("%02X ",val); + printf(" to selected Device\n"); + int sel=selectDevice(); + if (sel==0) exit(0); + sel-=1; + owDevice* dev=owi->devices[sel]; + dev->Communicate(&v,v.size(),0); + for (uint8_t val :v) printf("%02X ",val); + printf("\n"); + exit(0); + + + } + //------------------------------------------------------------------------------------------ // --------------------- Memory functions //------------------------------------------------------------------------------------------ @@ -655,14 +766,14 @@ int main(int argc, char *argv[]) { //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); + int r=dev->readMemory(pnr,0,dev->getPageSize(),&mem); for(int i=0;iwriteMemory(pnr,0,8,&mem); - r=dev->readMemory(pnr,0,8,&mem); + int r=dev->writeMemory(pnr,0,dev->getPageSize(),&mem); + r=dev->readMemory(pnr,0,dev->getPageSize(),&mem); for(int i=0;idevices[sel]->getNum(); + if ((getArg("g"))=="1") { printf("get ID from Server\n"); char s[255]; @@ -700,7 +812,7 @@ int main(int argc, char *argv[]) { if (c=='x') br=1; } else { if ((c==',')|(c=='}')) { - isnum.byte[i]=(uint8_t)strtol(s, NULL, 16); + isnum.byte[i]=(uint8_t)strtoull(s, NULL, 16); //printf("%x\n",strtol(s, NULL, 16)); i++; br=0; @@ -719,7 +831,22 @@ 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=strtoull(s.c_str(),NULL,16); + if ((snum.num&0xFF)!=(l&0xFF)) { + printf("This command change family-fode of Device 0x%02X->0x%02X\n",(int)(snum.num&0xFF),(int)(l&0xFF)); + int resetid=questionYesNo("Are you sure?"); + if (resetid==0) { + 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); + } + }