📄 cmapicom.c
字号:
/* * File: cmApiCom.c * * Description: Communications Interface with the camera module. * * WARNING: This library is not thread-safe. * */#include "cmApiCom.h"#include <stdio.h>// global declarationsint com_debug = 0; /* debug flag */int delayMs = 0; // inter-character delay argumentint sendOption = 9;int breakOption = 1; // use break signal eventint g_baudrate = CBR_115200; /* baud rate for interface *//* * Function: cmComOpen * * Description: Open a communications port to the camera module * * The communications is done via an RS-232 serial port. * * Input Arguments: * comName_p Pointer to a zero terminated character string * that has an ASCII representation of the port * name. For example: "COM1". It has to be * one recognized by the Win32 API. * Output Arguments: * None. * Returns: * comHandle A handle required for all other interface * function. It references the port that was * opened. * * Design Notes: * - For the camera module, the timeout between characters should be * set to the break signal duration. However, this duration is * 12 bit times, which is about 100 us. we will use 1. */HANDLE cmComOpen (char *comName_p) { HANDLE comh; // communications port handle char bResult; DCB comPortDCB; // Device control block COMMTIMEOUTS comPortTimeout; if (com_debug > 0) printf ("Opening COM port %s\n", comName_p); comh = CreateFile ( comName_p, // device name GENERIC_WRITE|GENERIC_READ, // desired access 0, // exclusive access NULL, // security access OPEN_EXISTING, // creation disposition
0, NULL); if (comh == INVALID_HANDLE_VALUE) { return comh; } // Read in port parameters bResult = GetCommState (comh, &comPortDCB); if (!bResult) { printf ("Error Retrieving COM port info = %d\n", GetLastError()); CloseHandle (comh); return INVALID_HANDLE_VALUE; } if (com_debug > 0) { // debug print out of relevant values printf ("\n\nORIGINAL SERIAL PORT SETTINGS\n"); printf ("Baud Rate = %d\n", comPortDCB.BaudRate); printf ("Parity (0-4=no,odd,even,mark,space) = %d\n", comPortDCB.Parity); printf ("Stop Bit (0,1,2 = 1, 1.5, 2) = %d\n", comPortDCB.StopBits); printf ("Parity Checking Enable = %d\n", comPortDCB.fParity); printf ("Number of bits/byte = %d\n", comPortDCB.ByteSize); printf ("CTS output flow control Enable = %d\n", comPortDCB.fOutxCtsFlow); printf ("DSR output flow control Enable = %d\n", comPortDCB.fOutxDsrFlow); printf ("DTR flow control type = %d\n", comPortDCB.fDtrControl); printf ("XON/XOFF out flow control = %d\n", comPortDCB.fOutX); printf ("XON/XOFF in flow control = %d\n", comPortDCB.fInX); printf ("RTS flow control = %d\n", comPortDCB.fRtsControl); } // Put in new settings. // 19200, 8, N, 1 NO flow control (HW or SW) comPortDCB.BaudRate = g_baudrate; comPortDCB.ByteSize = 8; // bits/byte comPortDCB.fParity = FALSE; comPortDCB.Parity = NOPARITY; comPortDCB.StopBits = ONESTOPBIT; comPortDCB.fOutxCtsFlow = FALSE; comPortDCB.fOutxDsrFlow = FALSE; comPortDCB.fDtrControl = FALSE; comPortDCB.fAbortOnError = FALSE; comPortDCB.fOutX = FALSE; comPortDCB.fInX = FALSE; comPortDCB.fRtsControl = FALSE; if (com_debug > 0) printf ("Setting Serial Port Settings\n"); bResult = SetCommState (comh, &comPortDCB); if (!bResult) { printf ("Error Setting COM port info = %d\n", GetLastError()); CloseHandle (comh); return INVALID_HANDLE_VALUE; } if (com_debug > 0) { bResult = GetCommState (comh, &comPortDCB); if (!bResult) { printf ("Error Retrieving COM port info = %d\n", GetLastError()); CloseHandle (comh); return INVALID_HANDLE_VALUE; } printf ("\n\nNEW SERIAL PORT SETTINGS\n"); printf ("Baud Rate = %d\n", comPortDCB.BaudRate); printf ("Parity (0-4=no,odd,even,mark,space) = %d\n", comPortDCB.Parity); printf ("Stop Bit (0,1,2 = 1, 1.5, 2) = %d\n", comPortDCB.StopBits); printf ("Parity Checking Enable = %d\n", comPortDCB.fParity); printf ("Number of bits/byte = %d\n", comPortDCB.ByteSize); printf ("CTS output flow control Enable = %d\n", comPortDCB.fOutxCtsFlow); printf ("DSR output flow control Enable = %d\n", comPortDCB.fOutxDsrFlow); printf ("DTR flow control type = %d\n", comPortDCB.fDtrControl); printf ("XON/XOFF out flow control = %d\n", comPortDCB.fOutX); printf ("XON/XOFF in flow control = %d\n", comPortDCB.fInX); printf ("RTS flow control = %d\n", comPortDCB.fRtsControl); } // Port timeout parameters if (com_debug > 0) { printf ("Retrieving COM port timeouts.\n"); bResult = GetCommTimeouts (comh, &comPortTimeout); if (!bResult) { printf ("Error Retrieving COM port timeouts = %d\n", GetLastError()); CloseHandle (comh); return INVALID_HANDLE_VALUE; } printf ("COM PORT TIMEOUTS\n"); printf ("Write total timeout multiplier = %d\n", comPortTimeout.WriteTotalTimeoutMultiplier); printf ("Write total timeout constant = %d\n", comPortTimeout.WriteTotalTimeoutConstant); printf ("Read Interval timeout = %d\n", comPortTimeout.ReadIntervalTimeout); printf ("Read total timeout constant = %d\n", comPortTimeout.ReadTotalTimeoutConstant); printf ("Read total timeout multiplier = %d\n", comPortTimeout.ReadTotalTimeoutMultiplier); } // The following settings will cause ReadFile to return immediately. // Therefore, you must use the WaitEventComm // MAXDWORD/0/0 means that if there is no character in the // buffer, then the call returns immediately with 0 bytes read. //comPortTimeout.ReadIntervalTimeout = MAXDWORD; //comPortTimeout.ReadTotalTimeoutMultiplier = 0; //comPortTimeout.ReadTotalTimeoutConstant = 0; comPortTimeout.ReadIntervalTimeout = 20; /* ms */ comPortTimeout.ReadTotalTimeoutMultiplier = 1; /* ms */ comPortTimeout.ReadTotalTimeoutConstant = 1; /* ms */ comPortTimeout.WriteTotalTimeoutMultiplier = 0; comPortTimeout.WriteTotalTimeoutConstant = 0; bResult = SetCommTimeouts (comh, &comPortTimeout); if (!bResult) { printf ("Error Setting COM port timeouts = %d\n", GetLastError()); CloseHandle (comh); return INVALID_HANDLE_VALUE; }
SetCommMask (comh, EV_BREAK|EV_RXCHAR); return comh; // successful completion}/* * Function: cmComSend * * Description: Send a command packet to the camera module. * * Input Arguments: * comh The handle to the port returned by the m2ComOpen. * cmd_p A pointer to a zero terminated ASCII string that * represents the command. There is no limit on the * length, but it has to be zero terminated. * nb Number of bytes to write from the buffer. * * Output Arguments: * None. * * Returns: * +1 = Success * -1 = Error * */int cmComSend (HANDLE comh, unsigned char *cmd_p, int nb){ int length; // string length int i; // loop index unsigned long nBytes; char bResult;
char cbuf; //length = strlen (cmd_p); length = nb; if (com_debug == 5) printf ("Tx: "); // Write out the command string for (i = 0; i < length; ++i) { nBytes = 0; bResult = WriteFile (comh, &cmd_p[i], 1, &nBytes, NULL); if (!bResult) { printf ("cmComSend: Error writing to serial port = %d\n", GetLastError()); printf ("cmComSend: TERMINATING COMMAND.\n"); break; /* out of the loop */ } if (com_debug == 5) printf ("%3x ", cmd_p[i]); // inter-character delay //if (debug > 1) cout << "cmComSend: Delaying " << delayMs << "Ms" << endl; //Sleep (delayMs); } // end for // Write out CR and LF if (sendOption == cmcomioctl_send_opt_cr_lf) { cbuf = 0x0d; if (com_debug > 0) printf ("cmComSend: Sending <cr>\n"); bResult = WriteFile (comh, &cbuf, 1, &nBytes, NULL); if (!bResult) { printf ("cmComSend: Error writing to serial port = %d\n" , GetLastError()); printf ("cmComSend: TERMINATING COMMAND.%d\n"); goto quit_send; } //if (debug > 1) cout << "cmComSend: Delaying " << delayMs << "Ms" << endl; //Sleep (delayMs); cbuf = 0x0a; if (com_debug > 0) printf ("cmComSend: Sending <lf>\n"); bResult = WriteFile (comh, &cbuf, 1, &nBytes, NULL); if (!bResult) { printf ("cmComSend: Error writing to serial port = %d\n", GetLastError()); printf ("cmComSend: TERMINATING COMMAND.\n"); goto quit_send; } if (com_debug > 1) printf ("cmComSend: Delaying %d Ms\n"); Sleep (delayMs); }quit_send: if (com_debug == 5) printf ("\n"); return 0; // OK}/* * Function: cmComRecv * * Description: Read the response packet from the camera module * * Input Arguments: * * comh The handle to the port returned by the cmComOpen. * buffer_p Pointer to the buffer to read bytes into * bufferlen Size of the buffer in bytes * nbytes_p Number of bytes to read * * Output Arguments: * * buffer_p A pointer to a zero terminated ASCII string that * represents the response packet. * nbytes_p The number of bytes written * * Returns: * * +1 = Success
* -1 = Error */int cmComRecv (HANDLE comh, unsigned char *buffer_p, int bufferlen, int *nbytes_p){ int status = 1; BOOL bResult = 0; int i; if (com_debug == 5) printf ("Rx: "); bResult = ReadFile (comh, buffer_p, (unsigned long) bufferlen, (unsigned long *) nbytes_p, NULL); if (!bResult) { printf ("cmComRead: Error %d while reading\n", GetLastError()); status = -1; } if (com_debug == 5) { for (i = 0; i < *nbytes_p; ++i) { printf ("%3x ", buffer_p[i]); } fflush(NULL); } return status;}/* * Function: cmComClose * * Description: Close a commnications port to the camera module * * Input Arguments: * comh Communications handle obtained from call to * cmComOpen. * * Output Arguments: * * None. * * Returns: * Void */void cmComClose (HANDLE comh){ if (com_debug > 0) printf ("cmComClose: Closing handle. \n"); CloseHandle (comh);}/* * Function: cmComIoctl * * Description: Set up com parameters * * * Input Arguments: * * mode Read or Write. Currently ignored (only write * supported). * tag Enumerated value for the parameter. * value_p Pointer to an integer that contains the * value of the parameter. If the pointer is * Output Arguments: * * None. * * Returns: * +1 Success * -1 Failure */int cmComIoctl (int tag, int *value_p) { int status = 1; switch (tag) { case CMCOMIOCTL_TAG_BREAK: breakOption = *value_p; break; case CMCOMIOCTL_TAG_DBG: com_debug = *value_p; break; case CMCOMIOCTL_TAG_DELAY: delayMs = *value_p; break; case CMCOMIOCTL_TAG_SEND_OPT: sendOption = *value_p; break; default: // ignore break; } return status;} /* * Function: h16toa * * Description: Convert a 16-bit hex number to ASCII hex * * Input Arguments: * * n A 4-byte integer. Only the lower 2 bytes are significant. * * Output Arguments: * * buf A pointer to a character buffer that is at least 5 * characters long. The string that is returned is zero * terminated. * Returns: * VOID */const char hexstring[17] = "0123456789ABCDEF"; // for both functionsvoid h16toa (int n, char *buf){ buf[4] = '\0'; buf[3] = hexstring[(n & 0x0F)]; // low order nibble buf[2] = hexstring[((n >> 4) & 0x0F)]; // next nibble buf[1] = hexstring[((n >> 8) & 0x0F)]; // next nibble buf[0] = hexstring[((n >> 12) & 0x0F)]; // next nibble}/* * Function: h8toa * * Description: Convert a 8-bit hex number to ASCII hex * * Input Arguments: * * n A 4-byte integer. Only the lowest byte is significant. * * Output Arguments: * * buf A pointer to a character buffer that is at least 3 * characters long. The string that is returned is zero * terminated. * Returns: * VOID */void h8toa (int n, char *buf){ buf[2] = '\0'; buf[1] = hexstring[(n & 0x0F)]; // low order nibble buf[0] = hexstring[((n >> 4) & 0x0F)]; // next nibble}/* * Function: cmComSet * * Description: Set up Communication parameters * * Input parameters: * * Output parameters: * * Returns: */int cmComSet (int tag, int value){ int status = 1; switch (tag) { case CMCOM_TAG_BAUDRATE: switch (value) { case 4800: g_baudrate = CBR_4800; break; case 9600: g_baudrate = CBR_9600; break; case 14400: g_baudrate = CBR_14400; break; case 19200: g_baudrate = CBR_19200; break; case 38400: g_baudrate = CBR_38400; break; case 57600: g_baudrate = CBR_57600; break; case 115200: g_baudrate = CBR_115200; break; default: g_baudrate = CBR_115200; break; } break; default: /* ignore */ break; } return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -