Code to convert Yokogawa messages to CAN bus

Dependencies:   USBHost mbed

main.cpp

Committer:
tomhillman
Date:
2015-09-09
Revision:
0:1dc6b238aeb5

File content as of revision 0:1dc6b238aeb5:

#include "mbed.h"
#include "USBHostSerial.h"

DigitalOut led(LED1);
Serial pc(USBTX, USBRX);


long i;
long j;
long k;
long start;
int l;
long m;
int myArray[8] = { 46, 47, 48, 52, 61, 62, 63, 67};
int8_t sendTest[8];
int main() {
    pc.printf("\nMain started \r\n");
    CAN can1(p30, p29);
    can1.frequency(500000);
    USBHostSerial serial;
    
    char buf2[1024];
    while(1) {
       
        pc.printf("Start\r\n");

    
        // try to connect a serial device
        while(!serial.connect())
        {
            pc.printf("Trying to connect \r\n");
            wait(1);
            }
        
        pc.printf("Connected \r\n");
        wait(1);
        LPC_UART0->FCR |= 0x06;
        wait(1);
        while (1) {
            i = 0;
            int test = 0;
            led=!led; 
            serial.flush();
            wait(0.1);
            
            for(m=0;m<8;m++){
            i = 0;
            serial.flush();
            wait(0.1);
            serial.printf("NUM:NORM:VAL? %d\r\n", myArray[m]); // send SCPI command // just \n works
            wait(0.1);
            
            test = 1;
            while (i<=30) {
                test = serial.available();
                buf2[i] = serial.getc();
                pc.printf("%c", buf2[i], test);
                i++;
                }
                serial.flush();
                
            j = 0;    
            
            
            for(j=0;j<i;j++){
                if(buf2[j]==0x23){ // look for "#" that marks start of data
                    break;
                    }
                }
            start = j+7;
            i = start-1;k=1;
                pc.printf("Item(%d): 0x%X.%X.%X.%X ",myArray[m],buf2[i],buf2[i+1],buf2[i+2],buf2[i+3]);

               for(l=0;l<4;l++){
                    
                    sendTest[l] = (int8_t)buf2[i+l];
                }
                
                if(can1.write(CANMessage((1024+m+1), (const char *) &sendTest[0], 8))){pc.printf(" Can Sent..\r\n");}
                //can1.write(CANMessage((1024+m+1), (const char *) &sendTest[0], 8));

                
  
            }
            
                        // if device disconnected, try to connect it again
            if (!serial.connected()){
                pc.printf("Connection Lost!\r\n");
                break;
                }
                
            wait(1);
            }
        }
        
        
        
}