100分请教串口通讯问题,很急的!(100)

S

sjm

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我的源代码,但运行后什么也没显示!请各位帮看看:using System;using System.Collections;using System.Diagnostics;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using Microsoft.VisualBasic;using System.Windows.Forms;namespace WRZS{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort(); public delegate void myDelegate(); private void Form1_Load(object sender, EventArgs e) { //获取串行端口总数名称 //for (int i = 0; i<=(new Microsoft.VisualBasic.Devices.Computer().Ports.SerialPortNames.Count -1); i++) //{ // cbbCOMPorts.Items.Add((new Microsoft.VisualBasic.Devices.Computer()).Ports.SerialPortNames); //} } private void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { //接收串口事件后触发事件 txtDataReceived.Invoke(new myDelegate(updateTextBox), new object[] { }); } private void updateTextBox() { txtDataReceived.Font = new Font("Grramond", 12.0F, FontStyle.Bold); //txtDataReceived.SelectedColor = Color.Red; txtDataReceived.AppendText(serialPort.ReadExisting()); txtDataReceived.ScrollToCaret(); } private void btnConnect_Click(object sender, EventArgs e) { if (serialPort.IsOpen) { serialPort.Close(); } try { //设置串口相关参数并打开 serialPort.PortName = "COM1"; //cbbCOMPorts.Text; serialPort.BaudRate = 4800; serialPort.Parity = System.IO.Ports.Parity.None; serialPort.DataBits = 8; serialPort.StopBits = System.IO.Ports.StopBits.One; serialPort.DiscardNull = true; serialPort.DtrEnable = false; serialPort.Encoding = System.Text.Encoding.ASCII; serialPort.RtsEnable = false; serialPort.Open(); lblMessage.Text = cbbCOMPorts.Text + "connected."; btnConnect.Enabled = false; btnDisconnect.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnDisconnect_Click(object sender, EventArgs e) { try { //关闭串口 serialPort.Close(); lblMessage.Text = serialPort.PortName + "disconnected."; btnConnect.Enabled = true; btnDisconnect.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }}说明:代码基本上是从书上参考来的。1. 获取串行端口总数名称那一段有问题只好先屏蔽掉;2. 其中 private void DataReceived() 这一段事件代码我是直接输入的,但是在其中加断点运行后根本就停不下来,说明这段事件代码形同虚设未起作用,不知该怎么办?把该机的说明记录下来给各位看看:1. 通讯接口采用15芯RS232插头座,其引脚定义见图(注:即15芯的6、7、8,其中6:RXD, 7:TXD, 8:信号地)。2. 所有数据均为ASCII码,每组数据由10位组成,第1位为起始位,第10位为停止位,中间8位为数据位,通讯方式为: 连续方式(tF=0): 所传送的数据位仪表显示的当前称重(毛重或净重),每帧数据由12组数据组成。格式如下:第x字节 内容及注释 1 02(XON) 开始 2 +或- 符号位、 3 称量数据 高位 : 称量数据 : : 称量数据 : 8 称量数据 低位 9 小数点位数 从右到左(0 - 4) 10 异或校验 高四位 11 异或校验 低四位 12 03(XOFF) 结束这是一台称重显示控制器,需要通过串行通讯接口与一般的计算机连接,以便读取数据即可.请帮帮我,谢谢了!
 
说明一下:我用的工具是 vs2005 c# 开发。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
591
import
I
I
回复
0
查看
607
import
I
I
回复
0
查看
702
import
I
顶部