![]() |
Writing 4 bytes to COM Port
Hello all,
I have a device that has 8 switches (relay). This device has controller which is linked to COM Port. In order to manipulate switches first the device needs to be intialized with 4 bytes: 1 1 0 and the fourth is XOR of these first three, (0). So I need to send 1 1 0 0 in order to intialize this device. I've tried to open com port and write this as characthers: "00000001 00000001 00000000 00000000" - (no space between) There is also some kind of driver, a monitor tool that monitors traffic between PC and the device. This monitor after sending above string says that only 2 bytes are sent. The device will not respond until 4 bytes are sent. Does anybody have any clues that can be useful? I've searched everywhere but nothing to solve it, so I am trying my luck here :) Thanks in advance, asmith [added]: tried with c and yes it is char type |
What you mean by writing as characters? Do you mean C data type character, then make sure that you are not using string API
Visu |
Hi,
You must make own program with following windows API func. CreateFile(), SetCommTimeouts, SetCommMask, GetCommState, BuildCommDCB and finaly WriteFile to send data |
If you must only to write these 4 bytes to COMx, this can be done as synchronous write (no need for timeouts e.t.c).
In MS SDK (section WIN32 development, Communications) are some samples in C++ - do needed modifications, build and try. Search MSDN for this. Requierad parts are: Create file "COM1" with expected attributes - without Overlapped in sync. op. case, DCB modification (speed, parity ...) - GetCommState,SetCommState, Write data to created file, Close handle. Output data must be formatted as byte array. In your case, you can pass pointer to int (0x0101) variable, length =4 as data buffer for WriteFile. DWORD l = 0; //return value for really sent bytes DWORD buf = 0x101; // data block 01, 01, 00, 00 WriteFile(hCom, &buf, 4, &l, NULL); //send buffer to opened COM port or, if response isn't expected for (int i=3; i>=0; i--) TransmitCommChar(hCom, (BYTE) (i>>1)); this function works very well for comm init strings sending w/o response. If you must handle some incoming packets too, better would be use async. operations mode with multithreading, else this all can hang up while waiting response ... |
| All times are GMT +8. The time now is 06:45. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX