Home > Back-end >  In C language, there is something wrong with the PC and PLC communications
In C language, there is something wrong with the PC and PLC communications

Time:11-28

I use VS2010C + + window procedure, do the two buttons to the PLC through a serial port to send data, an open Y0 - Y7, a closed Y0 - Y7, now is a problem, I pressed the button 1, opened, I press the button 2 won't shut down, want me to close the debug, open 2 can be turned off, press the button again but a button can't open, can only be used once, between two

CodePudding user response:

CodePudding user response:

CodePudding user response:

Can't open serial port 2 times, whether can do validation for hCom is empty

CodePudding user response:

Library files can use mature communication, more convenient:
http://blog.sina.com.cn/s/blog_16d7d3ecb0102x6wj.html

CodePudding user response:

How do I look the function of the two buttons are the same?

CodePudding user response:

Error in the port, can do a test, if the port is opened, is closed, or, if you open the closed to open, your program under the condition of the port is already open to open the same port will the wrong again, the best close port, use with the open again,

CodePudding user response:

You two button click respond to events were calling CreateFile inside to open the serial port, but did not close the serial port, button 1 performed successfully, and then execute button 2, inside the button 2 CreateFile function will fail, because of a serial port has been opened, so the return value hcom is an invalid handle, what do you do with an invalid handle WriteFile function parameters, can perform success didn't have a problem,
Solution:
Hcom is defined as global, add a button to open or close a serial port, inside the button response function according to the state decided to implement the open to close a serial port or serial interface, button 1 and 2 focus on sending data button, don't here a serial port opening and closing operation;

CodePudding user response:

But and PLC communication should have special libraries, happy times have ADS communication libraries, library is simpler,
  • Related