Save data to a csv or xls file

I just write a program that read the start-up SRAM data pattern using Arduino Mega 2560. The pattern can be transmitted from Arduino to laptop using serial port and displayed on serial monitor. But I want to save the data into a csv or xls file for the future processing. What should I do next? Must I use the SD card or some other similar chips? Besides, my laptop is a macbook pro. Here is my code

typedef unsigned short u16;
typedef unsigned char u8;
int CE = 50;
int OE = 51;
int WE = 52;
long count = 0;
double hamming_weight = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(CE, OUTPUT);//Set PB3(50) as outputs(chip enable)
  pinMode(OE, OUTPUT);//Set PB2(51) as outputs(output enable)
  pinMode(WE, OUTPUT);//Set PB1(52) as outputs(write enable)
  DDRA = B11111111;//Set PortA as outputs(Data)
  DDRC = B11111111;//Set PortC as outputs(Address)
  DDRL = B11111111;//Set PortL as outputs(Address)
  Serial.begin(9600);
}

void loop() {
  u8 r_data = 0;
  u8 w_data = 0xFF;
  u16 addr; //SRAM address
  /*SRAM Init*/
  SRAM_Init( );

  /*for(addr=0;addr<=0x7FFF;addr++){
    //Write the SRAM
    SRAM_Write(addr,w_data);
  }*/
  
  for(addr=0;addr<=0x7FFF;addr++){
    //Read the SRAM 
    r_data = SRAM_Read(addr);
    displayInfo(addr,r_data);
  }
  Serial.print("Total\t");
  Serial.println(count);
  hamming_weight = count/262144.00;
  Serial.print("Hamming weight\t");
  Serial.print(hamming_weight*100,2);
  Serial.println("%");
  while(1){
    }
  
}

/*SRAM Init*/
void SRAM_Init(void){
  digitalWrite(CE, HIGH);
  digitalWrite(WE, HIGH);
  digitalWrite(OE, HIGH);
}

/*Write 8-bit data to SRAM. Write cycle No.1 (/WE controlled)*/
void SRAM_Write( u16 addr, u8 data){
  /*Put the address lines 9X6*/
 
  PORTC = (u8)(addr&0x00FF);
  PORTL = (u8)((addr&0x7F00)>>8);
  
  /*Begin the write cycle*/
  digitalWrite(WE, HIGH);
  digitalWrite(OE, HIGH);
  digitalWrite(CE, LOW);//Select the chip
  /*Send data to SRAM*/
  PORTA = (u8)data;
  digitalWrite(WE, LOW);
  digitalWrite(WE, HIGH);
  digitalWrite(CE, HIGH);
  
}

/*Read from SRAM. Read cycle No.2 (WE = HIGH)*/
unsigned char SRAM_Read(u16 addr){
  unsigned char val = 0;//Store data
  
  /*Setup for data input*/
  DDRA = B00000000;//Set PortA as inputs(Data)
  digitalWrite(CE, HIGH);//Deselect chip

  /*Put the address lines 9X6*/
  PORTC = (u8)(addr&0x00FF);
  PORTL = (u8)((addr&0x7F00)>>8);
  /*Begin read cycle*/
  digitalWrite(WE, HIGH);
  digitalWrite(OE, HIGH);
  digitalWrite(CE, LOW);//Select the chip

  digitalWrite(OE, LOW);//Output enable
  val = PINA;
  digitalWrite(OE, HIGH);

  /*End read cycle*/
  digitalWrite(CE, HIGH);//Deselect chip
  return val;
}

/*Display the information*/
void displayInfo(u16 index, u8 message){
  
  char buffer[8];//address
  sprintf(buffer,"%02X",(u8)((index&0x7F00)>>8));
  Serial.print(buffer);
  
 

  sprintf(buffer,"%02X",(u8)(index&0x00FF));
  Serial.print(buffer);
 
  
  Serial.print("\t");
  
  for(int b = 0;b<=7;b++)//data
  { 
    if(bitRead(message,b) == 1)
    {  
      count += 1;
    }
    Serial.print(bitRead(message,b)); 
  }
  Serial.println();
}

