CodePudding user response:
CodePudding user response:
CodePudding user response:
Can't open serial port 2 times, whether can do validation for hCom is emptyCodePudding 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,