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
- void func1(HANDLE hCom) {
- /*
- * Useful code with read/write to hCom
- */
- }
- void func2(HANDLE hCOM) {
- /*
- * Useful code with write to hCom
- */
- }
- int main(int argc, char **argv) {
- HANDLE hCom;
- char* ComPort = argv[1];
- hCom = CreateComPort(ComPort);
- func1(hCom);
- func2(hCom);
- CloseHandle(hCom);
- }
Expand|Select|Wrap|Line Numbers
- void func1(char* ComPort) {
- HANDLE hCom;
- hCom = CreateComPort(ComPort);
- /*
- * Useful code with read/write to hCom
- */
- CloseHandle(hCom);
- }
- void func2(char* ComPort) {
- HANDLE hCom;
- hCom = CreateComPort(ComPort);
- /*
- * Useful code with write to hCom
- */
- CloseHandle(hCom);
- }
- int main(int argc, char **argv) {
- char* ComPort = argv[1];
- func1(ComPort);
- func2(ComPort);
- }
Expand|Select|Wrap|Line Numbers
- while (fSuccess == 0) {
- ReadFile(hCom, buffer, sizeof(buffer), &NumberBytesRead, NULL);
- ADUlen = NumberBytesRead;
- if (mb_slave(254, &ADUlen, (unsigned char*) buffer)) {
- NumberBytesToWrite = ADUlen;
- fSuccess = WriteFile(hComMB, buffer, NumberBytesToWrite,
- &NumberBytesWritten, NULL);
- }
- }