Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // v3.0 28/04/13 this version fixed the error of RS232 communication
- #include "RS232.h"
- using namespace std;
- HANDLE hComm;
- int RS232::RS232_close()
- {
- COM_flg = 0;
- CloseHandle(hComm);
- return 1;
- }
- //HANDLE RS232_open(const char *port)
- HANDLE RS232::RS232_open()
- {
- COM_flg = 1;
- TCHAR *pccomport=TEXT("COM1");
- hComm = CreateFile(pccomport,
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- 0,
- 0);
- if (hComm == INVALID_HANDLE_VALUE)//如果COM未開啟
- {
- cout << "can not open" << endl;
- cout <<endl;
- return 0;
- }
- else
- {
- cout << "opened " << endl;
- cout <<endl;
- }
- // ** Configuration
- DCB dcbConfig;
- if(GetCommState(hComm,&dcbConfig))
- {
- /*dcbConfig.BaudRate = CBR_38400;
- dcbConfig.ByteSize = 8;
- dcbConfig.Parity = NOPARITY;
- dcbConfig.StopBits = ONESTOPBIT;*/
- dcbConfig.BaudRate = CBR_38400; //CBR_19200; // ATTENTION!!! -> CBR_38400 can run with DENSO
- dcbConfig.ByteSize = 8;
- dcbConfig.Parity = NOPARITY;
- dcbConfig.StopBits = ONESTOPBIT;
- dcbConfig.fBinary = TRUE;
- dcbConfig.fParity = TRUE;
- } else
- {
- cout << "Get COM's status fail!!" << endl;
- }
- if (!SetCommState(hComm,&dcbConfig))
- {
- cout << "Set/Config COM fail!!" << endl;
- cout << endl;
- CloseHandle(hComm);
- return 0;
- }
- /*COMMTIMEOUTS time_out;
- time_out.ReadIntervalTimeout = MAXDWORD;
- time_out.ReadTotalTimeoutConstant=0;
- time_out.ReadTotalTimeoutMultiplier=0;
- time_out.WriteTotalTimeoutConstant=50;
- time_out.WriteTotalTimeoutMultiplier=5;
- SetCommTimeouts(hComm,&time_out);
- printf("setup and open com OK\n");
- cout <<endl;*/
- // ** Set TimeOuts
- COMMTIMEOUTS commTimeout;
- DWORD dwTimeOutInSec = 5;
- if(GetCommTimeouts(hComm, &commTimeout))
- {
- commTimeout.ReadIntervalTimeout = 1000 * dwTimeOutInSec;
- commTimeout.ReadTotalTimeoutConstant = 1000 * dwTimeOutInSec;
- commTimeout.ReadTotalTimeoutMultiplier = 0;
- commTimeout.WriteTotalTimeoutConstant = 1000 * dwTimeOutInSec;
- commTimeout.WriteTotalTimeoutMultiplier = 0;
- /*commTimeout.ReadIntervalTimeout = MAXDWORD;
- commTimeout.ReadTotalTimeoutConstant=0;
- commTimeout.ReadTotalTimeoutMultiplier=0;
- commTimeout.WriteTotalTimeoutConstant=0;
- commTimeout.WriteTotalTimeoutMultiplier=0;*/
- }
- else
- {
- cout << "Get COM's Timeout fail!!" << endl;
- }
- if(SetCommTimeouts(hComm, &commTimeout))
- cout << "Set COM's Timeout Succeed!!" << endl;
- else
- cout << "Set COM's Timeout fail!!" << endl;
- return hComm;
- }
- void RS232::RS232_write(char* buf)
- {
- cout<<" To RS232:";
- cout<<buf<<endl;
- /* evaluate the len of the buf */
- int n;
- int size = 0;
- while(buf[size] != 0)
- {
- size++;
- }/* End of while-loop */
- /* write the char to the RS232 channel */
- if(WriteFile(hComm, buf, size, (LPDWORD)((void *)&n), NULL))
- {
- //return(n);
- }
- //return 0;
- }
- void RS232::RS232_read()
- {
- unsigned char* ch=new unsigned char[1];
- int n;
- do
- {
- ReadFile(hComm, ch, 1, (LPDWORD)((void *)&n), NULL);
- }while (ch[0] != 's' && ch[0] != 'S');
- //return(n);
- //PurgeComm(hComm, PURGE_RXCLEAR);
- //ch=nullptr;
- delete[] ch;
- }
- RS232::RS232()
- {
- RS232_open();
- }
- RS232::~RS232()
- {
- CloseHandle(hComm); // close when finish communication OR there will be leak error!!
- }
- int RS232::Read()
- {
- RS232_read();
- return 1;
- }
- int RS232::Write(double* joints)
- {
- // reopen
- //if (!COM_flg)
- //{
- // RS232_open();
- //}
- double j1=joints[0];
- double j2=joints[1];
- double j3=joints[2];
- double j4=joints[3];
- double j5=joints[4];
- double j6=joints[5];
- double w=joints[6];
- double gripper = joints[7];
- //char joint[20];
- char *joint= new char[40];
- char *jdenso;
- char *input3="q\r";
- //char inp3[]="q\r";
- //input3=inp3;
- sprintf(joint, "%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f \r",j1,j2,j3,j4,j5,j6,w,gripper);
- //strcat(joint," \r");
- //jdenso=joint;
- //RS232_write(jdenso);
- RS232_write(joint);
- RS232_read();
- RS232_write(input3);
- //RS232_write(buf);
- // close then
- //RS232_close();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement