-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (148 loc) · 5.47 KB
/
Program.cs
File metadata and controls
167 lines (148 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Media;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace vsGUI
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
//全局变量
public static class GlobalValue
{
public static int globalFormNumber; //记录当前窗口ID
public static bool globalLanguage = false; //false = EN, true = CN.
public static string[] globalPortName; //全部的端口名称(数组)
public static string globalPortNameSet; //选择的端口名称
public static bool globalIsPortOpening = false; //记录端口状态
public static string globalTemperature = "0"; //实时温度
public static string globalCocktailCode; //鸡尾酒代码,用于和arduino通信
public static bool globalBartending = false; //记录调制状态
public static bool globalDone = false; //记录完成状态
}
//全局方法
public class GlobalMethod
{
static SerialPort serial_ = new SerialPort(); //声明串口
//获取全部串口,并写入全局变量
public static void GetPort()
{
string[] portName = SerialPort.GetPortNames();
GlobalValue.globalPortName = portName;
}
//初始化串口
public static void LoadPort()
{
serial_.Close();
//如果未指定串口,则默认选择第一个串口
if (GlobalValue.globalPortNameSet == null || GlobalValue.globalPortNameSet.Length == 0){
GlobalValue.globalPortNameSet = GlobalValue.globalPortName.First();}
serial_.PortName = GlobalValue.globalPortNameSet;
serial_.Open();
serial_.BaudRate = 9600;
serial_.DataBits = 8;
serial_.StopBits = StopBits.One;
serial_.Parity = Parity.None;
serial_.ReadTimeout = 100;
serial_.WriteTimeout = -1;
serial_.ReceivedBytesThreshold = 1;
serial_.DataReceived += new SerialDataReceivedEventHandler(PortReceived); //注册事件,如果串口收到消息,则调用PortReceived()方法
serial_.Close();
}
//开启串口
public static void OpenPort()
{
serial_.Close();
serial_.Open();
GlobalValue.globalIsPortOpening = true; //更新状态
if (GlobalValue.globalLanguage)
{
MessageBox.Show(serial_.PortName + " 已打开!");
}
else
{
MessageBox.Show(serial_.PortName + " opening!");
}
}
//关闭串口
public static void ClosePort()
{
serial_.Close();
GlobalValue.globalIsPortOpening = false; //更新状态
if (GlobalValue.globalLanguage)
{
MessageBox.Show(serial_.PortName + " 已关闭!");
}
else
{
MessageBox.Show(serial_.PortName + " closed!");
}
}
//串口发送
public static void PortSend(string send)
{
serial_.Write(send);
}
//串口接收
private static void PortReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort serial = (SerialPort)sender;
string s = serial.ReadLine();
//进行字符串处理
if (s.Length != 0 && s != null)
{
string ss = s.Substring(0, 1); //从第0位截取,截取1长度,等于截取首字母
if (ss == "t")
{
GlobalValue.globalTemperature = s.Substring(1);
}
else if (ss == "y")
{
//checkP = true
GlobalValue.globalBartending = true;
PortSend(GlobalValue.globalCocktailCode);
}
else if (ss == "n")
{
//checkP = false
if (GlobalValue.globalLanguage)
{
MessageBox.Show("请放置杯子后重试!");
}
else
{
MessageBox.Show("Please place the cup and try again!");
}
}
else if (ss == "d")
{
//done
SoundPlayer soundPlayer = new SoundPlayer(); //播放音频
soundPlayer.SoundLocation = @"D:\vsProject\vsGUI\Resources\levelup.wav";
soundPlayer.Load();
soundPlayer.Play();
GlobalValue.globalBartending = false; //更新状态
GlobalValue.globalDone = true; //更新状态
if (GlobalValue.globalLanguage)
{
MessageBox.Show("完成!");
}
else
{
MessageBox.Show("Done!");
}
}
}
}
}
}