
Kód: Vybrat vše
public void loop()
{
if((serialPort1.IsOpen) && (serialPort1.ReadBufferSize != 0))
{
label2.Text = Convert.ToString(serialPort1.ReadByte() + "°C");
}
}
Kód: Vybrat vše
public void loop()
{
if((serialPort1.IsOpen) && (serialPort1.ReadBufferSize != 0))
{
label2.Text = Convert.ToString(serialPort1.ReadByte() + "°C");
}
}
Kód: Vybrat vše
temp = serialPort1.ReadByte();
label2.Text = Convert.ToString((temp) + ("°C"));
Kód: Vybrat vše
while ((serialPort1.IsOpen) && (serialPort1.ReadBufferSize != 0)) {
/* Prichozi byte precteme a ulozime do pomocne promenne */
//1. receivedByte = serialPort1.ReadByte();
//2. rtb_received.Text += (char) serialPort1.ReadByte();
//3. Nyní spravne reseni:
AddASCII( (char) serialPort1.ReadByte() );
}
Kód: Vybrat vše
/*
* Created by SharpDevelop.
* User: Marek
* Date: 24.7.2011
* Time: 14:54
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace TempTerminal
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class First_form : Form
{
public First_form()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
int temp;
public void PortLoad()
{
int rate_int;
try {
string name = (System.IO.File.ReadAllText(@"C:\port_name.txt"));
serialPort1.PortName = name;
string rate = (System.IO.File.ReadAllText(@"C:\baud_rate.txt"));
rate_int = Convert.ToInt32(rate);
serialPort1.BaudRate = rate_int;
serialPort1.Open();
if ( serialPort1.IsOpen )
{
label3.Text = "Připojeno";
}
}
catch ( Exception )
{
label3.Text = ("Nelze otevřít port");
}
}
void Button1Click(object sender, EventArgs e)
{
Form1 Settings = new Form1();
Settings.Show();
serialPort1.Close();
}
void MainFormLoad(object sender, EventArgs e)
{
PortLoad();
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Visible = false;
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
loop();
}
public void loop()
{
while((serialPort1.IsOpen) && (serialPort1.ReadBufferSize != 0))
{
temp = serialPort1.ReadByte();
label2.Text = Convert.ToString((temp) + ("°C"));
ShowText((Convert.ToString((temp)) + ("°")), new Font("Helvetica", 8), Color.FromName ("White"));
}
}
public void ShowText(string text, Font font, Color col)
{
Brush brush = new SolidBrush(col);
Bitmap bitmap = new Bitmap(16, 16);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString(text, font, brush, 0, 0);
IntPtr hIcon = bitmap.GetHicon();
Icon icon = Icon.FromHandle(hIcon);
notifyIcon1.Icon = icon;
}
void NotifyIcon1MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
void NotifyIcon1MouseClick(object sender, MouseEventArgs e)
{
notifyIcon1.ShowBalloonTip(500, "TempSensor", "Teplota je: " + Convert.ToString(temp + "°C"), ToolTipIcon.Info);
}
void OtevřítToolStripMenuItemClick(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
void ZavřítToolStripMenuItemClick(object sender, EventArgs e)
{
this.Close();
}
}
}
Kód: Vybrat vše
void SerialPort1DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int receivedByte;
try {
/* Cteme z prijimaciho bufferu dokud neni prazdny */
while ((serialPort1.IsOpen) && (serialPort1.ReadBufferSize != 0)) {
/* Prichozi byte precteme a ulozime do pomocne promenne */
//1. receivedByte = serialPort1.ReadByte();
//2. rtb_received.Text += (char) serialPort1.ReadByte();
//3. Nyní spravne reseni:
AddASCII( (char) serialPort1.ReadByte() );
}
}
catch ( Exception ) {
/* Do status baru vypiseme chybove hlaseni */
label3.Text = "Nelze číst";
}
}
private void AddASCII(char receivedChar)
{
if (label2.InvokeRequired) {
label2.Invoke(new Action<char>(AddASCII), receivedChar);
}
else {
label2.Text = (Convert.ToString(receivedChar));
}
}
Zpět na “Programování a tvorba webu”
Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 4 hosti