关于串口通讯的问题.

adsi 2000-06-20 05:00:00
最近我用VC6编了一个串口通信程序,在windows98下采用57600速率(硬件流控)与一外部模块通信,通信中发现串口有时会丢数据.开始以为是外部模块没给数据,遂并接
RXD和GND至另一台PC的串口,用PComm Lite 2.2软件抓串口数据.结果发现当我这里丢数据时,另一台PC的PComm Lite 2.2抓的串口数据是好的,这说明外部模块是无辜的.不服,我将从Readfile函数中得到的值立刻save为文件,同时将本机的两个串口都利用上(一个正常通信,另一个用PComm Lite 2.2抓数据),结果发现当丢数据时,用PComm Lite 2.2抓的数据也丢了,而且都在相同的点上丢.这使我不得不考虑windows是否有Bug.我将串口的错误信息存了下来,发现在丢数据前总有一个CE_OVERRUN的error,表明windows没来得及读16550芯片的接收FIFO导致数据丢失,而此时windows的接收buffer还有很大余量.

请问windows98/95的串口驱动是怎样实现的(中断or查询)?
有没有方法避免我所遇到的问题?
感谢任何建议.
...全文
286 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
a67911524 2000-09-20
  • 打赏
  • 举报
回复

我用VB5编写了一个微机与单片机进行通讯的程序,具有相同故障。我的通讯协议
为“ 2400 ,N ,8, 1” 。 将8024个字节(BYTE)通过串行口发给单片机,
单片机将收到的数据回送到计算机进行比较,如果比较无错则通讯成功。但第一次
通讯几乎都不成功,以后情况有所好转 。我在整个程序中用了两种方法,具体见下
面原程序.其中第二种方法100%成功.我已试过我能想到的方法但仍然无进展 ,
希望能对你有帮助。能进行互相交流。也希望得到帮助。

我的Email:

A67911524@public.cta.cq.cn



附原程序:

1. 进行8024个字节的通讯:

Dim a As Integer

Dim cc As Integer

Dim k, k1 As Integer

Dim b As Byte

Dim c As Integer

Dim i As Integer

Dim l As Integer

Dim cw As Integer

Dim z As Integer

Dim Signal_Lamp As Integer

Dim ss, ss1, ss2, ss3 As Single

Dim msg As String

Dim ll As Long

Dim Flh, Fll As Integer

Dim safe1 As Byte



Dim a As Integer

Dim cc As Integer

Dim k, k1 As Integer

Dim b As Byte

Dim c As Integer

Dim i As Integer

Dim l As Integer

Dim cw As Integer

Dim z As Integer

Dim Signal_Lamp As Integer

Dim ss, ss1, ss2, ss3 As Single

Dim msg As String

Dim ll As Long

Dim Flh, Fll As Integer

Dim safe1 As Byte

Dim SendDataByteArray(8192) As Byte

Dim SendConByteArray(0) As Byte

Dim RecByteArray() As Byte

Dim SendByte(0) As Byte

Dim RecStr As String



Load Form13 功能是显示进度条

Form13.Caption = "传送程序......"

Form13.Label1.Caption = "正在传送程序......"

cw = 0

ll = FileLen("c:\mcs51\data.obj")

Flh = ll \ 256

Fll = ll Mod 256

Open "c:\mcs51\data.obj" For Binary As #2

Get #2, , SendDataByteArray

Close #2 读出将送出的数据8024

k = Call() 进行联络

Form13.Pic_d1.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d2.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(1).Picture

Screen.MousePointer = 11

Form13.Show

Form13.Refresh

Form4.Comm1.PortOpen = True

Form4.Comm1.OutBufferCount = 0

Form4.Comm1.InBufferCount = 0

SendConByteArray(0) = 255

Form4.Comm1.Output = SendConByteArray

SendConByteArray(0) = CByte(Fll)

Form4.Comm1.Output = SendConByteArray

SendConByteArray(0) = CByte(Flh)

Form4.Comm1.Output = SendConByteArray 以上几句为通讯协议

Signal_Lamp = 0

For i = 0 To ll - 1

With Form13

.Pic_d1.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

.Pic_d2.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

Signal_Lamp = IIf(Signal_Lamp = 0, 1, 0)

.Pic_d1.Refresh

.Pic_d2.Refresh

response = Progress_Bar(Int((i / ll) * 100), 1) 刷新进度条

End With

SendByte(0) = SendDataByteArray(i)

RecByteArray = Form4.Comm1.Input

Form4.Comm1.Output = SendByte

Next I

