高分请教:串口不能发送数据! 串口参数有没有问题?还有你的硬件是否OK? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没看到有串口初始化的代码--------------------- 没看到有串口初始化的代码--------------------- 我有一个串口测试小程序,你可以检查一下硬件故障 初始化应该没错,因为读线程是正确的程序如下:function TMainFrm.OpenCommPort(Port: string): integer;begin //open comm port; try hCom := CreateFile(Pchar(Port), GENERIC_READ or GENERIC_WRITE, 0,//独占式访问 nil, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); if hCom=INVALID_HANDLE_VALUE then begin showmessage('打开串口' + Port + '错误!'); abort; end; except Showmessage('打开串口失败!'); abort; end; //Result := True; if not SetupParameter() then begin CloseHandle(hCom); abort; end; MainFrm.statusBar1.Panels[0].Text := '端口:' + SetupFrm.PortLst.Text; EscapeCommFunction(hCom, SETDTR); EscapeCommFunction(hCom, SETRTS); Rt := TReadThread.Create(false); Rt.hCom := hCom; Result := hCom;end;设置:function TMainFrm.SetupParameter(): boolean;var cto : TCommTimeOuts; dcb : TDCB; dcbFlag : integer; tmpStr : string;begin //Setup Comm Configration try SetCommMask(hCom, EV_RXCHAR); SetupComm(hCom, 4096, 4096); PurgeComm(hCom, PURGE_RXABORT or PURGE_RXCLEAR or PURGE_TXABORT or PURGE_TXCLEAR); cto.ReadIntervalTimeout := $ffffffff; cto.ReadTotalTimeoutConstant := StrToInt(SetupFrm.TimeOutEdt.Text); StatusBar1.Panels[5].text := '超时:' + SetupFrm.TimeOutEdt.Text + '毫秒'; cto.ReadTotalTimeoutMultiplier := 0; cto.WriteTotalTimeoutConstant := 0; cto.WriteTotalTimeoutMultiplier := 2*CBR_9600 div CBR_19200; SetCommTimeOuts(hCom, cto); except Beep; Showmessage('串口超时设置失败!'); abort; end; try dcb.DCBlength := sizeof(TDCB); GetCommState(hCom,dcb); dcb.BaudRate := strToInt(SetupFrm.CBRLst.Text); StatusBar1.Panels[1].Text := '波特率:' + SetupFrm.CBRLst.Text; dcb.ByteSize := strToInt(SetupFrm.ByteSizeLst.Text); StatusBar1.Panels[2].Text := '比特长:' + SetupFrm.ByteSizeLst.Text + '位'; case SetupFrm.StopBitsLst.ItemIndex of 0 : dcb.StopBits := ONESTOPBIT; 1 : dcb.StopBits := ONE5STOPBITS; 2 : dcb.StopBits := TWOSTOPBITS; end; //dcb.StopBits := strToInt(SetupFrm.StopBitsLst.Text); StatusBar1.Panels[3].Text := '停止位:' + SetupFrm.StopBitsLst.Text + '位'; case SetupFrm.CheckLst.ItemIndex of 0: begin dcb.Parity := NOPARITY; tmpStr := '无校验'; end; 1: begin dcb.Parity := ODDPARITY; tmpStr := '奇校验'; end; 2: begin dcb.Parity := EVENPARITY; tmpStr := '偶校验'; end; 3: begin dcb.Parity := MARKPARITY; tmpStr := '标记校验'; end; end; StatusBar1.Panels[4].Text := '校验:' + tmpStr; dcb.XonChar := Chr($11); //Ctrl_Q dcb.XoffChar := Chr($13); //Ctrl_S dcb.ErrorChar := Chr(0); dcb.XonLim := 100; dcb.XoffLim := 100; dcbFlag := 1; //Binary must be True dcbFlag := dcbFlag or 2; //Parity Check True dcbFlag := dcbFlag or 4; //FOutxCtsFlowCtrl dcbFlag := dcbFlag or 8; //FOutxDsrFlowCtrl dcbFlag := dcbFlag or $20; //DtrHandShake dcbFlag := dcbFlag or $1000; //RtsEnable // dcbFlag := dcbFlag or $800; //Ignore NULL Char dcb.Flags := dcbFlag; SetCommState(hCom, dcb); Connected := True; except Beep; Showmessage('串口参数设置失败!'); abort; end; Result := True;end; 将一个表中的数据插入到另一个表中的字符处理问题 记录修改历史记录 怎样用delphi操作网页 急!建立控件数组的疑惑! 重返CSDN散分! 进销存软件都包括什么呀?? TNMUUProcessor是base64编码的控件?? 套接字是什么啊?帮帮忙啊。。大哥大姐们。。 EXCEL问题求助。 delphi help中关于string list的一个简单例子,我执行后找不到log文件,而且。。。。 DELPHI 的密码 我在uses里添加了DsgnIntf后,编译时出错说没有DsgnIntf这个文件,请问是怎么回事?
---------------------
---------------------
程序如下:
function TMainFrm.OpenCommPort(Port: string): integer;
begin
//open comm port;
try
hCom := CreateFile(Pchar(Port),
GENERIC_READ or GENERIC_WRITE,
0,//独占式访问
nil,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if hCom=INVALID_HANDLE_VALUE then begin
showmessage('打开串口' + Port + '错误!');
abort;
end;
except
Showmessage('打开串口失败!');
abort;
end;
//Result := True; if not SetupParameter() then begin
CloseHandle(hCom);
abort;
end;
MainFrm.statusBar1.Panels[0].Text := '端口:' + SetupFrm.PortLst.Text;
EscapeCommFunction(hCom, SETDTR);
EscapeCommFunction(hCom, SETRTS); Rt := TReadThread.Create(false);
Rt.hCom := hCom; Result := hCom;
end;设置:
function TMainFrm.SetupParameter(): boolean;
var
cto : TCommTimeOuts;
dcb : TDCB;
dcbFlag : integer;
tmpStr : string;
begin
//Setup Comm Configration
try
SetCommMask(hCom, EV_RXCHAR);
SetupComm(hCom, 4096, 4096);
PurgeComm(hCom, PURGE_RXABORT or PURGE_RXCLEAR or PURGE_TXABORT or PURGE_TXCLEAR);
cto.ReadIntervalTimeout := $ffffffff;
cto.ReadTotalTimeoutConstant := StrToInt(SetupFrm.TimeOutEdt.Text);
StatusBar1.Panels[5].text := '超时:' + SetupFrm.TimeOutEdt.Text + '毫秒';
cto.ReadTotalTimeoutMultiplier := 0;
cto.WriteTotalTimeoutConstant := 0;
cto.WriteTotalTimeoutMultiplier := 2*CBR_9600 div CBR_19200;
SetCommTimeOuts(hCom, cto);
except
Beep;
Showmessage('串口超时设置失败!');
abort;
end;
try
dcb.DCBlength := sizeof(TDCB);
GetCommState(hCom,dcb);
dcb.BaudRate := strToInt(SetupFrm.CBRLst.Text);
StatusBar1.Panels[1].Text := '波特率:' + SetupFrm.CBRLst.Text;
dcb.ByteSize := strToInt(SetupFrm.ByteSizeLst.Text);
StatusBar1.Panels[2].Text := '比特长:' + SetupFrm.ByteSizeLst.Text + '位';
case SetupFrm.StopBitsLst.ItemIndex of
0 : dcb.StopBits := ONESTOPBIT;
1 : dcb.StopBits := ONE5STOPBITS;
2 : dcb.StopBits := TWOSTOPBITS;
end;
//dcb.StopBits := strToInt(SetupFrm.StopBitsLst.Text);
StatusBar1.Panels[3].Text := '停止位:' + SetupFrm.StopBitsLst.Text + '位';
case SetupFrm.CheckLst.ItemIndex of
0: begin
dcb.Parity := NOPARITY;
tmpStr := '无校验';
end;
1: begin
dcb.Parity := ODDPARITY;
tmpStr := '奇校验';
end;
2: begin
dcb.Parity := EVENPARITY;
tmpStr := '偶校验';
end;
3: begin
dcb.Parity := MARKPARITY;
tmpStr := '标记校验';
end;
end;
StatusBar1.Panels[4].Text := '校验:' + tmpStr;
dcb.XonChar := Chr($11); //Ctrl_Q
dcb.XoffChar := Chr($13); //Ctrl_S
dcb.ErrorChar := Chr(0);
dcb.XonLim := 100;
dcb.XoffLim := 100;
dcbFlag := 1; //Binary must be True
dcbFlag := dcbFlag or 2; //Parity Check True
dcbFlag := dcbFlag or 4; //FOutxCtsFlowCtrl
dcbFlag := dcbFlag or 8; //FOutxDsrFlowCtrl
dcbFlag := dcbFlag or $20; //DtrHandShake
dcbFlag := dcbFlag or $1000; //RtsEnable
// dcbFlag := dcbFlag or $800; //Ignore NULL Char
dcb.Flags := dcbFlag;
SetCommState(hCom, dcb);
Connected := True;
except
Beep;
Showmessage('串口参数设置失败!');
abort;
end;
Result := True;
end;