Serial Ports - Receive Packets

Hi Guys,

I have written a program that opens Serial Com Port 1 and sends packets to an LCD Screen. That is fine. But now i need to be able to receive packets from the LCD screen. Does anyone know how I can do this?

My send_packet() code
[code]
void send_packet(COMMAND_PACKET *packet)
{
ubyte i;
unsigned char buffer[20];
int index;
index = 0;
buffer[index++] = packet->command;
buffer[index++] = packet->data_length;
for(i = 0 ; i < packet->data_length ; i++)
buffer[index++] = packet->data[i];

packet->CRC.as_word =
get_crc((ubyte *)&outgoing_response,outgoing_response.data_length+2,0xFFFF);
buffer[index++] = packet->CRC.as_bytes[0];
buffer[index++] = packet->CRC.as_bytes[1];

DWORD bytes_written;
bytes_written = 0;

if(handle)
if(TRUE != WriteFile(handle,
buffer,
index,
&bytes_written,
NULL))
printf("WriteFile() failed!
");
}
[/code]outgoing_response is a packet that is defined by the manufacturers it contains command, data_length, data, CRC

This is my attempt at receiving a packet
[code]
void receive_packet(COMMAND_PACKET *packet)
{
ubyte i;
unsigned char readbuf[20];
unsigned char received[20];
int index;
index = 0;
DWORD bytes_read;
bytes_read = 0;
if(handle)
received[index++] = ReadFile(handle,
&readbuf,
index,
&bytes_read,
NULL);
printf("ReadFile() failed!
");
//readbuf[index++] = packet->command;
//readbuf[index++] = packet->data_length;
for(i = 0 ; i < packet->data_length ; i++)
readbuf[index++] = packet->data[i];

packet->CRC.as_word =
get_crc((ubyte *)&incoming_command,incoming_command.data_length+2,0xFFFF);
readbuf[index++] = packet->CRC.as_bytes[0];
readbuf[index++] = packet->CRC.as_bytes[1];




printf("%s
", received);
getchar();
getchar();
}
[/code]

This is the information from the manufacturers data sheet for the ping command

0 (0x00): Ping Command
The LCD will return the Ping Command to the host.
type = 0x00 = 010
valid data_length is 0 to 16
data[0-(data_length-1)] can be filled with any arbitrary data

The return packet is identical to the packet sent, except the type will be 0x40 (normal response, Ping Command):
type = 0x40 | 0x00 = 0x40 = 6410
data_length = (identical to received packet)
data[0-(data_length-1)] = (identical to received packet)

I am completely lost with this so any help is greatly appreciated
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories