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. Managed C++/CLI
  4. How to use the SerialPort->Write function

How to use the SerialPort->Write function

Scheduled Pinned Locked Moved Managed C++/CLI
data-structureshelptutorialquestion
4 Posts 2 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.
  • S Offline
    S Offline
    ShayD1
    wrote on last edited by
    #1

    What I need to do is this: To take a structure (or a class that contains only variables) then convert it somehow to an array of bytes, so i will be able to send it with the Write function of the SerialPort Control. On the other side of the port i need to do backward process Can anyone help me with this one? thanks

    M 1 Reply Last reply
    0
    • S ShayD1

      What I need to do is this: To take a structure (or a class that contains only variables) then convert it somehow to an array of bytes, so i will be able to send it with the Write function of the SerialPort Control. On the other side of the port i need to do backward process Can anyone help me with this one? thanks

      M Offline
      M Offline
      Milton Karimbekallil
      wrote on last edited by
      #2

      Call createfile to get a valid handle to the port: hPort = CreateFile(lpszPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); // NT wants overlapped Then set the device control block with appropriate baude rate and parity bit settings and other settings. DCB dcb; dcb.DCBlength = sizeof( DCB ) ; GetCommState( ufp, &dcb ) ; //change settings as required by the device connected the serial port. dcb.BaudRate = baud; //set the new value back SetCommState(ufp, &dcb); Now you can start writing to the device. WriteFile(hPort, bfr2write, dwBytestoWrite, &dwBytesWritten, &OVERLAPPEDWrite); To Readfrom the file: ClearCommError( hPort, &dwErrorFlags, &ComStat ) ; // Clear error flag dwLength = min( (DWORD) MaxLength, ComStat.cbInQue ); // # of bytes to get if (dwLength > 0) { fReadStat = ReadFile( hPort, lpzBlock, // Do the READ dwLength, &dwLength, &osRead ) ; if (!fReadStat) { Err("ReadSPort"); dwLength = 0 ; ClearCommError( hPort, &dwErrorFlags, &ComStat ) ; } } cheers...milton.

      S 1 Reply Last reply
      0
      • M Milton Karimbekallil

        Call createfile to get a valid handle to the port: hPort = CreateFile(lpszPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); // NT wants overlapped Then set the device control block with appropriate baude rate and parity bit settings and other settings. DCB dcb; dcb.DCBlength = sizeof( DCB ) ; GetCommState( ufp, &dcb ) ; //change settings as required by the device connected the serial port. dcb.BaudRate = baud; //set the new value back SetCommState(ufp, &dcb); Now you can start writing to the device. WriteFile(hPort, bfr2write, dwBytestoWrite, &dwBytesWritten, &OVERLAPPEDWrite); To Readfrom the file: ClearCommError( hPort, &dwErrorFlags, &ComStat ) ; // Clear error flag dwLength = min( (DWORD) MaxLength, ComStat.cbInQue ); // # of bytes to get if (dwLength > 0) { fReadStat = ReadFile( hPort, lpzBlock, // Do the READ dwLength, &dwLength, &osRead ) ; if (!fReadStat) { Err("ReadSPort"); dwLength = 0 ; ClearCommError( hPort, &dwErrorFlags, &ComStat ) ; } } cheers...milton.

        S Offline
        S Offline
        ShayD1
        wrote on last edited by
        #3

        Thanks, But in this way I don’t really use the SerialPort Control that .net 2 gave me. Do u know how to do it with the SerialPort Control?

        M 1 Reply Last reply
        0
        • S ShayD1

          Thanks, But in this way I don’t really use the SerialPort Control that .net 2 gave me. Do u know how to do it with the SerialPort Control?

          M Offline
          M Offline
          Milton Karimbekallil
          wrote on last edited by
          #4

          sorry, not yet used.

          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