By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,655 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,655 IT Pros & Developers. It's quick & easy.

Function call with HANDLE or string as parameter, difference

P: n/a
I have some issues with the code, I have managed to solve the problem but does not understand the difference.

This should be part of a boot loader where a external PC gets control over the modbus network and flashes every attached device with new FW.

Func1 is supposed to listen to a "magic" packet and then answer with an init sequence on the MB network to put the current master as slave. The last code snippet is a example of this.

Func2 is supposed to act as the new master in the system and sending new FW out.

The first code example fails as the init sequence is not received at the device.

The seccond code example works.

I can not see the reason why they should have different behaviour.

Expand|Select|Wrap|Line Numbers
  1. void func1(HANDLE hCom) {
  2.     /*
  3.      * Useful code with read/write to hCom
  4.      */
  5. }
  6.  
  7. void func2(HANDLE hCOM) {
  8.     /*
  9.      * Useful code with write to hCom
  10.      */
  11. }
  12.  
  13. int main(int argc, char **argv) {
  14.     HANDLE hCom;
  15.     char* ComPort = argv[1];
  16.     hCom = CreateComPort(ComPort);
  17.     func1(hCom);
  18.     func2(hCom);
  19.     CloseHandle(hCom);
  20. }
  21.  
Expand|Select|Wrap|Line Numbers
  1. void func1(char* ComPort) {
  2.     HANDLE hCom;
  3.     hCom = CreateComPort(ComPort);
  4.  
  5.     /*
  6.      * Useful code with read/write to hCom
  7.      */
  8.  
  9.     CloseHandle(hCom);
  10. }
  11.  
  12. void func2(char* ComPort) {
  13.     HANDLE hCom;
  14.     hCom = CreateComPort(ComPort);
  15.  
  16.     /*
  17.      * Useful code with write to hCom
  18.      */
  19.  
  20.     CloseHandle(hCom);
  21. }
  22.  
  23. int main(int argc, char **argv) {
  24.     char* ComPort = argv[1];
  25.     func1(ComPort);
  26.     func2(ComPort);
  27. }
  28.  
Expand|Select|Wrap|Line Numbers
  1. while (fSuccess == 0) {
  2.     ReadFile(hCom, buffer, sizeof(buffer), &NumberBytesRead, NULL);
  3.     ADUlen = NumberBytesRead;
  4.     if (mb_slave(254, &ADUlen, (unsigned char*) buffer)) {
  5.         NumberBytesToWrite = ADUlen;
  6.         fSuccess = WriteFile(hComMB, buffer, NumberBytesToWrite,
  7.                 &NumberBytesWritten, NULL);
  8.     }
  9. }
  10.  
Oct 22 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.