Advertisement
Guest User

new RS232.cpp

a guest
Jun 18th, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 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. NULL);
  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.  
  48. memset(&dcbConfig, 0, sizeof(dcbConfig)); /* clear the new struct */
  49. dcbConfig.DCBlength = sizeof(dcbConfig);
  50.  
  51. baudr=new char[64];
  52. strcpy(baudr, "baud=38400 data=8 parity=N stop=1");
  53. if(!BuildCommDCBA(baudr, &dcbConfig)) // Linh Tao uses this one!!!
  54. {
  55. printf("unable to set comport dcb settings\n");
  56. CloseHandle(hComm);
  57. //return(1);
  58. }
  59.  
  60. //if(GetCommState(hComm,&dcbConfig)) // He does not use GetCommState
  61. //{
  62. // /*dcbConfig.BaudRate = CBR_38400;
  63. // dcbConfig.ByteSize = 8;
  64. // dcbConfig.Parity = NOPARITY;
  65. // dcbConfig.StopBits = ONESTOPBIT;*/
  66. // dcbConfig.BaudRate = CBR_38400; //CBR_19200; // ATTENTION!!! -> CBR_38400 can run with DENSO
  67. // dcbConfig.ByteSize = 8;
  68. // dcbConfig.Parity = NOPARITY;
  69. // dcbConfig.StopBits = ONESTOPBIT;
  70. // dcbConfig.fBinary = TRUE;
  71. // dcbConfig.fParity = TRUE;
  72.  
  73. //} else
  74. //{
  75. // cout << "Get COM's status fail!!" << endl;
  76. //}
  77.  
  78. if (!SetCommState(hComm,&dcbConfig))
  79. {
  80. cout << "Set/Config COM fail!!" << endl;
  81. cout << endl;
  82. CloseHandle(hComm);
  83. //return 0;
  84. }
  85.  
  86. // ** Set TimeOuts
  87. COMMTIMEOUTS commTimeout;
  88. DWORD dwTimeOutInSec = 5;
  89.  
  90. if(GetCommTimeouts(hComm, &commTimeout))
  91. {
  92. commTimeout.ReadIntervalTimeout = 1000 * dwTimeOutInSec;
  93. commTimeout.ReadTotalTimeoutConstant = 1000 * dwTimeOutInSec;
  94. commTimeout.ReadTotalTimeoutMultiplier = 0;
  95. commTimeout.WriteTotalTimeoutConstant = 1000 * dwTimeOutInSec;
  96. commTimeout.WriteTotalTimeoutMultiplier = 0;
  97. }
  98. else
  99. {
  100. cout << "Get COM's Timeout fail!!" << endl;
  101. }
  102.  
  103. if(SetCommTimeouts(hComm, &commTimeout))
  104. cout << "Set COM's Timeout Succeed!!" << endl;
  105.  
  106. else
  107. cout << "Set COM's Timeout fail!!" << endl;
  108.  
  109. return hComm;
  110. }
  111.  
  112.  
  113. void RS232::RS232_write(char* buf)
  114. {
  115.  
  116. cout<<" To RS232:";
  117. cout<<buf<<endl;
  118.  
  119. /* evaluate the len of the buf */
  120. int n;
  121. int size = 0;
  122. while(buf[size] != 0)
  123. {
  124. size++;
  125. }/* End of while-loop */
  126.  
  127. /* write the char to the RS232 channel */
  128. if(WriteFile(hComm, buf, size, (LPDWORD)((void *)&n), NULL))
  129. {
  130. //return(n);
  131. }
  132. //return 0;
  133. }
  134.  
  135. void RS232::RS232_read()
  136. {
  137. unsigned char* ch=new unsigned char[1];
  138.  
  139. int n;
  140.  
  141. do
  142. {
  143. ReadFile(hComm, ch, 1, (LPDWORD)((void *)&n), NULL);
  144. }while (ch[0] != 's' && ch[0] != 'S');
  145.  
  146. //return(n);
  147. //PurgeComm(hComm, PURGE_RXCLEAR);
  148. //ch=nullptr;
  149. delete[] ch;
  150. }
  151.  
  152.  
  153. RS232::RS232()
  154. {
  155. RS232_open();
  156. }
  157.  
  158.  
  159. RS232::~RS232()
  160. {
  161. CloseHandle(hComm); // close when finish communication OR there will be leak error!!
  162. }
  163.  
  164.  
  165. int RS232::Read()
  166. {
  167.  
  168. RS232_read();
  169.  
  170. return 1;
  171. }
  172.  
  173.  
  174. int RS232::Write(double* joints)
  175. {
  176. // reopen
  177. //if (!COM_flg)
  178. //{
  179. // RS232_open();
  180. //}
  181.  
  182. double j1=joints[0];
  183. double j2=joints[1];
  184. double j3=joints[2];
  185. double j4=joints[3];
  186. double j5=joints[4];
  187. double j6=joints[5];
  188. double w=joints[6];
  189. double gripper = joints[7];
  190.  
  191. //char joint[20];
  192. char *joint= new char[40];
  193. strcpy(joint,"\0");
  194. char *jdenso;
  195. char *input3="q\r";
  196. //char inp3[]="q\r";
  197. //input3=inp3;
  198.  
  199. sprintf(joint, "%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f \r",j1,j2,j3,j4,j5,j6,w,gripper);
  200. //strcat(joint," \r");
  201. //jdenso=joint;
  202. //RS232_write(jdenso);
  203. RS232_write(joint);
  204. RS232_read();
  205. RS232_write(input3);
  206.  
  207. //RS232_write(buf);
  208.  
  209. // close then
  210. //RS232_close();
  211.  
  212. return 1;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement