问题描述
- vs 代码 详细 求讲解! 犹豫字数限制只有上半部分,谢谢!
-
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using INIFILE;
using System.Text.RegularExpressions;
using ThoughtWorks.QRCode.Codec;
using System.Drawing.Printing;namespace SerialPortConnection
{
public partial class Form1 : Form
{
Dictionary deviceTypes = new Dictionary();
LonzClass.DataDeal.Serial.SerialData serialData = new LonzClass.DataDeal.Serial.SerialData(0xaa, 0, 16, 3);
LonzClass.Windows.myCom MyCom = new LonzClass.Windows.myCom();private delegate void deMessboxShow(string txt);//定义类型 private void MessboxShow(string txt) { Invoke(new deMessboxShow(ShowMesBox),txt);//调用 } private void ShowMesBox(string txt) { MessageBox.Show(txt);//常用参数 } ushort devicegenre { get; set; }//设备类型 uint deviceBH { get; set; }//设备编号 public Form1() { InitializeComponent(); serialData.ReceiveSerialBytes += serialData_ReceiveSerialBytes; MyCom.DataReceived += MyCom_DataReceived; nUDDeviceID.Enabled = false; btnSend.Enabled = false; btnPrint.Enabled = false; #region test //DeviceModel deviceInfo=new DeviceModel (); //deviceInfo.ISUSED=0; //deviceInfo.DEVICECODE=2; //deviceInfo.DEVICETYPE=1033; //Dal.GetDevice(deviceInfo); #endregion } void MyCom_DataReceived(object bytes) { txtReceive.AppendText("收到数据(" + ((byte[])bytes).Length + "):"); //接收数据控件打印 foreach (var item in (byte[])bytes) //数据的处理 { txtReceive.AppendText(item.ToString("X2") + " "); } txtReceive.AppendText("rn"); txtReceive.ScrollToCaret(); txtReceive.ScrollToCaret(); serialData.ReceiveData((byte[])bytes); } //加载 private void Form1_Load(object sender, EventArgs e) { INIFILE.Profile.LoadProfile();//加载所有 // 预置波特率 switch (Profile.G_BAUDRATE) { case "300": cbBaudRate.SelectedIndex = 0; break; case "600": cbBaudRate.SelectedIndex = 1; break; case "1200": cbBaudRate.SelectedIndex = 2; break; case "2400": cbBaudRate.SelectedIndex = 3; break; case "4800": cbBaudRate.SelectedIndex = 4; break; case "9600": cbBaudRate.SelectedIndex = 5; break; case "19200": cbBaudRate.SelectedIndex = 6; break; case "38400": cbBaudRate.SelectedIndex = 7; break; case "115200": cbBaudRate.SelectedIndex = 8; break; default: { MessageBox.Show("波特率预置参数错误。"); return; } } if (!string.IsNullOrEmpty(Profile.G_DEVICETYPE)) { string[] deviceTypeStr = Profile.G_DEVICETYPE.Split(','); foreach (var item in deviceTypeStr) { var s = item.Split(':'); deviceTypes.Add(s[0], s[1]); } deviceTypeComboBox.Items.AddRange(deviceTypes.Keys.ToArray()); deviceTypeComboBox.SelectedIndex = 0; } //预置波特率 switch (Profile.G_DATABITS) { case "4": cbDataBits.SelectedIndex = 0; break; case "5": cbDataBits.SelectedIndex = 1; break; case "6": cbDataBits.SelectedIndex = 2; break; case "7": cbDataBits.SelectedIndex = 3; break; case "8": cbDataBits.SelectedIndex = 4; break; default: { MessageBox.Show("数据位预置参数错误。"); return; } } //预置停止位 switch (Profile.G_STOP) { case "1": cbStop.SelectedIndex = 0; break; case "1.5": cbStop.SelectedIndex = 1; break; case "2": cbStop.SelectedIndex = 2; break; default: { MessageBox.Show("停止位预置参数错误。"); return; } } //预置校验位 switch (Profile.G_PARITY) { case "NONE": cbParity.SelectedIndex = 0; break; case "ODD": cbParity.SelectedIndex = 1; break; case "EVEN": cbParity.SelectedIndex = 2; break; default: { MessageBox.Show("校验位预置参数错误。"); return; } } //检查是否含有串口 string[] str = SerialPort.GetPortNames(); if (str == null) { MessageBox.Show("本机没有串口!", "Error"); return; } //添加串口项目 foreach (string s in System.IO.Ports.SerialPort.GetPortNames()) {//获取有多少个COM口 cbSerial.Items.Add(s); } //串口设置默认选择项 cbSerial.SelectedIndex = 0; //note:获得COM9口,但别忘修改 Control.CheckForIllegalCrossThreadCalls = false; //这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性) }
解决方案
代码太长,又没有看到是什么问题
只能飘过...
解决方案二:
就是串口通讯,具体为什么要这么通讯,取决于你的下位机怎么定义的。
时间: 2024-10-29 11:54:13