以上FOR 循环将8024个字节送到缓冲区

Do

Loop While Form4.Comm1.OutBufferCount <> 0

response = Progress_Bar(100, 1)

Form13.Pic_d1.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d2.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Caption = "正在回收程序......"

Form13.Label1.Caption = "正在回收程序......"

Form13.Refresh



Form4.Comm1.InputLen = 0

Form4.Comm1.InBufferCount = 0

SendConByteArray(0) = 254

Form4.Comm1.Output = SendConByteArray

h = 0

i = 0

Do

With Form13

.Pic_d3.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

.Pic_d4.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

Signal_Lamp = IIf(Signal_Lamp = 0, 1, 0)

.Pic_d3.Refresh

.Pic_d4.Refresh

response = Progress_Bar(Int((Form4.Comm1.InBufferCount / ll) * 100), 2)

End With

If Form4.Comm1.InBufferCount = h Then

i = i + 1

If i > 1000 Then

cw = 1

Exit Do

End If

End If

If Form4.Comm1.InBufferCount <> h Then

i = 0

End If

h = Form4.Comm1.InBufferCount

Loop While Form4.Comm1.InBufferCount < ll + 2

以上为数据回收

response = Progress_Bar(100, 2)

Form13.Pic_d1.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d2.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Refresh

h = 0

If cw <> 1 Then

RecByteArray = Form4.Comm1.Input

RecStr = RecByteArray

k1 = LenB(RecStr)

If k1 <> 0 Then

Form13.Caption = "正在比较程序......"

Form13.Label1.Caption = "正在比较程序......"

Form13.Refresh

Signal_Lamp = 0

For j = 0 To ll – 1 回收程序中有两个前导0,故比较加2

If RecByteArray(j + 2) <> SendDataByteArray(j) Then

cw = 1

Exit For

End If

If j Mod 128 = 0 Then

With Form13

.Pic_d1.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(2).Picture)

.Pic_d2.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(2).Picture)

.Pic_d3.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(2).Picture)

.Pic_d4.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(2).Picture)

Signal_Lamp = IIf(Signal_Lamp = 0, 1, 0)

.Caption = "正在处理数据......."

.Label1.Caption = "正在处理数据......."

End With

Else

Form13.Label1.Caption = ""

End If

Next j

End If

End If

Screen.MousePointer = 1

Form13.Pic_d1.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d2.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(1).Picture

End If

If cw <> 1 Then

Open "c:\mcs51\data.bak" For Binary As #1

For j = 2 To 8026

Put #1, , recbytearray(j)

Next j

Close #1

End If

Screen.MousePointer = 1

If cw = 1 Then

Form13.Label1.Caption = "传送程序失败!"

Else

Form13.Label1.Caption = "传送程序成功!"

SendConByteArray(0) = &HFB

Form4.Comm1.Output = SendConByteArray

Form12.Command1.Enabled = False

End If

c = t_delay(20)

Form4.Comm1.PortOpen = False

Call mine_off

If cw = 1 Then

sponse = MsgBox("传送程序失败!", 48, "信息提示")

Else

sponse = MsgBox("传送程序成功!", 64, "信息提示")

End If

End If

Unload Form13

End Sub



2. 下面的程序为我用的另外一种通讯程序,100%成功,单上面的传送不能利用

这种方法。下面的程序只送256个字节。

Private Sub Command2_Click()

Dim sz1 As Integer

Dim a As Integer

Dim cc As Integer

Dim k, k1 As Integer

Dim b As Integer

Dim c As Integer

Dim i As Integer

'Dim s As String

Dim l As Integer

Dim cw As Integer

Dim z As Integer

Dim ss, ss1, ss2, ss3 As Single

Dim msg As String



Dim ByteChr, safe1 As Byte

Dim SendDataByteArray(255) As Byte

Dim SendConByteArray(0) As Byte

Dim RecByteArray() As Byte

Dim RecStr As String



cw = 0

Open "c:\mcs51\data1.obj" For Binary As #2

Get #2, , SendDataByteArray

Close #2

k = Call() 进行通讯联络

If k <> 1 Then

j = MsgBox("呼叫不通,系统强制退出!", 16, "通讯错误")

Exit Sub

End If

Load Form13

Form13.Caption = "正在传送参数......"

Form13.Label1.Caption = "正在传送参数......"

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d1.Picture = Form13.ImageList1.ListImages(2).Picture

Form13.Pic_d2.Picture = Form13.ImageList1.ListImages(2).Picture

Form13.Show

Form13.Refresh



Form4.Comm1.InputLen = 0

