Sending Byte to serial port - Page 4
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 5 FirstFirst 12345 LastLast
Results 46 to 60 of 73

Thread: Sending Byte to serial port

  1. #46
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Sending Byte to serial port

    Sorry, I wasn't specific. I meant not using printf("%s", dst).
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  2. #47
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Well! Thank you! This is a very good forum. I forgot to create
    a loop for each element in the buffer!
    I have finished my second step with success: encoding and decoding the packet! And thanks to "printf" I have verified that all works correctly!
    Now I can pass to the 3rd step.

  3. #48
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    One fast question...
    I'm implementing a 16bit CCITT CRC algorithm and I have found this sample:

    Code:
    unsigned int crc(unsigned char data)
    	 {
    		 static unsigned int crc;
    		
    		crc = (unsigned char)(crc >> 8) | (crc << 8);
    	        crc = data;
    		crc = (unsigned char)(crc & 0xff) >> 4;
    
    		crc = (crc << 8) << 4;
    	        crc = ((crc & 0xff) << 4) << 1;
    		
    		 return crc;
    		 }

    I'd like to understand how work the "^=" operator.

  4. #49
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,283

  5. #50
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    OK thank you. I didn't find http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx.
    Why the compiler gives the follow error:
    - syntax error: ';' before identifier ''^" ?

  6. #51
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,283

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    OK thank you. I didn't find http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx.
    Why the compiler gives the follow error:
    - syntax error: ';' before identifier ''^" ?
    Because you are using the wrong symbol "ˆ" instead of the "^"
    Quote Originally Posted by Bfrancesco View Post
    One fast question...
    I'm implementing a 16bit CCITT CRC algorithm and I have found this sample:

    Code:
    unsigned int crc(unsigned char data)
    	 {
    		 static unsigned int crc;
    		
    		crc = (unsigned char)(crc >> 8) | (crc << 8);
    	        crc = data;
    		crc ˆ= (unsigned char)(crc & 0xff) >> 4;
    
    		crc ˆ= (crc << 8) << 4;
    	        crc ˆ= ((crc & 0xff) << 4) << 1;
    		
    		 return crc;
    		 }

    I'd like to understand how work the "^=" operator.
    Victor Nijegorodov

  7. #52
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Sending Byte to serial port

    Because what looks like '^' in the code actually isn't ^ but some other symbol. Replace what looks like '^' with an actual ^ and it should then compile.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #53
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Yes, Thank you!

  9. #54
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Hi

    Now I have obtained the correct packet byte to send after coding and CRC calculation. This is: "01 02 04 01 02 09 0c 10 02 06 12 01 01 13 01 01 49 f1 00"
    Now I want to send it using the project in http://www.naughter.com/serialport.html. But i need to understand well the code of this project to send correctly. It is composed mainly by 2 files: app.ccp and SerialPort.cpp. In the SerialPort.cpp file there is the function:

    Code:
    void CSerialPort::Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten)
    and I think that it's the function that i must use to send the packet. In the app.ccp this function is called:

    Code:
     port2.Open(6, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
    
        CEvent event(FALSE, TRUE);
        OVERLAPPED overlapped;
        memset(&overlapped, 0, sizeof(overlapped));
        overlapped.hEvent = event;
        try
        {
          port2.Write(pBuf, 10000, overlapped);
    Now I'm thinking to add the following code:

    Code:
    char sBuf[] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
    	port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
        printf("pBuf %x, \n", sBuf);
    But i don't know if it's correct. Can you help me?

  10. #55
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,283

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    Now I'm thinking to add the following code:

    Code:
    char sBuf[] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
    	port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
        printf("pBuf %x, \n", sBuf);
    But i don't know if it's correct. Can you help me?
    But why don't you want to try? It would be much faster than write this post to the forum and then wait to the answers!
    Victor Nijegorodov

  11. #56
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    I'm trying but I don't know if it's correct, because i don't read any error when I compile, but none window appears to read the byte that I send...

  12. #57
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    This is the complete code:

    Code:
    /////////////////////////////////  Includes  //////////////////////////////////
    
    #include "stdafx.h"
    #include "SerialPort.h"
    
    
    ///////////////////////////////// Defines /////////////////////////////////////
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    //////////////////////////////// Implementation ///////////////////////////////
    
    class CSerialPortApp : public CWinApp
    {
    public:
      virtual BOOL InitInstance();
    };
    
    CSerialPortApp theApp;
    
    BOOL CSerialPortApp::InitInstance()
    {
      BYTE* pBuf = new BYTE[10000];
    
      try
      {
        COMMCONFIG config;
        CSerialPort::GetDefaultConfig(1, config);
    
        CSerialPort port;
        port.Open(1, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);
    	
        HANDLE hPort = port.Detach();
        port.Attach(hPort);
    
        DWORD dwModemStatus;
        port.GetModemStatus(dwModemStatus);
    
        DCB dcb;
        port.GetState(dcb);
    
        dcb.BaudRate = 9600;
        port.SetState(dcb);    
    
        DWORD dwErrors;                      
        port.ClearError(dwErrors);
    
        port.SetBreak();
        port.ClearBreak();
    
        COMSTAT stat;
        port.GetStatus(stat);
    
        DWORD dwBytesWaiting = port.BytesWaiting();
        dwBytesWaiting;
    
        COMMTIMEOUTS timeouts;
        port.GetTimeouts(timeouts);
    
        port.Setup(10000, 10000);
    
        port.GetConfig(config);
    
        config.dcb.BaudRate = 9600;
        port.SetConfig(config);
    
        port.Set0WriteTimeout();
        port.Set0ReadTimeout();
    
        //char sBuf[] = "This should appear on the serial port";
        //port.Write(sBuf, static_cast<DWORD>(strlen(sBuf)));
    
        DWORD dwMask;
        port.GetMask(dwMask);
    
        port.SetMask(EV_TXEMPTY); 
    
        //port.WaitEvent(dwMask);
    
        port.TerminateOutstandingWrites();
    
        port.TransmitChar('p');
    
        port.Set0Timeout();
    
        char sRxBuf[10];
        DWORD dwRead = port.Read(sRxBuf, 10);
        dwRead; //To remove unreferrenced variable in VC 6.
    
        port.TerminateOutstandingReads();
    
        port.ClearDTR();
    
        port.ClearRTS();
    
        port.SetDTR();
    
        port.SetRTS();
    
        port.SetXOFF();
    
        port.SetXON();
    
        COMMPROP properties;
        port.GetProperties(properties);
    
        port.ClearWriteBuffer();
    
        port.ClearReadBuffer();
    
        port.Flush();
    
        port.Close();
    
    
        //Try out the overlapped functions
        CSerialPort port2;
        port2.Open(6, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
    
        CEvent event(FALSE, TRUE);
        OVERLAPPED overlapped;
        memset(&overlapped, 0, sizeof(overlapped));
        overlapped.hEvent = event;
        try
        {
        //  port2.Write(pBuf, 10000, overlapped);
    		char sBuf[19] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
    		port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
    		printf("pBuf %x, \n", sBuf);
    		SleepEx(INFINITE, TRUE);
    		
    	//  printf("pBuf %x, \n", pBuf);
    	}
        catch(CSerialException* pEx)
        {
          if (pEx->m_dwError == ERROR_IO_PENDING)
          {
            DWORD dwBytesTransferred = 0;
            port2.GetOverlappedResult(overlapped, dwBytesTransferred, TRUE);
            pEx->Delete();
          }
          else
          {
            DWORD dwError = pEx->m_dwError;
            pEx->Delete();
            CSerialPort::ThrowSerialException(dwError);
          }
        }
        try
        {
          //port2.Read(pBuf, 10, overlapped);
    	  port2.Read(pBuf, 20, overlapped);
    	  SleepEx(INFINITE, TRUE);
    	  printf("pBuf %x, \n", pBuf);
    
    	
    	}
        catch(CSerialException* pEx)
        {
          if (pEx->m_dwError == ERROR_IO_PENDING)
          {
            DWORD dwBytesTransferred = 0;
            port2.GetOverlappedResult(overlapped, dwBytesTransferred, TRUE);
            pEx->Delete();
          }
          else
          {
            DWORD dwError = pEx->m_dwError;
            pEx->Delete();
            CSerialPort::ThrowSerialException(dwError);
          }
        }
              
        port2.SetMask(EV_TXEMPTY); 
    
    	/*char sBuf[19] = {0x01,0x02,0x04,0x01,0x02,0x09,0x0c,0x10,0x02,0x06,0x12,0x01,0x01,0x13,0x01,0x01,0x49,0xf1,0x00} ;
    	port2.Write(sBuf,sizeof(sBuf)/sizeof(unsigned char),overlapped );
        printf("pBuf %x, \n", sBuf);
    	SleepEx(INFINITE, TRUE);
    	port2.Read(pBuf, 20);
    	SleepEx(INFINITE, TRUE);
    	printf("pBuf %x, \n", pBuf);
    	*/
    	/* for testing on NT only
        port2.WriteEx(sBuf, static_cast<DWORD>(strlen(sBuf)));
        SleepEx(INFINITE, TRUE);
    	printf("pBuf %x, \n", sBuf);
        port2.ReadEx(pBuf, 10);
        SleepEx(INFINITE, TRUE);
        */
    
      }
      catch (CSerialException* pEx)
      {
        TRACE(_T("Handle Exception, Message:%s\n"), pEx->GetErrorMessage().operator LPCTSTR());
        pEx->Delete();
      }
    
      delete [] pBuf;
    
      return FALSE;
    }

  13. #58
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,283

    Re: Sending Byte to serial port

    But how did you do it sending non-encoded byte sequence to the serial port?
    Victor Nijegorodov

  14. #59
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    I'm trying to send encoded bytes (The codification with other code is made). I have understood that to stamp in the MFC application I must use "TRACE" and not "printf", but the real problem is that I'm not understing how i can handle the "BYTE*" and how i can use it in "port.write" to send the bytes.

  15. #60
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,283

    Re: Sending Byte to serial port

    Did you debug your code to be sure this write() function is called and your byte array is sent to the port?

    Quote Originally Posted by Bfrancesco View Post
    ... the real problem is that I'm not understing how i can handle the "BYTE*" and how i can use it in "port.write" to send the bytes.
    What "BYTE*" do you mean?
    Victor Nijegorodov

Page 4 of 5 FirstFirst 12345 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center