Comm Port - Send Packets

This is a discussion on Comm Port - Send Packets within the C Programming forums, part of the General Programming Boards category; Hi guys, I am trying to write sotware in C to send text to an LCD screen from a Win ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    37

    Comm Port - Send Packets

    Hi guys,

    I am trying to write sotware in C to send text to an LCD screen from a Win XP machine through a Comm Port Does anyone know how to do this. I think I have set up the port I just have no idea how to send the packets.


    Can anyone help??

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    37
    To make it easier I should supply my code to set up the port:

    Code:
    #include <windows.h>
    
    #define RECEIVEBUFFERSIZE         4096
    
    typedef ubyte;
    
    int main(int argc, char *argv[])
    {
      DCB dcb;
      HANDLE hCom;
      BOOL fSuccess;
      char *pcCommPort = "COM1";
      ubyte SerialReceiveBuffer[RECEIVEBUFFERSIZE];
      DWORD ReceiveBufferHead;
      DWORD ReceiveBufferTail;
      DWORD ReceiveBufferTailPeek;
    
      //----------------------------------------------------------------------------
         
      int Serial_Init(char *port, int baud_rate)
      {
         hCom = CreateFile( pcCommPort,
                          GENERIC_READ | GENERIC_WRITE,
                          0,    // must be opened with exclusive-access
                          NULL, // no security attributes
                          OPEN_EXISTING, // must use OPEN_EXISTING
                          0,    // not overlapped I/O
                          NULL  // hTemplate must be NULL for comm devices
                          );
    
         if (hCom == INVALID_HANDLE_VALUE)
         {
             // Handle the error.
             printf ("CreateFile() failed with error %d.\n", GetLastError());
             return (1);
         }
    
         // Build on the current configuration, and skip setting the size
         // of the input and output buffers with SetupComm.
    
         fSuccess = GetCommState(hCom, &dcb);
    
         if (!fSuccess)
         {
            // Handle the error.
            printf ("GetCommState() failed with error %d.\n", GetLastError());
            hCom = 0;
            return (2);
         }
    
         // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
    
         dcb.BaudRate = baud_rate;     // set the baud rate
         dcb.ByteSize = 8;             // data size, xmit, and rcv
         dcb.Parity = NOPARITY;        // no parity bit
         dcb.StopBits = ONESTOPBIT;    // one stop bit
         dcb.fRtsControl = RTS_CONTROL_ENABLE;
         dcb.fDtrControl = DTR_CONTROL_ENABLE;
         dcb.fAbortOnError = FALSE;
         fSuccess = SetCommState(hCom, &dcb);
    
         if (!fSuccess)
         {
            // Handle the error.
            printf ("SetCommState() failed with error %d.\n", GetLastError());
            return (3);
         }
         
         COMMTIMEOUTS
           our_timeouts;
           
         if(TRUE != GetCommTimeouts (
                 hCom, &our_timeouts))
         {
                 hCom = 0;
                 printf("GetCommTimeouts() failed with error %d.\n", GetLastError());
                 return(4);
                 }
                 
         our_timeouts.ReadIntervalTimeout = 2000;
         our_timeouts.ReadTotalTimeoutMultiplier = 2000;
         our_timeouts.ReadTotalTimeoutConstant = 2000;
         
         if(TRUE != SetCommTimeouts(
                 hCom, &our_timeouts))
         {
                 hCom = 0;
                 printf("SetCommTimeouts() failed with error %d.\n", GetLastError());
                 return(5);
                 }
                 
         void EmptyReceiveBuffer(void);
         
         printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
         return (0);
      }
      
      //----------------------------------------------------------------------------
      
      void Uninit_Serial() {
           CloseHandle(hCom);
           hCom = NULL;
           }
           
      //----------------------------------------------------------------------------
      
      void Sync_Read_Buffer_Void(void) {
           ubyte Incoming[4096];
           DWORD BytesRead;
           COMSTAT status;
           DWORD errors;
           DWORD i = 0;
           int j = 10;
           
           if(!hCom)
             return;
             
           ClearCommError(
                          hCom,
                          &errors,
                          &status);
           
           if(status.cbInQue)
             {
             if(!ReadFile(hCom,
               (LPVOID *)Incoming,
               status.cbInQue,
               &BytesRead,
               NULL))
               printf("ReadFile() failed with error %d.\n", GetLastError());
             else
               if(i < BytesRead) {
                 SerialReceiveBuffer[ReceiveBufferHead] = Incoming[i];
                 ReceiveBufferHead++;
                 if(RECEIVEBUFFERSIZE <=ReceiveBufferHead) {
                   ReceiveBufferHead = 0;
                 }
                 i++;
               }
             }
           }
           
      //----------------------------------------------------------------------------
           
      DWORD BytesAvail(void) {
        register int LocalReceiveBufferHead;
        register int return_value;
          
        LocalReceiveBufferHead = ReceiveBufferHead;
        if((return_value=(LocalReceiveBufferHead) - (int)ReceiveBufferTail) < 0)
          return_value += RECEIVEBUFFERSIZE;
        return(return_value);
      }
      
      //----------------------------------------------------------------------------
      
      ubyte GetByte(void) {
            register DWORD LocalReceiveBufferTail;
            register DWORD LocalReceiveBufferHead;
            register ubyte return_byte;
            
            LocalReceiveBufferTail = ReceiveBufferTail;
            LocalReceiveBufferHead = ReceiveBufferHead;
            
            if(LocalReceiveBufferTail != LocalReceiveBufferHead) {
              return_byte=SerialReceiveBuffer[LocalReceiveBufferTail];
              LocalReceiveBufferTail++;
              if(RECEIVEBUFFERSIZE <= LocalReceiveBufferTail) {
                LocalReceiveBufferTail = 0;
              }
            ReceiveBufferTail = LocalReceiveBufferTail;
            return(return_byte);
            }
              
      }  
      
      //----------------------------------------------------------------------------
      
      DWORD PeekBytesAvail(void) {
        register int LocalReceiveBufferHead;
        register int return_value;
        
        LocalReceiveBufferHead = ReceiveBufferHead;
        if((return_value = (LocalReceiveBufferHead) - (int)ReceiveBufferTailPeek) < 0) {
          return_value += RECEIVEBUFFERSIZE;
        }
        return(return_value);
      }
      
      //----------------------------------------------------------------------------
      
      void Sync_Peek_Pointer(void) {
        ReceiveBufferTailPeek = ReceiveBufferTail;
      }
      
      //----------------------------------------------------------------------------
      
      ubyte PeekByte(void) {
        register int LocalReceiveBufferTailPeek;
        register int LocalReceiveBufferHead;
        register ubyte return_byte;
        
        LocalReceiveBufferTailPeek = ReceiveBufferTailPeek;
        LocalReceiveBufferHead = ReceiveBufferHead;
        
        if(LocalReceiveBufferTailPeek != LocalReceiveBufferHead) {
          return_byte = SerialReceiveBuffer[LocalReceiveBufferTailPeek];
          LocalReceiveBufferTailPeek++;
          if(RECEIVEBUFFERSIZE <= LocalReceiveBufferTailPeek) {
            LocalReceiveBufferTailPeek = 0;
          }
          ReceiveBufferTailPeek = LocalReceiveBufferTailPeek;
        }
        return(return_byte);
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send image (.jpg) file over serial port
    By aSpareName in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-30-2009, 10:35 AM
  2. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 04:27 PM
  3. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 02:58 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21