/* Combined CONIO and socket select code combined to use select as timer doug rice, 2010 Patched to poll GDS-1062 scope Based on code from: http://www.codersource.net/winsock_tutorial_server_select_model.html needs to link to: ../../lib/libws2_32.a operation: Send command, wait and read from file. Using the COM ports it's not possible to poll until buffer has enough bytes. You have to take what is available and buffer them until you have enough. The file commands do not block, and do not have a time out. If you do not wait before reading, the bytes may not have been received. There is no select() for COM ports, so use the Sockets library. */ // =========================================================================== // Sec 1.0 Includes // =========================================================================== #include #include #include // =========================================================================== // Sec 2.0 # Defines // =========================================================================== // socket stuff #define PORT 1150 #define BUFFERSIZE 8192 // RS232 stuff #define COM_PORT 2 // Modify as required. #define SCOPE_ACC_LEN 8014 // found by itteration // =========================================================================== // Sec 3.0 # Types // =========================================================================== /* Scope data forma # A B C D E F A: Data size digit B: Data size C: Time interval D: Channel indicator E: Resserved Data F: WaveForm Data */ // define the structure using appropriate types. It must be packed. struct scope_S { char T; /* # */ char A; char B[4]; float C; /* this needs byte swapping ABCD => DCBA */ char D; char E[3]; short F[4000]; /* this needs byte swapping AB => BA */ } __attribute__((__packed__)); // define the structure just using chars struct scope2_S { char T; char A; char B[4]; char C[4]; /* this needs byte swapping ABCD => BADC */ char D; char E[3]; char F[8000]; /* this needs byte swapping AB => BA */ } __attribute__((__packed__)); // use a union to overlay it. union scope_U { char buffer[SCOPE_ACC_LEN]; struct scope_S scope_s; struct scope2_S scope2_s; } ; // =========================================================================== // Sec 3.0 # Vars // =========================================================================== union scope_U scope_u1, scope_u2 ; union scope3_U { char buffer[8]; float nf3[2]; double nf3d; } scope3_u __attribute__((__packed__)) ; char opStr[8196]; int buffIndex; int runFlag ; char * fileNameP = "C:\\Dev-Cpp\\dev\\scope\\op\\test.csv"; /* type Tmem = ( h, dataSizeDigits, timeInterval , dataWords ); TacqmemBytesT = array[0..MAX_BUFF] of byte ; TacqmemCharsT = array[0..MAX_BUFF] of Char ; TacqmemT = packed record h : char; { # } dataSizeDigits : array[0..4] of char; { 48008 } {timeIntervalBytes : array[0..3] of char; {single;} {34 56 bf 94 => 94 bf 56 34 } // before use byted need to be reversed. {34 56 bf 94 => 94 bf 56 34 } timeInterval : single; {single;} {34 56 bf 94 => 94 bf 56 34 } ChannelIndicator : byte; reserved : array[0..2] of byte ; dataWords : array[0..7999] of SmallInt ; end; TacqmemUnionT = packed record lastCmd2 : string[30]; loadedFlag : boolean; bytesOffset : integer; // pointer strP : ^string; raw : boolean ; // indicates if bytes have been reversed. case t1 : integer of 1 : ( v1: TacqmemBytesT ) ; 2 : ( v2: TacqmemT ); 3 : ( v3: TacqmemCharsT ); end; */ typedef struct _MYSOCKET_INFORMATION { CHAR Buffer[BUFFERSIZE]; WSABUF DataBuf; SOCKET Socket; OVERLAPPED Overlapped; DWORD SendBytes; DWORD RecvBytes; } SOCKET_INFORMATION, * LPSOCKET_INFORMATION; DWORD TotalSockets = 0; LPSOCKET_INFORMATION SocketList[FD_SETSIZE]; //const struct timeval timeoutSelect = { 0,100000 }; const struct timeval timeoutSelect = { 0,400000 }; //handle for serial port I/O HANDLE hCom; HANDLE hOpFile; // iic state variables int iic_state = 0; int iic_nextstate = 0; int iic_sw; int iic_ad1,iic_ad2; char line[8192]; const time_t timer = time(NULL); // printf("ctime is %s\n", ctime(&timer)); char * padding1 = " "; char * padding2 = ""; char * paddingP = padding2 ; float offset1=0.0, scale1=0.0, offset2=0.0, scale2=0.0; // =========================================================================== // Sec 4.0 Function Prototypes // =========================================================================== BOOL CreateSocketInformation(SOCKET s); void FreeSocketInformation(DWORD Index); // delay_routines void delay_ms(clock_t millis); // =========================================================================== // Sec 4.0 Function Prototypes // =========================================================================== int scope_idn( int io_port, int setting ); int scope_mem_list( char * fileNameP ); void scope_timeslice( int io_port ); // RS232 Routines HANDLE rs_initialise (int io_port, const long int BaudRate, const char parity, const char data); void rs_flush(const int io_port); void rs_terminate(const int io_port); char rs_getch(const int io_port); int rs_getline(const int io_port, char line[], clock_t timeout); void rs_putch(const int io_port, const int txchar); void rs_putstr(const int io_port, const char *string); //int rs_getbuffer(const int io_port, clock_t timeout); int rs_getbuffer(const int io_port, scope_U * buff, clock_t timeout); // =========================================================================== // Sec 4.0 Functions - misc // =========================================================================== int fileExample(){ HANDLE hFile; DWORD wmWritten; char strVal[1024]; hFile = CreateFile("C:\\test.txt",GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); ReadFile(hFile,strVal,1024,&wmWritten,NULL); CloseHandle(hFile); } int fileWriteExample( char * txtPtr , char * fileNameP ){ HANDLE hFile; DWORD wmWritten; int lenTxt = strlen( txtPtr ); // char strVal[1024]; // hFile = CreateFile("C:\\Dev-Cpp\\dev\\scope\\op\\test.txt",GENERIC_READ|GENERIC_WRITE|FILE_APPEND_DATA, hFile = CreateFile( fileNameP ,FILE_APPEND_DATA, FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if ( ! hFile ) { printf( " cannot open %s \n", fileNameP ); return 1; } WriteFile(hFile,txtPtr,lenTxt,&wmWritten,NULL); CloseHandle(hFile); return ( int ) wmWritten; } // =========================================================================== // Sec 5.0 # Main Code // =========================================================================== int main1(void); int main(int argc, char *argv[]); //int main(void) //int main(void) int main(int argc, char *argv[]) { int io_port = COM_PORT; int cnt ; int temp ; // time_t* timer; printf( "\n=== scope comport file ===\n" ); for ( cnt = 0 ; cnt < argc ; cnt ++ ) { printf( " %d: %s\n", cnt , argv[cnt] ); } if ( cnt > 1 ) { io_port = atoi( argv[1] ); } if ( cnt > 2 ) { fileNameP = argv[2]; } //time_t time(time_t* timer) //Get the current time (number of seconds from the epoch) from the system clock. Stores that value in timer. If timer is null, the value is not stored, but it is still returned by the function. //timer = time(time_t* timer); //const time_t timer = time(NULL); printf("ctime is %s\n", ctime(&timer)); printf( "using comport: %d , opfile = %s @ %s \n", io_port , fileNameP , ctime(&timer)) ; printf( "\n=============\n" ); main1( ); // routine rs_terminate(io_port); } int main1(void) { SOCKET ListenSocket; SOCKET AcceptSocket; SOCKADDR_IN InternetAddr; WSADATA wsaData; INT Ret; FD_SET Writer; FD_SET Reader; DWORD i; DWORD Total; ULONG NonBlock; DWORD Flags; DWORD SendBytes; DWORD RecvBytes; int io_port = COM_PORT; clock_t timeout; opStr[8196-1]= '\0'; opStr[1]= '\0'; /* if(!rs_initialise(io_port ,19200, '8', 'N'))*/ if(!rs_initialise(io_port ,19200, 'N', '8')) { printf("Opening Port Failed"); delay_ms(5000); exit(1); } // scope_idn( io_port, 0 ); timeout = clock() + 60000; // every 60 secs // =================================================== if ((Ret = WSAStartup(MAKEWORD(2,0),&wsaData)) != 0) { printf("WSAStartup() failed with error %d\n", Ret); WSACleanup(); return 0; } // Create a socket. if ((ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) { printf("WSASocket() failed with error %d\n", WSAGetLastError()); return 0 ; } InternetAddr.sin_family = AF_INET; InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY); InternetAddr.sin_port = htons(PORT); if (bind(ListenSocket, (SOCKADDR *) &InternetAddr, sizeof(InternetAddr)) == SOCKET_ERROR) { printf("Binding failed with error %d\n", WSAGetLastError()); return 0 ; } if (listen(ListenSocket, 5)) { printf("listen failed with error %d\n", WSAGetLastError()); return 0 ; } // Change the socket mode on the listening socket from blocking to non-block NonBlock = 1; if (ioctlsocket(ListenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR) { printf("ioctlsocket() failed \n"); return 0 ; } runFlag = TRUE; while(runFlag) { // Initialize the Read and Write socket set. FD_ZERO(&Reader); FD_ZERO(&Writer); // Check for connection attempts. FD_SET(ListenSocket, &Reader); // FD_SET(STDIN_FILENO, &Reader); // Set Read and Write notification for each socket based on the // current state the buffer. for (i = 0; i < TotalSockets; i++) if (SocketList[i]->RecvBytes > SocketList[i]->SendBytes) FD_SET(SocketList[i]->Socket, &Writer); else FD_SET(SocketList[i]->Socket, &Reader); if ((Total = select(0, &Reader, &Writer, NULL, &timeoutSelect )) == SOCKET_ERROR) { printf("select function returned with error %d\n", WSAGetLastError()); return 0; } scope_timeslice( io_port); // Check for arriving connections on the listening socket. if (FD_ISSET(ListenSocket, &Reader)) { Total--; if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) != INVALID_SOCKET) { // Set the accepted socket to non-blocking mode so the server will // not get caught in a blocked condition on WSASends NonBlock = 1; if (ioctlsocket(AcceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR) { printf("ioctlsocket() failed with error %d\n", WSAGetLastError()); return 0 ; } if (CreateSocketInformation(AcceptSocket) == FALSE) return 0 ; } else { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("accept() failed with error %d\n", WSAGetLastError()); return 0 ; } } } // Check each socket for Read and Write notification for Total number of sockets for (i = 0; Total > 0 && i < TotalSockets; i++) { printf( " socket[ %d ] : %X \n ", i, SocketList[i]->Socket ); LPSOCKET_INFORMATION SocketInfo = SocketList[i]; // If the Reader is marked for this socket then this means data // is available to be read on the socket. //printf( "-- %X %X \n", SocketInfo->Socket, Reader ); if (FD_ISSET(SocketInfo->Socket, &Reader)) { Total--; SocketInfo->DataBuf.buf = SocketInfo->Buffer; SocketInfo->DataBuf.len = BUFFERSIZE; Flags = 0; if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes, &Flags, NULL, NULL) == SOCKET_ERROR) { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("Receive failed with error\n"); FreeSocketInformation(i); } continue; } else { SocketInfo->RecvBytes = RecvBytes; printf("%s\n",SocketInfo->DataBuf.buf); // If zero bytes are received, this indicates connection is closed. if (RecvBytes == 0) { FreeSocketInformation(i); continue; } } } // If the Writer is marked on this socket then this means the internal // data buffers are available for more data. if (FD_ISSET(SocketInfo->Socket, &Writer)) { Total--; SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->SendBytes; SocketInfo->DataBuf.len = SocketInfo->RecvBytes - SocketInfo->SendBytes; if (WSASend(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0, NULL, NULL) == SOCKET_ERROR) { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("Send failed with error\n"); FreeSocketInformation(i); } continue; } else { SocketInfo->SendBytes += SendBytes; if (SocketInfo->SendBytes == SocketInfo->RecvBytes) { SocketInfo->SendBytes = 0; SocketInfo->RecvBytes = 0; } } } } } } // =========================================================================== // Sec 6.0 Socket functions // =========================================================================== BOOL CreateSocketInformation(SOCKET s) { LPSOCKET_INFORMATION SI; printf("Accepted socket: %d \n" , s); if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR, sizeof(SOCKET_INFORMATION))) == NULL) { printf("GlobalAlloc() failed\n"); return FALSE; } // Prepare SocketInfo structure for use. SI->Socket = s; SI->SendBytes = 0; SI->RecvBytes = 0; SocketList[TotalSockets] = SI; TotalSockets++; printf("Accepted socket: %d totalSockets: %d \n" , s, TotalSockets ); return(TRUE); } void FreeSocketInformation(DWORD Index) { LPSOCKET_INFORMATION SI = SocketList[Index]; DWORD i; closesocket(SI->Socket); printf("Closing socket %d\n", SI->Socket ); GlobalFree(SI); // Remove from the socket array for (i = Index; i < TotalSockets; i++) { SocketList[i] = SocketList[i + 1]; } TotalSockets--; } // =========================================================================== // Sec 7.0 Com port function // =========================================================================== HANDLE rs_initialise (int io_port, const long int BaudRate, const char parity, const char data) { BOOL bPortReady; DCB dcb; COMMTIMEOUTS CommTimeouts; char ComPortName[5]="COM "; ComPortName[3]='0'+ io_port; hCom = CreateFile(ComPortName, GENERIC_READ | GENERIC_WRITE, 0, // exclusive access NULL, // no security OPEN_EXISTING, 0, // no overlapped I/O NULL); // null template if ((int)hCom <= 0) { printf("serial port COM%d connect fail %s error %d\n\r", io_port, ComPortName, GetLastError()); return 0; } //else printf(" serial port COM%d connect OK \n\r", io_port); bPortReady = SetupComm(hCom, 128, 128 ); // set buffer sizes // bPortReady = SetupComm(hCom, 8196, 8196); // set buffer sizes if (!bPortReady ) { printf("serial port COM%d SetupComm fail %d\n\r", io_port, GetLastError()); return 0; } //else printf(" serial port COM%d connect OK \n\r", io_port); bPortReady = GetCommState(hCom, &dcb); if (!bPortReady ) { printf("serial port COM%d GetCommState fail %d\n\r", io_port, GetLastError()); return 0; } // else printf(" serial port COM%d connect OK \n\r", io_port); dcb.BaudRate = BaudRate; if( data == '7') dcb.ByteSize = 7; else dcb.ByteSize = 8; if( parity == 'E') dcb.Parity = EVENPARITY; if( parity == 'O') dcb.Parity = ODDPARITY; else dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fAbortOnError = TRUE; // set XON/XOFF dcb.fOutX = FALSE; // XON/XOFF off for transmit dcb.fInX = FALSE; // XON/XOFF off for receive // set RTSCTS dcb.fOutxCtsFlow = FALSE; // turn off CTS flow control dcb.fRtsControl = FALSE; // RTS_CONTROL_HANDSHAKE; // // set DSRDTR dcb.fOutxDsrFlow = FALSE; // turn off DSR flow control //dcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR handshake dcb.fDtrControl = DTR_CONTROL_DISABLE; // // dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; // bPortReady = SetCommState(hCom, &dcb); if (!bPortReady ) { printf("serial port COM%d SetCommState fail %d\n\r", io_port, GetLastError()); return 0; } // Communication timeouts //COMMTIMEOUTS CommTimeouts; bPortReady = GetCommTimeouts (hCom, &CommTimeouts); CommTimeouts.ReadIntervalTimeout = 5 ; CommTimeouts.ReadTotalTimeoutConstant = 5 ; CommTimeouts.ReadTotalTimeoutMultiplier = 1 ; CommTimeouts.WriteTotalTimeoutConstant = 2 ; CommTimeouts.WriteTotalTimeoutMultiplier = 1 ; bPortReady = SetCommTimeouts (hCom, &CommTimeouts); if (!bPortReady ) { printf(" serial port COM%d SetCommTimeouts fail %d\n\r", io_port, GetLastError()); return 0; } else { printf(" serial port COM%d connect OK \n\r", io_port); } return (hCom); } void rs_terminate(const int io_port) { CloseHandle(hCom); } char rs_getch(const int io_port) { char rxchar; BOOL bReadRC; static DWORD iBytesRead; /* ReadFile is non blocking and returns available characters */ bReadRC = ReadFile(hCom, &rxchar, 1, &iBytesRead, NULL); if (iBytesRead) { return rxchar; } else { return 0; // return 0 if no character read } } /* * we need a function to load more bytes into the buffer. * * */ int rs_getbuffer(const int io_port, scope_U * buff, clock_t timeout) { int cnt ; char rxchar,temp; BOOL bReadRC; static DWORD iBytesRead; BOOL finished; /* * * We need to loop until timeout or all characters received. * However, just in case run a time out. */ int buffIndex = 0; clock_t endtime; endtime = timeout + clock(); finished = false; while ( ! finished ) { bReadRC = ReadFile(hCom, &buff->buffer[ buffIndex ] , ( SCOPE_ACC_LEN-buffIndex) , &iBytesRead, NULL); buffIndex += iBytesRead; if ( buffIndex >= SCOPE_ACC_LEN ) { finished = TRUE; } if (clock() > endtime) { finished = TRUE; printf(" \n------ ACC TIME OUT ------\n"); } } // swap bytes for time stamp. /* EXAMPLE DELPHI CODE temp := p.v1[6]; p.v1[6] := p.v1[9]; p.v1[9] := temp; temp := p.v1[7]; p.v1[7] := p.v1[8]; p.v1[8] := temp; */ /* swap time stamp*/ temp = buff->scope2_s.C[3]; buff->scope2_s.C[3] = buff->scope2_s.C[0]; buff->scope2_s.C[0] = temp; temp = buff->scope2_s.C[2]; buff->scope2_s.C[2] = buff->scope2_s.C[1]; buff->scope2_s.C[1] = temp; /* swap integers to correct format. */ for( cnt = 0 ; cnt < 8000 ; cnt += 2 ){ temp = buff->scope2_s.F[cnt+1]; buff->scope2_s.F[cnt+1] = buff->scope2_s.F[cnt]; buff->scope2_s.F[cnt] = temp; } return iBytesRead; } void rs_flush(const int io_port) { while(rs_getch(io_port)!=0) ; } int rs_getline(const int io_port, char line[], clock_t timeout) { int num_chars = 0; int last_num_chars = 0; char ch; double nf3; int chan = 0 ,ss1=0,ss2=0, ss3=0, foundss1=0,foundss2=0; clock_t endtime; endtime = timeout + clock(); while(clock() < endtime) { ch = rs_getch(io_port); //printf("%d ", ch); if (ch != 0) { // printf("%c", ch); if ((ch == 10) || (ch == 13) ) { line[num_chars] = ch; ++num_chars; line[num_chars] = '\0'; // terminate the string strcat( opStr, "\n" ); // flush last command as line // Check if line starts with :, is so padd if ( line[ last_num_chars ] == ':' ) { strcat( opStr, "\n" ); } else { strcat( opStr, " " ); } strcat( opStr, &line[ last_num_chars ] ); last_num_chars = num_chars; fileWriteExample( opStr, fileNameP ); strcpy( opStr, "\0") ; return(num_chars); } else { line[num_chars] = ch; ++num_chars; } if (ch == ';' ) { line[num_chars] = '\0'; // terminate the string strcat( opStr, "\n" ); // flush last command as line // Check if line starts with :, is so padd ss1= sscanf( &line[ last_num_chars ], ":CHANnel1:DISPlay %d", &chan ); if ( ss1 == 1 ) { foundss1 = 1 ; } ss2= sscanf( &line[ last_num_chars ], ":CHANnel2:DISPlay %d", &chan ); if ( ss2 == 1 ) { foundss2 = 1 ; } if ( foundss1 == 1 ) { // found Channel // Offset seems to be half that displayed on screen ss2= sscanf( &line[ last_num_chars ], "OFFSet %e", &offset1 ); ss3= sscanf( &line[ last_num_chars ], "SCALe %e", &scale1 ); if ( ss3 == 1 ) { foundss1 = 0 ; printf( "%d,%d,%d, chan :%s:%d,%f,%f,\n",ss1,ss2,ss3,&line[ last_num_chars ], chan, offset1,scale1 ); } } if ( foundss2 == 1 ) { // found Channel // Offset seems to be half that displayed on screen ss2= sscanf( &line[ last_num_chars ], "OFFSet %e", &offset2 ); ss3= sscanf( &line[ last_num_chars ], "SCALe %e", &scale2 ); if ( ss3 == 1 ) { foundss2 = 0 ; printf( "%d,%d,%d, chan :%s:%d,%f,%f,\n",ss1,ss2,ss3,&line[ last_num_chars ], chan, offset2,scale2 ); } } //printf( "%d,%d,%d, chan :%s:%d,%f,%f,\n",ss1,ss2,ss3,&line[ last_num_chars ], chan, offset,scale ); if ( line[ last_num_chars ] == ':' ) { strcat( opStr, "\n" ); } else { strcat( opStr, " " ); } strcat( opStr, &line[ last_num_chars ] ); last_num_chars = num_chars; paddingP = padding1; } /* want next line to start with */ } } // end of while line[num_chars] = '\0'; return(-1); // timeout } void rs_putch(const int io_port, const int txchar) { BOOL bWriteRC; static DWORD iBytesWritten; bWriteRC = WriteFile(hCom, &txchar, 1, &iBytesWritten,NULL); return; } void rs_putstring(const int io_port, const char *string) { while (*string != '\0') rs_putch(io_port, *string++); } void delay_ms(clock_t millis) { // This uses a lot of cpu time to run. find out hoe to use system timer. clock_t endtime; endtime = millis + clock(); while( endtime > clock() ) ; } // =========================================================================== // Sec 8.0 scope port function // =========================================================================== int scope_idn( int io_port, int setting ) { rs_flush(io_port); } /* hFile = CreateFile("C:\\Dev-Cpp\\dev\\scope\\op\\test.txt",FILE_APPEND_DATA, FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); WriteFile(hFile,txtPtr,lenTxt,&wmWritten,NULL); CloseHandle(hFile); */ int scope_mem_list( char * fileNameP ){ // printf(" buffered trace...\n"); /* # A B C D E F Aa # A: Data size digit B: Data size C: Time interval D: Channel indicator E: Resserved Data F: WaveForm Data */ int cnt; scope_S * b_p; char * bb_p; float t1, t2 ; float dt; float t,v1,v2; FILE * opStream; opStream = fopen( fileNameP, "a" ); if ( ! opStream ) { printf( " cannot open %s \n", fileNameP ); return 1 ; }; if ( scope_u1.scope_s.T != '#' ) { printf( " No data \n", fileNameP ); return 1 ; } t1 = scope_u1.scope_s.C; t2 = scope_u2.scope_s.C; dt = scope_u1.scope_s.C; t = 0.0; printf( " %g %g , %g %g \n", scale1,scale2, offset1, offset2); fprintf( opStream, "dt,%g,%e,%e \n\n", t,scope_u1.scope_s.C, scope_u2.scope_s.C ); // note: does not allow for invert of Probe x fprintf( opStream, "offset,%g,%g,scale,0, %g,%g\n",offset1,offset2,scale1,scale2 ); fprintf( opStream, "// note: does not allow for invert, Probe x or offset, may be best to use a bigger scale.\n\n" ); fprintf( opStream, "point,ch1,ch2, scaled:,t,Vch1,Vch2\n" ); for ( cnt =0 ; cnt < 4000 ; cnt +=1 ){ // converts dots -> V // 1 div = 25 dots so the value needs to be divided by 25 // The scale in not required. v1 = ( scope_u1.scope_s.F[ cnt ]/25.0*scale1 ); //+offset1; v2 = ( scope_u2.scope_s.F[ cnt ]/25.0*scale2 ); //+offset2; fprintf( opStream, "%d, %d, %d , scaled:, %g, %g, %g \n", cnt, scope_u1.scope_s.F[ cnt ], scope_u2.scope_s.F[ cnt ], t, v1,v2 ); t += dt; } fprintf( opStream, "\n\ntime,%g,%e,%e \n", t, scope_u1.scope_s.C, scope_u2.scope_s.C ); fclose( opStream ); return 1 ; } void scope_timeslice( int io_port ){ int bytesRead; int cnt; int got; iic_state = iic_nextstate; switch( iic_nextstate ) { case 0: { // Debug code to check variable size and pointers // printf(" char:%d int:%d float:%d short:%d double:%d\n",sizeof(v1),sizeof(v2),sizeof(v3),sizeof(v4),sizeof(v5)); // printf( "prts: %p %p %p \n", &scope_u1.buffer[0],&scope_u1.scope_s.C,&scope_u1.scope2_s.C[0] ); // printf( "prts: %p %p %p \n", &scope_u2.buffer[0],&scope_u2.scope_s.C,&scope_u2.scope2_s.C[0] ); rs_flush(io_port); rs_getline(io_port, line, 1); // printf("s0.%d.%d.%s.\n",iic_nextstate,strlen(line),line); printf("Connected to scope: "); rs_putstring(io_port, "*rst\r" ); iic_nextstate++; } break; case 1: { fileWriteExample( "===============================\n", fileNameP ); fileWriteExample( ctime(&timer), fileNameP ); fileWriteExample( "===============================\n", fileNameP ); // printf("ctime is %s\n", ctime(&timer)); opStr[0] = '\0'; rs_putstring(io_port, "*idn?\r" ); iic_nextstate++; } break; case 2: { got= rs_getline(io_port, line, 300); printf(" %s\n", line ); iic_nextstate++; } break; case 3: { iic_nextstate++; printf("getting *lrn?...\n"); rs_putstring(io_port, "*lrn?\r" ); } break; case 4: { got= rs_getline(io_port, line, 300); iic_nextstate++; } break; case 5: { fileWriteExample( "===============================\n", fileNameP ); printf("getting :acq1:mem?...\n"); rs_putstring(io_port, ":acq1:mem?\r" ); iic_nextstate++; } break; case 6: { iic_nextstate++; } break; case 7: { bytesRead = rs_getbuffer(io_port, &scope_u1 , 10); printf("getting :acq2:mem?...\n"); rs_putstring(io_port, ":acq2:mem?\r" ); iic_nextstate++; } break; case 8: { bytesRead = rs_getbuffer(io_port, &scope_u2, 10); // // Dump aquired measurement // printf("saving acq to file...\n"); scope_mem_list( fileNameP ); iic_nextstate++; } case 9: { fileWriteExample( "==== end ====\n\n\n", fileNameP ); iic_nextstate++; printf("finished...\n"); } break; case 13: { iic_nextstate=1; runFlag=FALSE; } break; default: { iic_nextstate++; } } /* end switch() */ } // ============================================================================