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. Basic Serial I/O in VC++

Basic Serial I/O in VC++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
4 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.
  • J Offline
    J Offline
    JoeMass
    wrote on last edited by
    #1

    I have limited VC++ experience. I have used C and other languages a lot. I need to communicate with a serial device from a VC++ Dialog application, which I am developing. How do I do this? Thank you, Joe Massarelli

    A 1 Reply Last reply
    0
    • J JoeMass

      I have limited VC++ experience. I have used C and other languages a lot. I need to communicate with a serial device from a VC++ Dialog application, which I am developing. How do I do this? Thank you, Joe Massarelli

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      I posted this reply (incorrectly - the guy was asking about a parrallel port...) this morning. Mostly like reading and writing a file: (EXAMPLE - your mileage may vary...) void CSerialPort::initPort(int comNumber, int baudRate) { closePort(); m_ComNumber = comNumber; m_BaudRate = baudRate; CString comStr; comStr.Format(_T("%s%d"), _T("COM"), comNumber); m_ComHandle = CreateFile(comStr, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); DCB dcb; GetCommState(m_ComHandle, &dcb); dcb.BaudRate = m_BaudRate; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; SetCommState(m_ComHandle, &dcb); COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(m_ComHandle, &timeouts); EscapeCommFunction(m_ComHandle, SETRTS); EscapeCommFunction(m_ComHandle, SETDTR); } bool CSerialPort::readByteFromChannel(char* inByte) { DWORD numRead; BOOL ret = true; ret = ReadFile(m_ComHandle, inByte, 1, &numRead, 0); return ( (ret) && (numRead == 1) ); } void CSerialPort::sendPacket(char* pktBuffer, int len) { DWORD numWritten; WriteFile(m_ComHandle, pktBuffer, len, &numWritten, 0); if (numWritten != (DWORD)len) // do comm error handling here }

      R 1 Reply Last reply
      0
      • A Anonymous

        I posted this reply (incorrectly - the guy was asking about a parrallel port...) this morning. Mostly like reading and writing a file: (EXAMPLE - your mileage may vary...) void CSerialPort::initPort(int comNumber, int baudRate) { closePort(); m_ComNumber = comNumber; m_BaudRate = baudRate; CString comStr; comStr.Format(_T("%s%d"), _T("COM"), comNumber); m_ComHandle = CreateFile(comStr, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); DCB dcb; GetCommState(m_ComHandle, &dcb); dcb.BaudRate = m_BaudRate; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; SetCommState(m_ComHandle, &dcb); COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(m_ComHandle, &timeouts); EscapeCommFunction(m_ComHandle, SETRTS); EscapeCommFunction(m_ComHandle, SETDTR); } bool CSerialPort::readByteFromChannel(char* inByte) { DWORD numRead; BOOL ret = true; ret = ReadFile(m_ComHandle, inByte, 1, &numRead, 0); return ( (ret) && (numRead == 1) ); } void CSerialPort::sendPacket(char* pktBuffer, int len) { DWORD numWritten; WriteFile(m_ComHandle, pktBuffer, len, &numWritten, 0); if (numWritten != (DWORD)len) // do comm error handling here }

        R Offline
        R Offline
        r i s h a b h s
        wrote on last edited by
        #3

        and how 'bout usb port? rIsHaBh

        J 1 Reply Last reply
        0
        • R r i s h a b h s

          and how 'bout usb port? rIsHaBh

          J Offline
          J Offline
          jhorstkamp
          wrote on last edited by
          #4

          Don't know - haven't had to do them yet but I think they are treated the same as basic serial ports.

          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