Advertisement
Guest User

RS232.cpp

a guest
Jun 16th, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. // v3.0 28/04/13 this version fixed the error of RS232 communication
  2.  
  3. #include "RS232.h"
  4.  
  5. using namespace std;
  6.  
  7. HANDLE hComm;
  8.  
  9.  
  10. int RS232::RS232_close()
  11. {
  12.     COM_flg = 0;
  13.  
  14.     CloseHandle(hComm);
  15.  
  16.     return 1;
  17. }
  18.  
  19.  
  20. //HANDLE RS232_open(const char *port)
  21. HANDLE RS232::RS232_open()
  22. {
  23.     COM_flg = 1;
  24.  
  25.     TCHAR *pccomport=TEXT("COM1");
  26.     hComm = CreateFile(pccomport,
  27.                        GENERIC_READ | GENERIC_WRITE,
  28.                        0,
  29.                        NULL,
  30.                        OPEN_EXISTING,
  31.                        0,
  32.                        0);
  33.     if (hComm == INVALID_HANDLE_VALUE)//如果COM未開啟
  34.     {
  35.         cout << "can not open" << endl;
  36.         cout <<endl;
  37.         return 0;
  38.     }
  39.     else
  40.     {
  41.         cout << "opened " << endl;
  42.         cout <<endl;
  43.     }
  44.    
  45.     // ** Configuration
  46.     DCB dcbConfig;
  47.     if(GetCommState(hComm,&dcbConfig))
  48.     {
  49.         /*dcbConfig.BaudRate = CBR_38400;
  50.         dcbConfig.ByteSize = 8;
  51.         dcbConfig.Parity = NOPARITY;
  52.         dcbConfig.StopBits = ONESTOPBIT;*/
  53.         dcbConfig.BaudRate = CBR_38400; //CBR_19200;     // ATTENTION!!! -> CBR_38400 can run with DENSO
  54.         dcbConfig.ByteSize = 8;
  55.         dcbConfig.Parity = NOPARITY;
  56.         dcbConfig.StopBits = ONESTOPBIT;
  57.         dcbConfig.fBinary = TRUE;
  58.         dcbConfig.fParity = TRUE;
  59.  
  60.     } else
  61.     {
  62.         cout << "Get COM's status fail!!" << endl;
  63.     }
  64.  
  65.     if (!SetCommState(hComm,&dcbConfig))
  66.     {
  67.         cout << "Set/Config COM fail!!" << endl;
  68.         cout << endl;
  69.         CloseHandle(hComm);
  70.         return 0;
  71.     }
  72.  
  73.     /*COMMTIMEOUTS time_out;
  74.                  time_out.ReadIntervalTimeout = MAXDWORD;
  75.                  time_out.ReadTotalTimeoutConstant=0;
  76.                  time_out.ReadTotalTimeoutMultiplier=0;
  77.                  time_out.WriteTotalTimeoutConstant=50;
  78.                  time_out.WriteTotalTimeoutMultiplier=5;
  79.  
  80.     SetCommTimeouts(hComm,&time_out);
  81.                 printf("setup and open com OK\n");
  82.                 cout <<endl;*/
  83.  
  84.     // ** Set TimeOuts
  85.     COMMTIMEOUTS commTimeout;
  86.     DWORD dwTimeOutInSec = 5;
  87.  
  88.     if(GetCommTimeouts(hComm, &commTimeout))
  89.     {
  90.         commTimeout.ReadIntervalTimeout     = 1000 * dwTimeOutInSec;
  91.         commTimeout.ReadTotalTimeoutConstant     = 1000 * dwTimeOutInSec;
  92.         commTimeout.ReadTotalTimeoutMultiplier     = 0;
  93.         commTimeout.WriteTotalTimeoutConstant     = 1000 * dwTimeOutInSec;
  94.         commTimeout.WriteTotalTimeoutMultiplier = 0;
  95.  
  96.         /*commTimeout.ReadIntervalTimeout = MAXDWORD;
  97.         commTimeout.ReadTotalTimeoutConstant=0;
  98.         commTimeout.ReadTotalTimeoutMultiplier=0;
  99.         commTimeout.WriteTotalTimeoutConstant=0;
  100.         commTimeout.WriteTotalTimeoutMultiplier=0;*/
  101.     }
  102.     else
  103.     {
  104.         cout << "Get COM's Timeout fail!!" << endl;
  105.     }
  106.  
  107.     if(SetCommTimeouts(hComm, &commTimeout))
  108.         cout << "Set COM's Timeout Succeed!!" << endl;
  109.  
  110.     else
  111.         cout << "Set COM's Timeout fail!!" << endl;
  112.  
  113.  
  114.     return hComm;
  115. }
  116.  
  117.  
  118. void RS232::RS232_write(char* buf)
  119. {
  120.  
  121.     cout<<" To RS232:";
  122.     cout<<buf<<endl;
  123.  
  124.         /* evaluate the len of the buf */
  125.     int n;
  126.     int size = 0;
  127.     while(buf[size] != 0)
  128.     {
  129.         size++;
  130.     }/* End of while-loop */
  131.        
  132.      /* write the char to the RS232 channel */
  133.     if(WriteFile(hComm, buf, size, (LPDWORD)((void *)&n), NULL))
  134.     {
  135.         //return(n);
  136.     }
  137.     //return 0;
  138. }
  139.  
  140. void RS232::RS232_read()
  141. {
  142.     unsigned char* ch=new unsigned char[1];
  143.  
  144.     int n;
  145.  
  146.     do
  147.     {
  148.         ReadFile(hComm, ch, 1, (LPDWORD)((void *)&n), NULL);
  149.     }while (ch[0] != 's' && ch[0] != 'S');
  150.  
  151.     //return(n);
  152.     //PurgeComm(hComm, PURGE_RXCLEAR);
  153.     //ch=nullptr;
  154.     delete[] ch;
  155. }
  156.  
  157.  
  158. RS232::RS232()
  159. {
  160.     RS232_open();
  161. }
  162.  
  163.  
  164. RS232::~RS232()
  165. {
  166.     CloseHandle(hComm);  // close when finish communication OR there will be leak error!!
  167. }
  168.  
  169.  
  170. int RS232::Read()
  171. {
  172.  
  173.     RS232_read();
  174.  
  175.     return 1;
  176. }
  177.  
  178.  
  179. int RS232::Write(double* joints)
  180. {
  181.     // reopen
  182.     //if (!COM_flg)
  183.     //{
  184.     //  RS232_open();
  185.     //}
  186.  
  187.     double j1=joints[0];  
  188.     double j2=joints[1];
  189.     double j3=joints[2];
  190.     double j4=joints[3];
  191.     double j5=joints[4];
  192.     double j6=joints[5];
  193.     double  w=joints[6];  
  194.     double gripper = joints[7];
  195.  
  196.     //char joint[20];
  197.     char *joint= new char[40];
  198.     char *jdenso;
  199.     char *input3="q\r";
  200.     //char inp3[]="q\r";
  201.     //input3=inp3;
  202.    
  203.     sprintf(joint, "%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f \r",j1,j2,j3,j4,j5,j6,w,gripper);
  204.     //strcat(joint," \r");
  205.     //jdenso=joint;
  206.     //RS232_write(jdenso);
  207.     RS232_write(joint);
  208.     RS232_read();
  209.     RS232_write(input3);
  210.  
  211.     //RS232_write(buf);
  212.  
  213.     // close then
  214.     //RS232_close();
  215.  
  216.     return 1;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement