Agree with the PC program how a command starts and ends. For a simple terminal test, a line ending is often an easy choice.
CDC is a stream of bytes. One PC message can arrive in several USB_Read() calls. Several short PIC replies can arrive together in one PC read.
Choose a clear way to mark each command: a newline, a fixed length, or a length field at the start. Keep an incomplete command until the rest arrives. Do not assume one USB packet always equals one command.
Send only the bytes you filled. This main-program procedure sends OK and a new line (CR/LF). Set USB_TX_SIZE to at least 4:
Proc SerialSendOK()
TXReport[0] = 79 ' O
TXReport[1] = 75 ' K
TXReport[2] = 13 ' CR
TXReport[3] = 10 ' LF
USB_WriteWait(4, 20)
EndProc
Check USB_Result after calling it. Sending all 64 bytes for a four-byte reply adds 60 unwanted bytes and may delay delivery by keeping bulk transfers full. A packet shorter than the configured maximum can complete a host bulk read promptly; host read lengths, timeouts and application buffering also affect when text appears.
For a response ending on an exact maximum-packet boundary, a zero-length packet can mark the USB transfer's end when the host needs that termination. After the last full packet is accepted, queue USB_WriteWait(0, 20) and check its result. This sends no application byte and is different from sending a zero-valued byte. The stack does not append this packet automatically. USB_Flush() waits for queued packets to leave; it does not add a zero-length packet or a text newline. Do not insert one after every full packet in a continuous stream unless your transfer protocol calls for it.
Created with the Personal Edition of HelpNDoc: Maximize Your Productivity with HelpNDoc's Efficient User Interface