The communication format of the computer and PLC and implementation method
- Time:
- Click:155
- source:SEENER CNC Machining
GetCommState(m_hIDComDev of DCB1ength=sizeof(DCB); , &dcb);// obtains communication resource to install Dcb currently.
Rate of BaudRate=9600;// set baud is Dcb of 9600 .
ByteSize=7;//7 data Dcb.
Parity=2;// occasionally Dcb of desired result .
StopBits=0;// set stops a If(SetCommState(m_hIDComDev 1 times, else Return(FALSE);// of &dcb)return(TRUE); installs port, if install a success to return TRUE, what return FALSE } to need to explain otherwise is CSerial be be used at serial communication kind, it included the needs function that has serial news report. Outside dividing Open of afore-mentioned function of port initialization member, still include additional function of two important members: One is EndData, transmit data to serial port from a buffer. Another is ReadData, the reception from port reads in data in buffer. Next, 4 kinds of afore-mentioned operations have in every before a kind of medium operation, have handclasp contact even. To PLC code of ENQ(of mark of hair request question is OX05) , the response that reads PLC next question mark. The response that if read,arrives question mark is OX06) for ACK(code, express PLC already be all set, wait to receive communication data. Language of + of + of handclasp contact VC shows PLC already be all set, wait to receive communication data. Implementation of language of + of + of handclasp contact VC is: BOOL CNTJD1g: ReadFromPLC(char *Read_char Char *Read_address, if(Serial of kind of of of Char Read_BUFFER; that CSerial Serial;// of of Int Read_bytes){ uses at serial communication.
Open(2)// initialization is serial Serial of of {of of of COM2 of buccal communication mouth.
SendData(&ENQ_request, 1);// sends Sleep(1000);// of of of contact question mark to await Serial 1 second.
ReadData(&read_BUFFER, 1);// is read take PLC to answer If(read_BUFFER of question mark =={of ACK) if PLC answers question mark to be equal to ACK, have 4 kinds of afore-mentioned operations: Serial of}} .
After Close():// operation completes, shut of communication mouth} mouth of 3 process designing command operation (1) component or word component condition read operation operand component: Component of the X of PLC interior, Y, M, S, T, C, D; Order form: Explain: ① commands initiative mark STX to read, code is OX02; ② is a component or word component condition reads command CMDO, command code is OX30; ③ is 4 when read a component or word component initiative addresses, perch is sent first, low late, and it is the form that piles up with ASCII is sent; ④ takes a number of a component or word component to be read, most readable take OXff the component of byte, the form that piles up with ASCII is sent; ⑤ is stop a mark ETX, code is OX03; ⑥ is mixed for 2 desired result, it is ② , ③ , ④ with accumulative total code, take its and minimum two are changed into ASCII code, perch is sent first, low late. Nextpage is in after sending afore-mentioned order form code, can read the news that takes PLC to answer directly. Answer news format to be as follows: Language of VC + + comes true: BOOL CNTJDlg: ReadFromPLC(char *Read_char Char*Read_address, serial of of Int I; of of of Int Datasum_check=0; of of of Char Readdatasum_check[2];int Readdata_sum; of of of Char Senddatasum_CHECK[2];char Readdatasum_CHECK[2]; Char Total_DATABYTES[2]; of of Int Read_bytes){ .
SendData(&STX_start, 1);/ is sent to PLC " begin " Serial of mark code .
SendData(&CMDO_read, 1);// is sent " read " For(i=0;i<4;i++){Serial of of of command code Datasum_check+=CMDO_read; .
SendData(&Read_address[i] , the Change To Asci(total DATABYTES of of of ASCII code Datasum_check+=Read_address[i];} that 1);// delivers initiative component address, read_bytes);// changes byte number into For (i=0;i<2;i++){Serial of ASCII code .
SendData(&total_DATABYTES[i] , the ASCII code that 1);// sends component byte to count) Serial of Datasum_check+total_DATABYTES[i];} .
SendData(&ETX_end, 1);// is sent " end " Change_to_asci(senddatasum_CHECK of of of mark code Senddatasum_CHECK+ETX_end; , senddatasum_CHECK);// will " and " change into ASCII to pile up For (i=0;i<2;i++) Serial.
SendData(&senddatasum_CHECK[i] , sleep(1000);// of 1); awaits PLC to answer Serial.
ReadData(&read_BUFFER, if(read_BUFFER of 1); ==For(i=0;i<2*Read_bytes;i++){Serial of of of of of Readdata_sum=0; of of of STX_start){ .
ReadData(&Read_char[i] , 1);// reads Read_bytes Serial of of of of byte Readdata_sum+Read_char[i];} .
ReadData(&read_BUFFER, if(read_BUFFER of of of 1); ==ETX_end){Serial.
ReadData(readdatasum_CHECK, 2);// reads in " and " low Change_to_asci(readdatasum_check of of of of Readdata_sum+=ETX_end;} of 2 ASCII code, readdata_sum);// receives consideration " and " change into ASCII to pile up If(*readdatasum_CHECK==*Readdatasum_check)// " and " AfxMessageBox(of of {of desired result " data numerates successful! " ) AfxMessageBox(of Else {of of of Return TRUE;} " desired result mistake " ) Return FALSE.
}} (2) component or word component condition keep operation operand component: With 3(1) ; Order form: Explain: ① commands initiative mark STX to write, code is OX02; ② is a component or word component condition writes command CMD1, command code is OX31; ③ is 4 when keep a component or word component initiative addresses, perch is sent first, low late, and it is the form that piles up with ASCII is sent; ④ keeps a number of a component or word component for, the form that piles up with ASCII is sent; ⑤ writes the data DATA of PLC RAM area to wait for, with ASCII code form is sent; ⑥ is stop a mark ETX, code is 2 for ⑦ of of OX03; with desired result, it is ② , ③ , ④ with accumulative total code, take its and minimum two are changed into ASCII code, perch is sent first, low late. Language of VC + + comes true: BOOL CNTJDlg: WritePLC(char *data_ADDRESS, char *Write_ASC, serial of of of of Int Datasum_check=0; Int I=0; of of of of Char Read_BUFFER;char Read_finishBUFFER; of of of of Char Total_BYTES[2];char Senddatasum_CHECK[2]; of of of of {of of Int Bytesnumber) .
SendData(&STX_start, 1);// is sent to PLC " begin " Datasum_check=0;Serial of of mark code .
SendData(&CMD1_write, 1);// is sent " write " For(i=0;i<4;i++) {Serial of of of of Datasum_check+CMD1_write; of of command code .
SendData(&data_ADDRESS[i] , the ASCII that 1);// delivers initiative component address piles up Change_to_asci(total_DATABYTES of of of of of Datasum_check+=data_ADDRESS[i]; , bytesnumber);// changes byte number into ASCII to pile up Serial of of of of {of of For(i=0;i<2;i++) .
SendData(&total_BYTES, the of of of Datasum_check+=total_BYTES[i];} of of ASCII code that 1);// sends component byte to count Serial of of of of Int Sum=0; of of of of Char Read_buffer; of of of of Syn_Check[2]; of │ of Char of of of of I; of of {Int of of of For {i=0;inOperation (char *ON_Address) .
SendData(&STX_start, 1);// is sent to PLC " begin " Serial of of mark code .
SendData(&CMD7_ForceON, 1);// is sent " ON " For (i=0; I<4; I++) {Serial of of of of Sum+=CMD7 ForceON; of of command code .
SendData(&ON_Address[i] , the ASCII that 1)// delivers initiative component address piles up Serial of of of of } of Sum+=ON_Address[i]; .
SendData(&ETX_end, 1);// is sent " end " Change_to_asci(Sum_Check of of of of mark code Sum+=ETX_end; , sum);// will " and " change into ASCII to pile up Serial.
SendData(&Sum_Check, serial of of of of Sleep(1000); of of 2); .
ReadData(&read_buffer, if(read_finishBUFFER of of 1); ==ACK_reply) AfxMessageBox(" ON operates OK " ) ; Else AfxMessageBox(" ON handles failure " ) . } (4) L of operand of compulsive OFF operation is the same as component 3(3) ; Order form: Explain: ① commands initiative mark STX for compulsive OFF, code is OX02; ② commands CMD8 for compulsive OFF, command code is OX38H; ③ is compulsive OFF component 4 initiative addresses, perch is sent first, low late, with ASCII code form is sent; ④ is stop a mark ETX, code is OX03; ⑤ is mixed for 2 desired result, it is ② , ③ , ④ with accumulative total code, take its and minimum two are changed into ASCII code, perch is sent first, low late. Language of VC + + comes true: Void NTJDlg: Serial of of of Int Sum=0; of of of Char Read_buffer; of of of Char Sum_Check[2]; of of Int I; of of of {of of of ForceOffOperation (char *OFF_Address) .
SendData(&STX_start, 1);// is sent to PLC " begin " Serial of mark code .
SendData(&CMD8_ForceOFF, 1);// is sent " OFF " Serial of of of of For (i=0;i<4;i++) {of of of Sum=CMD8_ForceOFF; of command code .
SendData(&OFF_Address[i] , the ASCII that 1);// delivers initiative component address piles up Serial of of of Sum+=OFF_Address[i];} .
SendData(&ETX_end, 1);// is sent " end " Change_to_asci(Sum_Check of of of Sum+=ETX_end; of mark code , sum);// will " and " change into ASCII to pile up Serial.
SendData(&Sum_Check, serial of of of Skeeo(1000); of 2); .
ReadData(&read_buffer, if(read_fininhBUFFER of 1); ==ACK_reply) AfxMessageBox(" OFF operates OK " ) ; Else AfxMessageBox(" OFF handles failure " ) . } notices: Must command according to 4 kinds of afore-mentioned operations the format undertakes sending strictly, before send, initiative address, data, data number, desired result and must positional changeover is piled up into ASCII. The data that reads from PLC also is ASCII code form, need to be used through changing ability appropriately. Additional, like wanting to notice to command address and the order that read address are not compulsively, and can convey only at most 64 byte data. 4 conclusion use orders of afore-mentioned 4 kinds of operations, can be opposite the RAM area data of PLC undertakes administrative operating. Under management of the in a subtle way of working state offer PLC computer. On this foundation, the user can apply VC very the interface of PLC man-machine interface that designs oneself conveniently, move with what manage PLC for monitoring provide a kind of good means. CNC Milling CNC Machining