Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C++ and Serial Ports ?

Status
Not open for further replies.

Eng_Bandar

New Member
Hi all,
This is first post for me in this forum and I need your helping.
I am very wonderful from C++,I thought I will find more tutorials about C++ with serial communication but after search I didn't find good tutorial about this case Why? C++ very famous and very strong. I am very frustrate from this. I don't want begin wiht new programming language (C#, Java, ...etc). Most codes in web about this case contain errors.

I use Microsoft Visual C++ 2010.

Any simple tutorials, Any advices, Any simple codes (like send 'a' through serial port).
 
Last edited:
I don't want begin wiht new programming language (C#, Java, ...etc).
Any simple tutorials, Any advices, Any simple codes (like send 'a' through serial port).

You could give C# a chance.

This is serial port code in C#:

Code:
SerialPort serialPort1;
serialPort1 = new System.IO.Ports.SerialPort();
serialPort1.BaudRate = 9600;
serialPort1.PortName = "COM1";
serialPort1.Open();
serialPort1.Write('a');

This is serial port code in C (small part of it):

Code:
/** Initialize the serial port for data transfer with ATmega88.*/
int com2_initialize(void){
  int fd;

  // Initialize the serial device
  fd = open(COM2_DEVICE, O_RDWR | O_NOCTTY);
  // Returns error if failed to initialize the device.
  if(fd < 0){
    writelog("rs232laser", "failed to initialize serial device, fd was %i", fd);
    return 0;
  }

  // Get and save the current serial port settings.
  tcgetattr(fd, &presentTermIO);

  // Initialize structure for new port settings.
  memset(&newTermIO, 0, sizeof(newTermIO));

  /*
    Set necessary attributes for port.
    Attributes:
    
    BAUDRATE = bps rate
    CS8 = 8bit, no parity, 1 stopbit
    CLOCAL = local connection, no modem
    CREAD = enable port for receiving characters
  */
  newTermIO.c_cflag = COM2_BAUDRATE | CS8 | CLOCAL | CREAD;

  // Enable raw output.
  newTermIO.c_oflag = 0;

  // Set input mode to non-canonical.
  newTermIO.c_lflag = 0;

  // Non-canonical input processing parameters.
  newTermIO.c_cc[VMIN] = COM2_MAX_INPUTSIZE;
  newTermIO.c_cc[VTIME] = COM2_TIMEOUT;

  // Clean the modem line and activate the settings for the port.
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newTermIO);

  return fd;

}
 
Last edited:
You could give C# a chance.

This is serial port code in C#:

Code:
SerialPort serialPort1;
serialPort1 = new System.IO.Ports.SerialPort();
serialPort1.BaudRate = 9600;
serialPort1.PortName = "COM1";
serialPort1.Open();
serialPort1.Write('a');

This is serial port code in C (small part of it):

Code:
/** Initialize the serial port for data transfer with ATmega88.*/
int com2_initialize(void){
  int fd;

  // Initialize the serial device
  fd = open(COM2_DEVICE, O_RDWR | O_NOCTTY);
  // Returns error if failed to initialize the device.
  if(fd < 0){
    writelog("rs232laser", "failed to initialize serial device, fd was %i", fd);
    return 0;
  }

  // Get and save the current serial port settings.
  tcgetattr(fd, &presentTermIO);

  // Initialize structure for new port settings.
  memset(&newTermIO, 0, sizeof(newTermIO));

  /*
    Set necessary attributes for port.
    Attributes:
    
    BAUDRATE = bps rate
    CS8 = 8bit, no parity, 1 stopbit
    CLOCAL = local connection, no modem
    CREAD = enable port for receiving characters
  */
  newTermIO.c_cflag = COM2_BAUDRATE | CS8 | CLOCAL | CREAD;

  // Enable raw output.
  newTermIO.c_oflag = 0;

  // Set input mode to non-canonical.
  newTermIO.c_lflag = 0;

  // Non-canonical input processing parameters.
  newTermIO.c_cc[VMIN] = COM2_MAX_INPUTSIZE;
  newTermIO.c_cc[VTIME] = COM2_TIMEOUT;

  // Clean the modem line and activate the settings for the port.
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newTermIO);

  return fd;

}

Thank you so much
I killed the frustrating by C#
Thanks again
this is first project for me in C#
YouTube - EngBandar1's Channel
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top