Hi guys,
I am trying to write sotware in C to send text to an LCD screen from a Win XP machine through a Comm Port Does anyone know how to do this. I think I have set up the port I just have no idea how to send the packets.
Can anyone help??
This is a discussion on Comm Port - Send Packets within the C Programming forums, part of the General Programming Boards category; Hi guys, I am trying to write sotware in C to send text to an LCD screen from a Win ...
Hi guys,
I am trying to write sotware in C to send text to an LCD screen from a Win XP machine through a Comm Port Does anyone know how to do this. I think I have set up the port I just have no idea how to send the packets.
Can anyone help??
To make it easier I should supply my code to set up the port:
Code:#include <windows.h> #define RECEIVEBUFFERSIZE 4096 typedef ubyte; int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; char *pcCommPort = "COM1"; ubyte SerialReceiveBuffer[RECEIVEBUFFERSIZE]; DWORD ReceiveBufferHead; DWORD ReceiveBufferTail; DWORD ReceiveBufferTailPeek; //---------------------------------------------------------------------------- int Serial_Init(char *port, int baud_rate) { hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); if (hCom == INVALID_HANDLE_VALUE) { // Handle the error. printf ("CreateFile() failed with error %d.\n", GetLastError()); return (1); } // Build on the current configuration, and skip setting the size // of the input and output buffers with SetupComm. fSuccess = GetCommState(hCom, &dcb); if (!fSuccess) { // Handle the error. printf ("GetCommState() failed with error %d.\n", GetLastError()); hCom = 0; return (2); } // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit. dcb.BaudRate = baud_rate; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit dcb.fRtsControl = RTS_CONTROL_ENABLE; dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fAbortOnError = FALSE; fSuccess = SetCommState(hCom, &dcb); if (!fSuccess) { // Handle the error. printf ("SetCommState() failed with error %d.\n", GetLastError()); return (3); } COMMTIMEOUTS our_timeouts; if(TRUE != GetCommTimeouts ( hCom, &our_timeouts)) { hCom = 0; printf("GetCommTimeouts() failed with error %d.\n", GetLastError()); return(4); } our_timeouts.ReadIntervalTimeout = 2000; our_timeouts.ReadTotalTimeoutMultiplier = 2000; our_timeouts.ReadTotalTimeoutConstant = 2000; if(TRUE != SetCommTimeouts( hCom, &our_timeouts)) { hCom = 0; printf("SetCommTimeouts() failed with error %d.\n", GetLastError()); return(5); } void EmptyReceiveBuffer(void); printf ("Serial port %s successfully reconfigured.\n", pcCommPort); return (0); } //---------------------------------------------------------------------------- void Uninit_Serial() { CloseHandle(hCom); hCom = NULL; } //---------------------------------------------------------------------------- void Sync_Read_Buffer_Void(void) { ubyte Incoming[4096]; DWORD BytesRead; COMSTAT status; DWORD errors; DWORD i = 0; int j = 10; if(!hCom) return; ClearCommError( hCom, &errors, &status); if(status.cbInQue) { if(!ReadFile(hCom, (LPVOID *)Incoming, status.cbInQue, &BytesRead, NULL)) printf("ReadFile() failed with error %d.\n", GetLastError()); else if(i < BytesRead) { SerialReceiveBuffer[ReceiveBufferHead] = Incoming[i]; ReceiveBufferHead++; if(RECEIVEBUFFERSIZE <=ReceiveBufferHead) { ReceiveBufferHead = 0; } i++; } } } //---------------------------------------------------------------------------- DWORD BytesAvail(void) { register int LocalReceiveBufferHead; register int return_value; LocalReceiveBufferHead = ReceiveBufferHead; if((return_value=(LocalReceiveBufferHead) - (int)ReceiveBufferTail) < 0) return_value += RECEIVEBUFFERSIZE; return(return_value); } //---------------------------------------------------------------------------- ubyte GetByte(void) { register DWORD LocalReceiveBufferTail; register DWORD LocalReceiveBufferHead; register ubyte return_byte; LocalReceiveBufferTail = ReceiveBufferTail; LocalReceiveBufferHead = ReceiveBufferHead; if(LocalReceiveBufferTail != LocalReceiveBufferHead) { return_byte=SerialReceiveBuffer[LocalReceiveBufferTail]; LocalReceiveBufferTail++; if(RECEIVEBUFFERSIZE <= LocalReceiveBufferTail) { LocalReceiveBufferTail = 0; } ReceiveBufferTail = LocalReceiveBufferTail; return(return_byte); } } //---------------------------------------------------------------------------- DWORD PeekBytesAvail(void) { register int LocalReceiveBufferHead; register int return_value; LocalReceiveBufferHead = ReceiveBufferHead; if((return_value = (LocalReceiveBufferHead) - (int)ReceiveBufferTailPeek) < 0) { return_value += RECEIVEBUFFERSIZE; } return(return_value); } //---------------------------------------------------------------------------- void Sync_Peek_Pointer(void) { ReceiveBufferTailPeek = ReceiveBufferTail; } //---------------------------------------------------------------------------- ubyte PeekByte(void) { register int LocalReceiveBufferTailPeek; register int LocalReceiveBufferHead; register ubyte return_byte; LocalReceiveBufferTailPeek = ReceiveBufferTailPeek; LocalReceiveBufferHead = ReceiveBufferHead; if(LocalReceiveBufferTailPeek != LocalReceiveBufferHead) { return_byte = SerialReceiveBuffer[LocalReceiveBufferTailPeek]; LocalReceiveBufferTailPeek++; if(RECEIVEBUFFERSIZE <= LocalReceiveBufferTailPeek) { LocalReceiveBufferTailPeek = 0; } ReceiveBufferTailPeek = LocalReceiveBufferTailPeek; } return(return_byte); }