Home > Software engineering >  Urgent please! C about the hexadecimal serial port to send.
Urgent please! C about the hexadecimal serial port to send.

Time:11-06

Younger brother bought a digital servo and control board in a treasure, and use the usb to TTL serial port control, according to the seller for the agreement of the younger brother in vs2010 wrote a simple c + + the hexadecimal serial port to send the program as follows,
#include
#include
Int main ()
{
HANDLE hcom.

Hcom=CreateFile (COM2, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

If (hcom==INVALID_HANDLE_VALUE)
{
return -1;
}
SetupComm (hcom, 1024102, 4);
DCB DCB.

GetCommState (hcom, & amp; DCB);
DCB. BaudRate=9600;
DCB. ByteSize=8;
DCB. Parity=NOPARITY;
DCB. StopBits=1;
SetCommState (hcom, & amp; DCB);

Char data []={0 x55, 0 x55, 0 x08, 0 x03, 0 x01, 0 xe8, 0 x03, 0 x01, 0 xd0, 0 x07};

DWORD dwWrittenLen=0;

if(! WriteFile (hcom, data, 8, & amp; DwWrittenLen, NULL))
{
return -1;
}
The CloseHandle (hcom);
return 0;

}
Using this program can't work out control steering gear
To send data and select a serial port debug tool can achieve control effect
As shown in the figure below

Both data are according to the agreement, all is right, as why would appear in the associated Settings vs2010 cannot reach a serial port on the control effect, hope to get your answer

CodePudding user response:

Cannot reach the control effect of meaning, steering gear without any response, from existing data displayed on the usb to ttL module,

CodePudding user response:

Are you sure that you write your programs, hexadecimal data is correct??????
Is the string that was obtained from the edit box,, are you sure, you the transformed data is right??????

CodePudding user response:

Instructions are written according to their agreement, below is their agreement screenshot , according to the feedback control board, should indeed data sent you a wrong, this char data []={0 x55, 0 x55, 0 x08, 0 x03, 0 x01, 0 xe8, 0 x03, 0 x01, 0 xd0, 0 x07};
There is a problem?

CodePudding user response:

Application to send data and serial data,

CodePudding user response:

The serial assistants is already checked hexadecimal, so only send the data part, on the vs2010 0 x, serial assistants and vs2010 send data, but were written satisfies the requirement of agreement,

CodePudding user response:

if(! WriteFile (hcom, data, 8, & amp; DwWrittenLen, NULL)) should be 10 bytes

if(! WriteFile (hcom, data, 10, & amp; DwWrittenLen, NULL)) give it a try

CodePudding user response:

Caution is the ship, ten thousand,

CodePudding user response:

It is recommended to use portmon auxiliary software debugging serial communication program,

CodePudding user response:

, the program compiled in vs2010 appear above the warning message,

CodePudding user response:

X55 0 x55, 0, 0 x08, 0 x03, 0 x01, 0 xe8, 0 x03, 0 x01, 0 xd0, 0 x07};

You the data and the data in the serial port

CodePudding user response:

Take short lines to receive and send, it is good, take a look at what data is sent

CodePudding user response:

To send data length and data inconsistency
  • Related