终端通讯开发问题
首次连接串口成功,写入AT指令无返回值请问是什么问题,如果用超级终端连接一次后再用我的程序进行连接一切正常
下面是源代码:
InitCom(Setport,Convert.ToInt32(SetBaudRate));//打开并初始化串口
ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMI\r"));
string response = Encoding.ASCII.GetString(ss_port.Read(128));//首次连接返回0字节
Write方法为:
public void Write(byte[] WriteBytes) {
if (hComm!=INVALID_HANDLE_VALUE) {
System.Threading.Thread.Sleep(2000);
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);
}
else {
throw(new ApplicationException("串口未打开!"));
}
Read方法为:
public byte[] Read(int NumBytes) {
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes];
if (hComm!=INVALID_HANDLE_VALUE) {
System.Threading.Thread.Sleep(2000);
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead=0;
ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes,OutBytes,BytesRead);
}
else {
throw(new ApplicationException("串口未打开!"));
}
return OutBytes;
}
--------------------编程问答-------------------- 是不是通讯模块工作不正常,你用超级终端发送AT指令,看看返回内容正确吗
--------------------编程问答-------------------- 返回正常的,但如果直接用我程序连就连不上,用超级终端先连一次,用我的程序可以正常连上 --------------------编程问答-------------------- 接收一下你的程序启动时发出的指令内容,看看是不是有问题 --------------------编程问答-------------------- 好像应该不是指令发出的问题,我只要使用超级终端连接一次,我的程序能够正常执行,但重新启动电脑又出现这个问题,我在想是不是串口操作问题,我的串口连接类如下:请高手帮我看看谢谢~~:
public void Open()
{
DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
// 打开串口 OPEN THE COMM PORT.
hComm = CreateFile(PortNum ,GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);
// 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
if(hComm == INVALID_HANDLE_VALUE)
{
throw(new ApplicationException("非法操作,不能打开串口!"));
}
// 设置通信超时时间 SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm,ref ctoCommPort);
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm,ref ctoCommPort);
// 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate=BaudRate;
dcbCommPort.flags=0;
//dcb.fBinary=1;
dcbCommPort.flags|=1;
if (Parity>0)
{
//dcb.fParity=1
dcbCommPort.flags|=2;
}
dcbCommPort.Parity=Parity;
dcbCommPort.ByteSize=ByteSize;
dcbCommPort.StopBits=StopBits;
if (!SetCommState(hComm, ref dcbCommPort))
{
//uint ErrorNum=GetLastError();
throw(new ApplicationException("非法操作,不能打开串口!"));
}
//unremark to see if setting took correctly
//DCB dcbCommPort2 = new DCB();
//GetCommState(hComm, ref dcbCommPort2);
Opened = true;
}
public void Close() {
if (hComm!=INVALID_HANDLE_VALUE) {
CloseHandle(hComm);
}
}
public byte[] Read(int NumBytes) {
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes];
if (hComm!=INVALID_HANDLE_VALUE) {
System.Threading.Thread.Sleep(2000);
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead=0;
ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes,OutBytes,BytesRead);
}
else {
throw(new ApplicationException("串口未打开!"));
}
return OutBytes;
}
} --------------------编程问答-------------------- 使用西门子M35开发短信发送程序,遇到问题,
打开串口正常,可以写入AT指令,但无返回值?
使用超级终端可以正常返回值,使用官方配置软件也可以,请问问题出在哪?
如果使用超级终端连接一次后我的程序有正常返回值,请遇到同样问题的朋友帮忙解决,以下是我的代码
ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMI\r")); //获取手机品牌,写入AT指令
string response = Encoding.ASCII.GetString(ss_port.Read(128));//获取返回值,执行后无返回值,长度为0?
问题仍未解决 --------------------编程问答-------------------- 5年了,没找出问题在哪?
补充:.NET技术 , C#