Thank you!

If you want the Arduino to do the saving then use an SD card. If you want the MacBook to do the saving you could write a little code for the MacBook that reads the serial line and saved into the format you want. Or you could just write the values to the serial monitor with commas in between and copy-paste that into a text editor and save with a .csv extension.

zhliao: The pattern can be transmitted from Arduino to laptop using serial port and displayed on serial monitor. But I want to save the data into a csv or xls file for the future processing.

Since you are sending the data to the serial monitor all you need do is format the data sensibly, and send it to a proper terminal programme instead. It is simply a matter of sending the numeric values with a comma after each except the last, which gets a lineshift instead i.e.

Serial.print(firstval);
Serial.print(",");
Serial.println(lastval);

The terminal programme can record the data as a .csv, which is all you need. It can also timestamp the dataline using the PC clock.

There is no need for an SD unless you cannot maintain the connection with the computer. If you do need an SD, you send the data to it in essentially the same manner

Myfile.print(firstval);
Myfile.print(",");
Myfile.println(lastval);

And transfer the file to PC at leisure.

Duplicate post deleted.
DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.

Delta_G: If you want the Arduino to do the saving then use an SD card. If you want the MacBook to do the saving you could write a little code for the MacBook that reads the serial line and saved into the format you want. Or you could just write the values to the serial monitor with commas in between and copy-paste that into a text editor and save with a .csv extension.

I want the Macbook to do the saving job but I do not know how to use the suitable functions to finish this job. Could you please give me some suggestions? Thank you.

Nick_Pyner: Since you are sending the data to the serial monitor all you need do is format the data sensibly, and send it to a proper terminal programme instead. It is simply a matter of sending the numeric values with a comma after each except the last, which gets a lineshift instead i.e.

Serial.print(firstval);
Serial.print(",");
Serial.println(lastval);

The terminal programme can record the data as a .csv, which is all you need. It can also timestamp the dataline using the PC clock.

There is no need for an SD unless you cannot maintain the connection with the computer. If you do need an SD, you send the data to it in essentially the same manner

Myfile.print(firstval);
Myfile.print(",");
Myfile.println(lastval);

And transfer the file to PC at leisure.

I do not want to use the SD card just using the Macbook to do the job. So is there any proper terminal program that can save the data into a .csv file? Or what content should I looking for? Thank you~

RealTerm can save your data, I am sure there are others too. http://realterm.sourceforge.net/

Not sure RealTerm works on MacBook. .

Learn python if not already, the python serial library is a great way to handle datatransfer as python has lots of useful libraries and is easy to use and portable. You can import csv files/streams into pandas (the python data analysis library) for instance. But that's for another day!

import serial

device = # you have to figure this bit out
baudrate = 115200 # or whatever you use

arduino_port = serial.Serial (device,
                              baudrate = baudrate,
                              parity   = serial.PARITY_NONE,
                              stopbits = serial.STOPBITS_ONE,
                              bytesize = serial.EIGHTBITS,
                              rtscts   = False,
                              dsrdtr   = False,
                              xonxoff  = False,
                              timeout  = 0)
while True:
    line = arduino_port.readline ()
    # whatever you want to do here...

zhliao: I do not want to use the SD card just using the Macbook to do the job. So is there any proper terminal program that can save the data into a .csv file?

Yes. Other than knowing how and when to print a comma and a linefeed, you really don't need anything else, and I submit that the idea of learning another language for a simple task as this is absurd. Having said that, knowing what junk not to print is also a good idea. True to its name, a CSV file usually just contains numbers as read or derived from sensors, no strings, and stuff like "Hamming weight\t"; is junk.

Like Larry, I use RealTerm. I understand there is a swag of them, they all work in essentially the same way, and they are all free. I'm sure the situation is the same for a macbook and I believe I recently heard of somebody using CoolTerm.