Problem:
No więc ostatnio zetknąłem się z takim problemem - mam napisać aplikację do komunikacji po 'porcie szeregowym' (USB-> CAN) narzuconą dll do wykorzystania z jej konstrukcji wiem jedynie tyle co jest napisane w pliku nagłówkowym:
/*************************************************************************************/
/* UART.DLL */
/* The UART.DLL si the dynamic linking library(DLL)designed for Windows 95/98 */
{...definy...}
EXPORTS WORD CALLBACK Get_Uart_Version(void);
EXPORTS WORD CALLBACK Open_Com(unsigned char cPort, DWORD dwBaudrate, char cData, char cParity, char cStop);
EXPORTS WORD CALLBACK Change_Baudrate(unsigned char cPort, DWORD dwBaudrate);
EXPORTS WORD CALLBACK Change_Config(unsigned char cPort, DWORD dwBaudrate, char cData, char cParity, char cStop);
EXPORTS int CALLBACK Change_ParityErrorCheck(int port, int mode, char cErrorChar);
EXPORTS WORD CALLBACK Get_Com_Status(unsigned char cPort);
/********************************Get_Com_Status ***************************/
/* cPort: 1~255 */
/* Return: 0 ==> COM port not in use */
/* 1 ==> COM port in use */
/**************************************************************************/
EXPORTS BOOL CALLBACK Close_Com(unsigned char cPort);
EXPORTS WORD CALLBACK Send_Receive_Cmd(unsigned char cPort, char szCmd[], char
szResult[], WORD wTimeOut, WORD wChksum, WORD *wT);
/********************************Send_Receive_Cmd ****************************************/
/* cPort: 1~255 */
/* szCmd[]: 1024 Bytes maximum, without zero (0x0D) character. */
/* szResult[]: 1024 Bytes maximum, with one zero or 0x0D terminal character. */
/* wTimeOut: Timeout for receiving result string. */
/* Unit: ms (1/1000 second) */
/* wChksum: 0 ==> add one 0x0D byte to the end of the szCmd */
/* <>0 ==> add two check sum bytes and one 0x0D byte to the end of the szCmd*/
/* *wT return a reference number for identify the performance. */
/* *wT --> 0 :good */
/*****************************************************************************************/
EXPORTS WORD CALLBACK Send_Receive_UserCmd(unsigned char cPort, char szCmd[],
char szResult[], WORD wTimeOut, WORD wChksum, WORD *wT,char sUserString[],int iUserLen);//@35
/********************************Send_Receive_Cmd ****************************************/
/* cPort: 1~255 */
/* szCmd[]: 1024 Bytes maximum, without zero (0x0D) character. */
/* szResult[]: 1024 Bytes maximum, with one zero or 0x0D terminal character. */
/* wTimeOut: Timeout for receiving result string. */
/* Unit: ms (1/1000 second) */
/* wChksum: 0 ==> add one 0x0D byte to the end of the szCmd */
/* <>0 ==> add two check sum bytes and one 0x0D byte to the end of the szCmd*/
/* *wT return a reference number for identify the performance. */
/* *wT --> 0 :good */
/* sUserString: The user-defined terminal string */
/* iUserLen: The length of theuser-defined terminal string */
/* Note: The length of szCmd and sUserString string cannot over 1024 bytes */
/*****************************************************************************************/
EXPORTS WORD CALLBACK Send_Cmd(unsigned char cPort, char szCmd[],WORD wChksum);
EXPORTS WORD CALLBACK Receive_Cmd(unsigned char cPort, char szResult[], WORD wTimeOut, WORD wChksum, WORD *wT);
/********************* Receive_Cmd ***********************************/
/*This function only sends a command string to DCON series modules */
/*********************************************************************/
EXPORTS WORD CALLBACK Send_Binary(unsigned char cPort, char szCmd[],int iLen);
EXPORTS WORD CALLBACK Receive_Binary(unsigned char cPort, char szResult[], WORD wTimeOut, WORD wLen, WORD *wT);
/********************** Receive_Binary *******************************/
/* This function is applied to receive the fix length response */
/* To get response that not with 0x0d(CR) end. */
/* The function will recive wLen bytes. */
/*********************************************************************/
EXPORTS WORD CALLBACK ReadComn(unsigned char cPort, char szResult[], WORD wmaxLen, WORD *wT);
/**************************** ReadComn ************************************/
/* This function is applied to receive the multi-bytes response from */
/* COM port */
/* cPort: 1~255 */
/* szResult[]: Pointer to the buffer that receives the data read */
/* from COM port. */
/* wmaxLen: Number of bytes to be read from COM port */
/* *wT: return a reference number for identify the performance. */
/* */
/* Return: The number of bytes actually read. */
/**************************************************************************/
EXPORTS DWORD CALLBACK DataSizeInCom(int port);
/******************** DataSizeOutCom *********************************/
/* Get the how many data existed on the input buffer of COM port */
/*********************************************************************/
EXPORTS DWORD CALLBACK DataSizeOutCom(int port);
/******************** DataSizeOutCom *********************************/
/* Get the how many data existed on the output buffer of COM port */
/*********************************************************************/
EXPORTS WORD CALLBACK SetLineStatus(int port,int pin,int mode);
EXPORTS WORD CALLBACK GetLineStatus(int port,int pin);
EXPORTS WORD CALLBACK Send_Receive_Cmd_WithChar(unsigned char cPort, char szCmd[], char szResult[], WORD wTimeOut, WORD wChksum, WORD *wT,unsigned char cEndChar);
/*********** Send_Receive_Cmd_WithChar ***************************/
/* Send command and then receive datas */
/* with the last character */
/* It is similar to the Send_Receive_Cmd except cEndChar paramter*/
/*****************************************************************/
EXPORTS WORD CALLBACK Send_Cmd_WithChar(unsigned char cPort, char szCmd[],WORD wChksum,unsigned char cEndChar);
/******************************* Send_Cmd_WithChar *******************/
/* Send command with a character */
/* in the end of string */
/* It is similar to the Send_Cmd_WithChar except cEndChar paramter */
/*********************************************************************/
EXPORTS WORD CALLBACK Receive_Cmd_WithChar(unsigned char cPort,char szResult[], WORD wTimeOut, WORD wChksum, WORD *wT,unsigned char cEndChar);
/************************** Receive_Cmd_WithChar **********************/
/* Receive command with the last character */
/* It is similar to the Receive_Cmd_WithChar except cEndChar paramter */
/**********************************************************************/
//EXPORTS WORD CALLBACK Receive_Binary_MOS(unsigned char cPort, LPVOID szResult,WORD wLen);
/************* Receive_Binary_MOS ************/
/* This function is called by MiniOS7 only. */
/* Ensure to receive the wLen length of */
/* the szResult. */
/*********************************************/
EXPORTS WORD CALLBACK Set_FlowControl(unsigned char cPort,int DCB_Member,int mode);
/********************************** Set_FlowControl **********************************************************/
/*
/* This function is used to set DCB settings which are related to the
/* flow control (Software and Hardware).
/* Software flow control:
/* Use XOFF and XON characters to control the transmission and reception of data.
/* Hareware flow control:
/* Use control lines of the serial cable to control whether sending or receiving is enabled.
/* DCB_Member: 0 CtS (fOutxCtsFlow)
/* 1 Rts (fRtsControl)
/* 2 Dsr (fOutxDsrFlow)
/* 3 Dtr (fDtrControl)
/* 4 Tx (fOutx :Tx XON/XOFF flow control)
/* 5 Rx (fInx :Rx XON/XOFF flow control)
/* Mode:
/* CTS : 1: Enable CTS to monitored for output flow control, 0:Disable CTS flow control
/* RTS : 1: Enable RTS flow control, 0:Disable RTS flow control, 2:RTS_CONTROL_HANDSHAKE, 3:RTS_CONTROL_TOGGLE
/* DSR : 1: Enable DSR flow control / 0:Disable DSR flow control
/* DTR : 1: Enable DTR flow control, 0:Disable DTR flow control, 2:DTR_CONTROL_HANDSHAKE
/* TX : 1: Enable TX XON/XOFF, 0: Disable TX XON/XOFF
/* RX : 1: Enable RX XON/XOFF, 0: Disable RX XON/XOFF
/* [Nov,21,2003] by Sean
/**************************************************************************************************************/
EXPORTS WORD CALLBACK Get_FlowControl(unsigned char cPort,int DCB_Member);
/*********************************** Get_FlowControl ***********************************************/
/*
/* This function is used to get DCB settings which are related to the
/* flow control (Software and Hardware).
/* DCB_Member: 0 CtS (fOutxCtsFlow)
/* 1 Rts (fRtsControl)
/* 2 Dsr (fOutxDsrFlow)
/* 3 Dtr (fDtrControl)
/* 4 Tx (fOutx :Tx XON/XOFF flow control)
/* 5 Rx (fInx :Rx XON/XOFF flow control)
/* return:
/* CtS : 0:fOutxCtsFlow value is False
/* 1:fOutxCtsFlow status is True
/* Rts : fRtsControl value
/* 0:RTS_CONTROL_DISABLE
/* 1:RTS_CONTROL_ENABL
/* 2:RTS_CONTROL_HANDSHAKE
/* 3:RTS_CONTROL_TOGGLE
/* Dsr : 0:fOutxDsrFlow value is False
1:fOutxDsrFlow value is True
/* Dtr : fDtrControl value
/* 0:DTR_CONTROL_DISABLE,
/* 1:DTR_CONTROL_ENABL
/* 2:DTR_CONTROL_HANDSHAKE
/* Tx : 0: fOutx value is False.
1: fOutx value is True, XON/XOFF flow control is used during transmission
/* Rx : 0: fInx value is False
/* 1: fInx value is True, XON/XOFF flow control is used during reception
/*
/* [Nov,21,2003] by sean
/*****************************************************************************************************/
#ifndef _WIN32_WCE //Provided for WinCon
EXPORTS WORD CALLBACK Set_Break(unsigned char cPort,unsigned char cAction);
#else
EXPORTS WORD CALLBACK Set_Break(int port, int action);
#endif
/*********************************** Set_Break ***********************************************/
/*
/* This function is used to send the Break signal for the COM port.
/* [Feb,2,2004] by sean
/*****************************************************************************************************/
EXPORTS WORD CALLBACK ModbusGetCRC16(char puchMsg[],unsigned char CRC[], int DataLen);
/**************** ModbusGetCRC16 ****************/
/* */
/* Get the 16-bit CRC from the CRC table */
/* */
/* [Dec,12,2004] by Martin */
/*************************************************/
EXPORTS WORD CALLBACK Send_Receive_Binary(unsigned char cPort, char ByteCmd[],WORD in_Len,char ByteResult[], WORD out_Len, WORD wTimeOut);
/********************************Send_Receive_Binary *************************************/
/* Send binary command and receive binary data with the fixed length */
/* */
/* cPort: 1~255 */
/* ByteCmd[]: Pointer to data to be send */
/* in_Len: Maximum number of bytes to send */
/* ByteResult[]: Buffer into which received data will be stored. */
/* out_Len: Maximum number of bytes to be received */
/* wTimeOut: Timeout for receiving result string. */
/* Unit: ms (1/1000 second) */
/*****************************************************************************************/
/*#ifdef _WIN32_WCE
#endif //__UART_H__
#endif*/
Załączyłem wszystkie funkcje (chyba) poprawnie do c# i wygląda to tak:
[DllImport("uart.dll")]
private static extern UInt16 Change_Config(Byte cPort, UInt32 dwBaudrate, Byte cData, Byte cParity, Byte cStop);
[DllImport("uart.dll")]
private static extern UInt16 Get_Com_Status(Byte cPort);
[DllImport("uart.dll")]
private static extern bool Close_Com(Byte cPort);
[DllImport("uart.dll")]
private static extern UInt16 Open_Com(Byte cPort, UInt32 dwBaudrate, Byte cData, Byte cParity, Byte cStop);
[DllImport("uart.dll")]
private static extern UInt16 Send_Receive_Cmd(byte cPort, char[] szCmd, char[]
szResult, UInt16 wTimeOut, UInt16 wChksum, ref UInt16 wT);
[DllImport("uart.dll")]
private static extern UInt16 Send_Receive_UserCmd(byte cPort, char[] szCmd,
char[] szResult, UInt16 wTimeOut, UInt16 wChksum,ref UInt16 wT, char[] sUserString, int iUserLen);
[DllImport("uart.dll")]
private static extern UInt16 Send_Cmd(byte cPort, char[] szCmd, UInt16 wChksum);
[DllImport("uart.dll")]
private static extern UInt16 Receive_Cmd(byte cPort, char[] szResult, UInt16 wTimeOut, UInt16 wChksum, ref UInt16 wT);
[DllImport("uart.dll")]
private static extern UInt16 Send_Binary(byte cPort, char[] szCmd, int iLen);
[DllImport("uart.dll")]
private static extern UInt16 Receive_Binary(byte cPort, char[] szResult, UInt16 wTimeOut, UInt16 wLen,ref UInt16 wT);
[DllImport("uart.dll")]
private static extern UInt16 ReadComn(byte cPort, char[] szResult, UInt16 wmaxLen,ref UInt16 wT);
[DllImport("uart.dll")]
private static extern UInt32 DataSizeInCom(int port);
[DllImport("uart.dll")]
private static extern UInt32 DataSizeOutCom(int port);
[DllImport("uart.dll")]
private static extern UInt16 SetLineStatus(int port, int pin, int mode);
[DllImport("uart.dll")]
private static extern UInt16 GetLineStatus(int port, int pin);
[DllImport("uart.dll")]
private static extern UInt16 Send_Receive_Cmd_WithChar(byte cPort, char[] szCmd, char[] szResult, UInt16 wTimeOut, UInt16 wChksum,ref UInt16 wT, byte cEndChar);
[DllImport("uart.dll")]
private static extern UInt16 Send_Cmd_WithChar(byte cPort, char[] szCmd, UInt16 wChksum, byte cEndChar);
[DllImport("uart.dll")]
private static extern UInt16 Receive_Cmd_WithChar(byte cPort, char[] szResult, UInt16 wTimeOut, UInt16 wChksum,ref UInt16 wT, byte cEndChar);
Korzystając z gotowej 'wzorcowej' aplikacji wyniuchałem co przesyła :
Wysyła:
E0 40 40 00 28 .
W odpowiedzi dostaje:
e0 40 40 00 26 01 00 01 00 01 00 .
i to jest poprawne, przy zastosowaniu funkcji
Send_Receive_Cmd(3, polecenie, res, 100, 0,ref wsk);
dla polecenia:
char[] polecenie = new char[] { 'E', '0', '4', '0', '4', '0', '0', '0', '2', '8'};
Na snifferku mogę zobaczyć:
45 30 34 30 34 30 30 30 32 38 0D E040400028. <- czyli to samo co wczesniej.
jednak nie uzyskuję żadnej odpowiedzi
podczas zmiany czasu oczekiwania na 5000 dostaje krzaki nie zwiazane z odpowiedzia.
Więc pytanie brzmi ma ktoś pomysł jak to zrobić tak żeby odbierało poprawne dane ?