全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1351
推到 Plurk!
推到 Facebook!

yyu10 大哥:我的调用还是不行,请您再帮我看看,好吗?谢谢e

尚未結案
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-28 10:43:08 IP:219.128.xxx.xxx 未訂閱
我很详细的列出来: VC声明: short WINAPI u_OpenCom(short sComPort, short sType) 输入参数: Com_prt 结构: sComPort 串口号 ( 1 - 255 ) sType 串口的波特率 ( 1 - 9600 2 - 14400 3 - 19200 4 - 38400 5 - 56000 6 - 115200 ) 函数返回值: 00: 打开串口成功。 15: 输入参数无效。 02: 打开串口失败。 函数描述: 以指定的波特率打开串口,准备通信。 VC声明: short WINAPI u_SetDeviceMode(COMDATA &comdata, DEVICEMODE &dm) 输入参数: COMDATA结构 : ComPort 通信串口号 1 - 255 Address 设备地址 0 - 255 ReData 函数输出值 (100 Bytes) DEVICEMODE结构 : DeviceType 设备类型 (1 Byte ’0’ - 读写器 ‘1’ - 发卡器 ‘2’ - 充值机 ‘3’ - 读卡器) OutPutMode 输出模式 (1 Byte ’0’ - 韦根输出 ‘1’ - RS232/RS422输出) 函数返回值: 000: 通信成功。 242: 命令执行出错,返回错误代码。 160: 硬件返回系统错误。 其它: 通信失败,具体返回值含义请参阅返回值代码表 函数描述: 设定设备工作模式。 输出值格式(函数执行成功): “1B” VC声明: short u_PCSaleCoin(COMDATA &comdata, BCD card, MONEY &coin) 输入参数: COMDATA结构 : ComPort 通信串口号 1 - 255 Address 设备地址 0 - 255 ReData 函数输出值 (100 Bytes) BCD结构 : Value 用户卡号 (8 Bytes 十六进制字符 “00000000” - “FFFFFFFF”) MONEY结构 : Value 硬币数 (8 Bytes “00000000” - “16777215” 单位:分) 函数返回值: 000: 通信成功。 242: 命令执行出错,返回错误代码。 160: 硬件返回系统错误。 其它: 通信失败,具体返回值含义请参阅返回值代码表 函数描述: PC销售硬币。 输出值格式(函数执行成功): “33” typedef struct _COMDATA{ unsigned char ComPort; unsigned char Address; unsigned char ReData[DATA_MAX]; }COMDATA; typedef struct _DEVICEMODE{ unsigned char DeviceType; unsigned char OutPutMode; }DEVICEMODE; typedef struct _BCD{ unsigned char Value[8]; }BCD; typedef struct _MONEY{ unsigned char Value[8]; }MONEY; short WINAPI u_OpenCom(short sComPort, short sType) { return u_GSHW.Open_Com(sComPort,sType); } short CCommunications::Open_Com(short sComPort, short sType) { DWORD dwBaud = CBR_9600; m_ucComPort = 0; if(sComPort < 1 || sComPort > 255) return COM_INVALID; //Check the com port ID if(sType < 1 || sType > 6) return COM_INVALID; //Check the open mode //------------------------------------------- //Set the baud if(sType == 1) dwBaud = CBR_9600; if(sType == 2) dwBaud = CBR_14400; if(sType == 3) dwBaud = CBR_19200; if(sType == 4) dwBaud = CBR_38400; if(sType == 5) dwBaud = CBR_56000; if(sType == 6) dwBaud = CBR_115200; //------------------------------------------- m_ucComPort = (unsigned char)sComPort; if(m_hComPort[m_ucComPort] != INVALID_HANDLE_VALUE) {//The com port is opened if(sType != m_ucType[m_ucComPort]) {//The baud doesn't match the sType if (!SetCommState( m_hComPort[m_ucComPort], dwBaud)) {//Open the com port again DWORD dwError = GetLastError(); COMSTAT ComStat; ::ClearCommError( m_hComPort[m_ucComPort],&dwError,&ComStat ); CloseHandle( m_hComPort[m_ucComPort] ); m_hComPort[m_ucComPort] = INVALID_HANDLE_VALUE; m_ucType[m_ucComPort] = 0; m_ucComPort = 0; return COM_OPENFAIL; } else m_ucType[m_ucComPort] = (unsigned char)sType; } return COM_OPENED; } //-------------------------------------------------------- //Open the com port CHAR szPort[15] = {0}; sprintf(szPort,"\\\\.\\COM%d", m_ucComPort); m_hComPort[m_ucComPort] = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,NULL,NULL); if(m_hComPort[m_ucComPort] == INVALID_HANDLE_VALUE ) { m_ucType[m_ucComPort] = 0; m_ucComPort = 0; return COM_OPENFAIL; } //---------------------------------------------------------- SetCommMask(m_hComPort[m_ucComPort],EV_RXCHAR|EV_TXEMPTY); //设置通讯波特率 if ((!SetCommState( m_hComPort[m_ucComPort], dwBaud)) || (!SetupComm( m_hComPort[m_ucComPort], 1024,1024)) || (!SetCommTimeouts( m_hComPort[m_ucComPort] ))) { DWORD dwError = GetLastError(); COMSTAT ComStat; ::ClearCommError( m_hComPort[m_ucComPort],&dwError,&ComStat ); CloseHandle( m_hComPort[m_ucComPort] ); m_hComPort[m_ucComPort] = INVALID_HANDLE_VALUE; m_ucType[m_ucComPort] = 0; m_ucComPort = 0; return COM_OPENFAIL; } PurgeComm( m_hComPort[m_ucComPort], PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR ); m_ucType[m_ucComPort] = (unsigned char)sType; return COM_OPENED; } short WINAPI u_SetDeviceMode(COMDATA &comdata, DEVICEMODE &dm) { return u_GSHW.HW_SetDeviceMode(comdata,dm); } short CGSHW::HW_SetDeviceMode(COMDATA &comdata, DEVICEMODE &dm) { unsigned char T_Dat[DATA_MAX] = {0}; unsigned char nCmd = SETDEVICEMODE; unsigned short nNum = 1; short sRt = 0,sRt1 = 0; memset(&comdata.ReData,0,DATA_MAX); m_ucComPort = comdata.ComPort; m_ucID = comdata.Address; if(dm.DeviceType - '0' < 0 || dm.DeviceType - '0' > 3) return INVALID; if(dm.OutPutMode - '0' < 0 || dm.OutPutMode - '0' > 1) return INVALID; T_Dat[0] = dm.DeviceType - '0' (dm.OutPutMode - '0') * 0x10; sRt = ProcessData(&nCmd,T_Dat,&nNum); if(sRt != SUCCESS) return sRt; if((sRt1 = ProcessCmd(comdata.ReData,T_Dat,nCmd)) != SUCCESS) return sRt1; return sRt; } short WINAPI u_PCSaleCoin(COMDATA &comdata, BCD &card, MONEY &coin) { return u_GSHW.HW_PCSaleCoin(comdata,card,coin); } short CGSCtrl::HW_PCSaleCoin(COMDATA &comdata, BCD &card, MONEY &coin) { unsigned char T_Dat[DATA_MAX] = {0}; unsigned char nCmd = PCSALECOIN; unsigned short nNum = 7; short sRt = 0,sRt1 = 0; unsigned char ucMoney[9] = {0}; memset(&comdata.ReData,0,DATA_MAX); m_ucComPort = comdata.ComPort; m_ucID = comdata.Address; //Check the parameter if(!IsHexNum(card.Value,8)) return INVALID; if(!IsDecNum(coin.Value,8)) return INVALID; //Proccess the card NO. for(int i = 0;i < 4;i ) { T_Dat[i] = CHRTOBCD(card.Value,i * 2); } //Coin memcpy(ucMoney,coin.Value,8); if(atoi((char *)ucMoney) < 0 || atoi((char *)ucMoney) > 0xffffff) return INVALID; T_Dat[4] = (atoi((char *)ucMoney) & 0xff0000) >> 16; T_Dat[5] = (atoi((char *)ucMoney) & 0xff00) >> 8; T_Dat[6] = atoi((char *)ucMoney) & 0xff; sRt = ProcessData(&nCmd,T_Dat,&nNum); if(sRt != SUCCESS) return sRt; if((sRt1 = ProcessCmd(comdata.ReData,T_Dat,nCmd)) != SUCCESS) return sRt1; return sRt; } ///////yyu10 大哥 ,上面的就是 C 的东西,做成为 DLL 了的: 我在 delphi 中这样调用它: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Edit1: TEdit; Edit2: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses unit2; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin u_OpenCom(1,1); //打开串口 end; procedure TForm1.Button2Click(Sender: TObject);//设置工作模式 var com:COMDATA; dm:DEVICEMODE; begin com.comport:=1; com.Address:=0; dm.DeviceType:=49; dm.OutPutMode:=48; showmessage(inttostr(u_SetDeviceMode(com,dm))); end; procedure TForm1.Button3Click(Sender: TObject);//售币 var com:COMDATA; card:BCD; coin:MONEY; i:integer; s1:string; s2:string; begin com.comport:=1; com.Address:=0; s1:=edit1.Text; s2:=edit2.Text; move(s1[1],card.Value[0],length(s1)); move(s2[1],coin.value[0],length(s2)); showmessage(inttostr(u_PCSaleCoin(com,card,coin))); end; end. unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; const DATA_MAX = 100; type COMDATA=packed record comport:byte; Address:byte; ReData:array[0..DATA_MAX-1] of byte; end; type BCD=packed record Value:array[0..7] of byte; end; type MONEY=packed record Value:array[0..7] of byte; end; type DEVICEMODE=packed record DeviceType:byte; OutPutMode:byte; end; function u_OpenCom(sComPort, sType:smallint):smallint;stdcall; function u_SetDeviceMode(var comdata:COMDATA; var dm:DEVICEMODE):smallint;stdcall; function u_PCSaleCoin(var comdata:COMDATA; card:BCD; var coin:MONEY):smallint;stdcall; implementation function u_OpenCom(sComPort, sType:smallint):smallint;external 'GSCtrl.dll'; function u_SetDeviceMode(var comdata:COMDATA; var dm:DEVICEMODE):smallint;external 'GSCtrl.dll'; function u_PCSaleCoin(var comdata:COMDATA; card:BCD; var coin:MONEY):smallint;external 'GSCtrl.dll'; end. yyu10 大哥,现在问题是: 单击 button1 ,打开串口 ,是成功的。 单击 button2 ,则 show 出 1 。1 对应的是:函数参数无效。 yyu10 大哥,我真的找不出错在哪里了? 请您帮我调式一下,好吗? 谢谢 yyu10 大哥!
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-28 15:50:22 IP:219.128.xxx.xxx 未訂閱
yyu10 大哥 大哥, 原先是我参数设置错了,我把改过来, procedure TForm1.Button2Click(Sender: TObject);//设置工作模式 var com:COMDATA; dm:DEVICEMODE; begin com.comport:=1; com.Address:=1; dm.DeviceType:=50; dm.OutPutMode:=49; showmessage(inttostr(u_SetDeviceMode(com,dm))); end; 可是 show 出的是:3 。3 对应的是:硬件无响应 我又错在那里呢? yyu10 大哥,一般有那些情况导致 硬件无响应 呢? 谢谢!
yyu10
中階會員


發表:9
回覆:99
積分:96
註冊:2005-02-18

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-28 17:15:51 IP:220.244.xxx.xxx 未訂閱
lsh998,    感谢你的信任. 但是端口通讯并不是我的专长, 对于你的问题我无处下手, 请原谅.    建议你将这个问题发到'週邊設備程式設計討論區(Delphi)'更加合适一些.    请精通端口通讯的版主们, 高手们帮lsh998看看这个问题, 谢谢.    试试
引言: com.comport:=1; com.Address:=0; dm.DeviceType:=49; // char '1' dm.OutPutMode:=48; // char '0' showmessage(inttostr(u_SetDeviceMode(com,dm)));
改成
com.comport := 1;
com.Address := 0;

dm.DeviceType := 1;   // 数字 1
dm.OutPutMode := 0;   // 数字 0    showmessage(inttostr(u_SetDeviceMode(com,dm)));
_________________________ Programming is a passion 發表人 - yyu10 於 2005/03/28 17:59:37
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-03-28 20:45:04 IP:218.14.xxx.xxx 未訂閱
现在我参数改了,那个参数错误没有了 com.comport:=1; com.Address:=1; dm.DeviceType:=50; dm.OutPutMode:=49; showmessage(inttostr(u_SetDeviceMode(com,dm))); 现在出现的错误: 硬件无响应 yyu10,您真的使我很敬佩!
系統時間:2024-11-28 0:49:52
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!