Form4.Comm1.PortOpen = True

SendConByteArray(0) = 255

Form4.Comm1.Output = SendConByteArray

SendConByteArray(0) = 0

Form4.Comm1.Output = SendConByteArray

SendConByteArray(0) = 1

Form4.Comm1.Output = SendConByteArray

Form4.Comm1.Output = SendDataByteArray

Signal_Lamp = 0

Do

With Form13

.Pic_d1.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

.Pic_d2.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

.Pic_d1.Refresh

.Pic_d2.Refresh

Signal_Lamp = IIf(Signal_Lamp = 0, 1, 0)

response = Progress_Bar(Int((256 - Form4.Comm1.OutBufferCount) / 256 * 100), 1)

End With

Loop While Form4.Comm1.OutBufferCount <> 0

SendConByteArray(0) = 254

Form4.Comm1.Output = SendConByteArray

response = Progress_Bar(100, 1)

Form13.Pic_d1.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d2.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Caption = "正在回收参数......"

Form13.Label1.Caption = "正在回收参数......"

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(2).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(2).Picture

Form13.Refresh

Form4.Comm1.InputLen = 0



h = 0

i = 0

Do

With Form13

.Pic_d3.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(1).Picture, .ImageList1.ListImages(2).Picture)

.Pic_d4.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(1).Picture, .ImageList1.ListImages(2).Picture)

Signal_Lamp = IIf(Signal_Lamp = 0, 1, 0)

.Pic_d3.Refresh

.Pic_d4.Refresh

response = Progress_Bar(Int((Form4.Comm1.InBufferCount / 258) * 100), 2)

End With

If Form4.Comm1.InBufferCount = l Then

i = i + 1

If i > 100 Then

cw = 1

Exit Do

End If

Else

i = 0

End If

h = Form4.Comm1.InBufferCount

Loop While Form4.Comm1.InBufferCount < 258

response = Progress_Bar(100, 2)

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Refresh



h = 0

If cw <> 1 Then

RecStr = Form4.Comm1.Input

k1 = LenB(RecStr)

If k1 <> 0 Then

RecByteArray = RecStr

For j = 0 To 255 回收参数中有两个前导0,故比较加2

If RecByteArray(j + 2) <> SendDataByteArray(j) Then

cw = 1

Exit For

End If

Signal_Lamp = 0

If j Mod 15 = 0 Then

With Form13

.Pic_d1.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

.Pic_d2.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

.Pic_d3.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

.Pic_d4.Picture=IIf(Signal_Lamp=0, .ImageList1.ListImages(2).Picture, .ImageList1.ListImages(1).Picture)

Signal_Lamp = IIf(Signal_Lamp = 0, 1, 0)

.Label1.Caption = "正在处理数据......."

.Caption = "正在处理数据......."

End With

Else

Form13.Label1.Caption = ""

End If

Next j

Open "c:\mcs51\data1.bak" For Binary As #1

For j = 2 To 258

Put #1, , recbytearray(j)

Next j

Close #1

Form13.Pic_d1.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d2.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d3.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Pic_d4.Picture = Form13.ImageList1.ListImages(1).Picture

Form13.Refresh

End If

End If

response = Progress_Bar(100, 2)

'Beep

If cw = 1 Then

Form4.Comm1.PortOpen = False

Form13.Label1.Caption = "传送参数失败!"

sponse = MsgBox("参数传送失败!", 48, "信息提示")

Unload Form13

Exit Sub

Else

Form13.Label1.Caption = "传送参数成功!"

SendConByteArray(0) = &HFB

Form4.Comm1.Output = SendConByteArray

End If

c = t_delay(10)

Form4.Comm1.PortOpen = False

Unload Form13

sponse = MsgBox("参数传送成功!", 48, "信息提示")

End


blue 2000-06-26
  • 打赏
  • 举报
回复
支持接近硬件编程的方法!!
Un1 2000-06-22
  • 打赏
  • 举报
回复
不要自己写readfile, 使用商业串口读写控件,比如MSComm。
adsi 2000-06-22
  • 打赏
  • 举报
回复
难道没有更好的方法吗?因为我需要高速通信.
leslielu 2000-06-22
  • 打赏
  • 举报
回复
同意降速!
fuzzy 2000-06-22
  • 打赏
  • 举报
回复
降速
fuzzy 2000-06-22
  • 打赏
  • 举报
回复
使用查询方式,用尽可能接近硬件的方法编程,不要用包装过的东西,也不要用消息机制,这些都是降低反应速度的东西。

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