ADC plotter (Matlab)


t90add

Recommended Posts

Hello, I wrote a simple script in Matlab that will use the serial com port to create a plot (16 bit plot). Currently works with offroad's bitfile.

 

 

 Since I can't upload the file, I will place the code here:

portnum = input('What COM port is it (i.e. COM4 type 4)?') %Asks a numberrate=input('What Baudrate is it? (Default is 9600 (type 1))')if sum(rate)    rate=9600endTimePeriod=input('What time period? (Default 400)')portstr=num2str(portnum)port=['COM',portstr]hold off;s1=serial(port,'Baudrate',rate);  %puts parameters on serial portfopen(s1);                          %opens the serial portarray=fscanf(s1,'%c');              %starts listening. instantous readings pin0=[1:TimePeriod];    %Initliaze vectors for each ADC pinpin1=[1:TimePeriod];pin2=[1:TimePeriod];pin3=[1:TimePeriod];pin4=[1:TimePeriod];pin5=[1:TimePeriod];pin6=[1:TimePeriod];pin7=[1:TimePeriod];pin8=[1:TimePeriod];pin9=[1:TimePeriod];pin10=[1:TimePeriod];pin11=[1:TimePeriod];pin12=[1:TimePeriod];pin13=[1:TimePeriod];pin14=[1:TimePeriod];pin15=[1:TimePeriod];i=1;while i<TimePeriod    array=fscanf(s1,'%c');         %Outputs a character line of the Hex values from the two ADCs    Hexpin0=array([1:2]);           %Hex values from first input    Hexpin1=array([4:5]);    Hexpin2=array([7:8]);    Hexpin3=array([10:11]);    Hexpin4=array([13:14]);    Hexpin5=array([17:18]);    Hexpin6=array([20:21]);    Hexpin7=array([23:24]);    Hexpin8=array([26:27]);    Hexpin9=array([29:30]);    Hexpin10=array([32:33]);    Hexpin11=array([35:36]);    Hexpin12=array([38:39]);    Hexpin13=array([41:42]);    Hexpin14=array([44:45]);    Hexpin15=array([46:47]);        Intpin0=hex2dec(Hexpin0);           %Converted the Hex values to Integers    Intpin1=hex2dec(Hexpin1);    Intpin2=hex2dec(Hexpin2);    Intpin3=hex2dec(Hexpin3);    Intpin4=hex2dec(Hexpin4);    Intpin5=hex2dec(Hexpin5);    Intpin6=hex2dec(Hexpin6);    Intpin7=hex2dec(Hexpin7);    Intpin8=hex2dec(Hexpin8);    Intpin9=hex2dec(Hexpin9);    Intpin10=hex2dec(Hexpin10);    Intpin11=hex2dec(Hexpin11);    Intpin12=hex2dec(Hexpin12);    Intpin13=hex2dec(Hexpin13);    Intpin14=hex2dec(Hexpin14);    Intpin15=hex2dec(Hexpin15);    pin0(i)=Intpin0;                %Vector of intgers from ADC's first input    pin1(i)=Intpin1;    pin2(i)=Intpin2;    pin3(i)=Intpin3;    pin4(i)=Intpin4;    pin5(i)=Intpin5;    pin6(i)=Intpin6;    pin7(i)=Intpin7;    pin8(i)=Intpin8;    pin9(i)=Intpin9;    pin10(i)=Intpin10;    pin11(i)=Intpin11;    pin12(i)=Intpin12;    pin13(i)=Intpin13;    pin14(i)=Intpin14;    pin15(i)=Intpin15;     i=i+1;end    x=[1:TimePeriod];    plot(x,pin0,'y');   %Plot the first analog input       hold on     plot(x,pin1,'m')    plot(x,pin2,'c')    plot(x,pin3,'r')    plot(x,pin4,'g')    plot(x,pin5,'b')    plot(x,pin6,'w')    plot(x,pin7,'k')        plot(x,pin8,'y-*')    plot(x,pin9,'m-*')    plot(x,pin10,'c-*')    plot(x,pin11,'r-*')    plot(x,pin12,'g-*')    plot(x,pin13,'b-*')    plot(x,pin14,'w-*')    plot(x,pin15,'k-*')        fclose(s1); 
Link to comment
Share on other sites

Nice.

 

If anybody wants to access it from C, it can be done on Windows as follows (open-source Cygwin)

This simply dumps the ASCII to stdout without any parsing.

 

// compile simply via "gcc thisProgram.c"

#define WIN32_LEAN_AND_MEAN
#define NOSOUND
#include <windows.h>
#include <assert.h>
#include <stdio.h>
int main(void){
    // the backslash jungle: see http://support.microsoft.com/kb/115831
  HANDLE hSerial = CreateFile
    ("\\\\.\\COM15",      
     GENERIC_READ | GENERIC_WRITE,    
     0,                  
     NULL,               
     OPEN_EXISTING,      
     0,                  
     NULL                
     );
 
  assert(hSerial != INVALID_HANDLE_VALUE);
 
  while (1){
    char buf[3];
    DWORD tmp;
    buf[2] = 0;
    int r = ReadFile(hSerial, buf, 2, &tmp, NULL);
    assert®;
    if (tmp > 0){
      buf[tmp] = 0;
      fprintf(stdout, "%s", buf);
    }
  }
}
 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.