Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to write more that ONE data byte into ioctl file?

How to write more that ONE data byte into ioctl file?

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingquestionhardwareregexhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.

    // original expanded
    // write directly to expander using ioctl file descriptor
    // and plain write(fd,data,#)
    // parameters
    // int file file descriptor
    // char data data to write
    // int NumberOfCharacters to write
    // return 0 when actual # of characters match NumberOfCharacters
    int CLASS_LCM1602::expanderWrite(
    int file,
    char data,
    int NumberOfCharacters) {
    #ifdef DEBUG
    //cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
    cout << "*** TRACE file " << __FILE__ << endl;
    cout << " Enhanced function " << __FUNCTION__ << endl;
    cout << " line " << __LINE__ << endl;
    #endif
    int NumberOfCharactersWritten;
    // why NOBACKLIGHT during initialization - too fast to see anyway (?)
    char buffer = data; // | LCD_BACKLIGHT;
    //buffer = 0;
    //while (1)
    {
    #ifdef DEBUG
    cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
    cout << " buffer HEX cast " << hex << buffer << endl;
    cout << " buffer + " << hex << +buffer << endl;

    cout << " function " << \_\_FUNCTION\_\_ << endl;
    cout << " line " << \_\_LINE\_\_ << endl;
    

    #endif
    //sleep(5);
    }

    // NumberOfCharacters defaults to 1

    NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);

    // check characters written
    if (NumberOfCharacters == NumberOfCharactersWritten) {
    #ifdef DEBUG
    cout << "Success NumberOfCharacters written " << NumberOfCharacters
    << endl;
    #endif
    //exit(1);
    return 0;
    } else {
    cout << "\033[1;31mFailed expanderWrite\033[0m\n"; /

    L V J 3 Replies Last reply
    0
    • V Vaclav_

      This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.

      // original expanded
      // write directly to expander using ioctl file descriptor
      // and plain write(fd,data,#)
      // parameters
      // int file file descriptor
      // char data data to write
      // int NumberOfCharacters to write
      // return 0 when actual # of characters match NumberOfCharacters
      int CLASS_LCM1602::expanderWrite(
      int file,
      char data,
      int NumberOfCharacters) {
      #ifdef DEBUG
      //cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
      cout << "*** TRACE file " << __FILE__ << endl;
      cout << " Enhanced function " << __FUNCTION__ << endl;
      cout << " line " << __LINE__ << endl;
      #endif
      int NumberOfCharactersWritten;
      // why NOBACKLIGHT during initialization - too fast to see anyway (?)
      char buffer = data; // | LCD_BACKLIGHT;
      //buffer = 0;
      //while (1)
      {
      #ifdef DEBUG
      cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
      cout << " buffer HEX cast " << hex << buffer << endl;
      cout << " buffer + " << hex << +buffer << endl;

      cout << " function " << \_\_FUNCTION\_\_ << endl;
      cout << " line " << \_\_LINE\_\_ << endl;
      

      #endif
      //sleep(5);
      }

      // NumberOfCharacters defaults to 1

      NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);

      // check characters written
      if (NumberOfCharacters == NumberOfCharactersWritten) {
      #ifdef DEBUG
      cout << "Success NumberOfCharacters written " << NumberOfCharacters
      << endl;
      #endif
      //exit(1);
      return 0;
      } else {
      cout << "\033[1;31mFailed expanderWrite\033[0m\n"; /

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Vaclav_ wrote:

      ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED.

      Well that really encourages people to want to help you.

      1 Reply Last reply
      0
      • V Vaclav_

        This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.

        // original expanded
        // write directly to expander using ioctl file descriptor
        // and plain write(fd,data,#)
        // parameters
        // int file file descriptor
        // char data data to write
        // int NumberOfCharacters to write
        // return 0 when actual # of characters match NumberOfCharacters
        int CLASS_LCM1602::expanderWrite(
        int file,
        char data,
        int NumberOfCharacters) {
        #ifdef DEBUG
        //cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
        cout << "*** TRACE file " << __FILE__ << endl;
        cout << " Enhanced function " << __FUNCTION__ << endl;
        cout << " line " << __LINE__ << endl;
        #endif
        int NumberOfCharactersWritten;
        // why NOBACKLIGHT during initialization - too fast to see anyway (?)
        char buffer = data; // | LCD_BACKLIGHT;
        //buffer = 0;
        //while (1)
        {
        #ifdef DEBUG
        cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
        cout << " buffer HEX cast " << hex << buffer << endl;
        cout << " buffer + " << hex << +buffer << endl;

        cout << " function " << \_\_FUNCTION\_\_ << endl;
        cout << " line " << \_\_LINE\_\_ << endl;
        

        #endif
        //sleep(5);
        }

        // NumberOfCharacters defaults to 1

        NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);

        // check characters written
        if (NumberOfCharacters == NumberOfCharactersWritten) {
        #ifdef DEBUG
        cout << "Success NumberOfCharacters written " << NumberOfCharacters
        << endl;
        #endif
        //exit(1);
        return 0;
        } else {
        cout << "\033[1;31mFailed expanderWrite\033[0m\n"; /

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        Vaclav_ wrote:

        PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED.

        Hmm, don't you mind such a "thesis" together with unformatted code looks like gross unrespect to guys you ask to help? :sigh: :suss:

        1 Reply Last reply
        0
        • V Vaclav_

          This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.

          // original expanded
          // write directly to expander using ioctl file descriptor
          // and plain write(fd,data,#)
          // parameters
          // int file file descriptor
          // char data data to write
          // int NumberOfCharacters to write
          // return 0 when actual # of characters match NumberOfCharacters
          int CLASS_LCM1602::expanderWrite(
          int file,
          char data,
          int NumberOfCharacters) {
          #ifdef DEBUG
          //cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
          cout << "*** TRACE file " << __FILE__ << endl;
          cout << " Enhanced function " << __FUNCTION__ << endl;
          cout << " line " << __LINE__ << endl;
          #endif
          int NumberOfCharactersWritten;
          // why NOBACKLIGHT during initialization - too fast to see anyway (?)
          char buffer = data; // | LCD_BACKLIGHT;
          //buffer = 0;
          //while (1)
          {
          #ifdef DEBUG
          cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
          cout << " buffer HEX cast " << hex << buffer << endl;
          cout << " buffer + " << hex << +buffer << endl;

          cout << " function " << \_\_FUNCTION\_\_ << endl;
          cout << " line " << \_\_LINE\_\_ << endl;
          

          #endif
          //sleep(5);
          }

          // NumberOfCharacters defaults to 1

          NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);

          // check characters written
          if (NumberOfCharacters == NumberOfCharactersWritten) {
          #ifdef DEBUG
          cout << "Success NumberOfCharacters written " << NumberOfCharacters
          << endl;
          #endif
          //exit(1);
          return 0;
          } else {
          cout << "\033[1;31mFailed expanderWrite\033[0m\n"; /

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          You do it like with any write operation to a file descriptor by passing a buffer and it's size:

          char buf[2];
          // Assuming you need to write two 4 bit values passed as a byte
          // with sending the lower nibble first
          buf[0] = data & 0x0f;
          buf[1] = data >> 4;
          write(file, buf, 2);

          V 1 Reply Last reply
          0
          • J Jochen Arndt

            You do it like with any write operation to a file descriptor by passing a buffer and it's size:

            char buf[2];
            // Assuming you need to write two 4 bit values passed as a byte
            // with sending the lower nibble first
            buf[0] = data & 0x0f;
            buf[1] = data >> 4;
            write(file, buf, 2);

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            Thank you.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups