核心功能
- 需要使用C#在Tim或QQ的聊天窗口自动发送消息
先使用命令行实现最核心的功能:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TimSendMessage
{
class Program
{
[DllImport("user32.dll")]
static extern IntPtr FindWindow(String ClassName, String WindwosName);
[DllImport("user32.dll")]
static extern void keybd_event(byte vk, byte vsacn, int flag, int wram);
[DllImport("user32.dll")]
static extern void PostMessage(IntPtr hwnd, uint msg, int w, string l);
[DllImport("user32.dll")]
static extern void PostMessage(IntPtr hwnd, uint msg, int w, int l);
[STAThread]
static void Main(string[] args)
{
//Console.WriteLine("发送QQ的名字");
var name = "我的Android手机";
Console.WriteLine("要发送的字符");
var t = Console.ReadLine();
//Console.WriteLine("要发送的次数");
//var Count = int.Parse(Console.ReadLine());
var Count = 0;
while (Count > -1)
{
Thread.Sleep(TimeSpan.FromMilliseconds(50));
Clipboard.SetText(t);
SendKey(name, t);
Count--;
Console.WriteLine("测试次数" + Count);
}
}
static void SendKey(string name, string l)
{
var win = FindWindow(null, name);
keybd_event(0x01, 0, 0, 0);//激活TIM
PostMessage(win, 0x0302, 0, 0);
PostMessage(win, 0x0101, new Random().Next(65,128),0);//发送字符 //下面是发送回车
PostMessage(win, 0x0100, 13, 0);
PostMessage(win, 0x0101, 13, 0);
keybd_event(0x11, 0, 0x0002, 0);
}
}
}
思想
该应用程序想要实现的功能是作为一个中转站
- 将单片机通过串口发送的信息通过Tim转发到手机中,或发送给其他好友
- 因此该应用程序还需要具备串口助手的基本功能
- 串口助手(上位机)的设计可参考
https://www.cnblogs.com/yangfengwu/p/12382103.html
- 结合窗口设计
- 具体功能实现见代码
```csharp
/* Copyright (c) 2020 - ~, WANGXI
- APE CHAT WAY APPLICATION
- Used in C Sharp
- Gitee https://gitee.com/WangXi_Chn/
- Note https://www.yuque.com/wangxi_chn
- Change Logs:
- Date Author Notes Mail
- 2020-12-10 WangXi first version WangXi_Chn@foxmail.com */
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO.Ports;
namespace ApeChatWay { public partial class ApeChatWay : Form { [DllImport(“user32.dll”)] static extern IntPtr FindWindow(String ClassName, String WindwosName);
[DllImport("user32.dll")]
static extern void keybd_event(byte vk, byte vsacn, int flag, int wram);
[DllImport("user32.dll")]
static extern void PostMessage(IntPtr hwnd, uint msg, int w, string l);
[DllImport("user32.dll")]
static extern void PostMessage(IntPtr hwnd, uint msg, int w, int l);
public ApeChatWay()
{
InitializeComponent();
//更新可用串口列表
PortUpDate();
//默认设置波特率参数
comboBox_BaudRate.Text = "115200";
}
/// <summary>
/// 发送功能核心
/// </summary>
/// <param name="message">要发送的字符串参数</param>
public void chatsend(string message)
{
//在消息上添加系统时间戳
message += "\r\n";
message += DateTime.Now.ToString();
//获取TIM窗口
string name = textBox_receiver.Text;
Thread.Sleep(TimeSpan.FromMilliseconds(50));
Clipboard.SetText(message);
var win = FindWindow(null, name);
keybd_event(0x01, 0, 0, 0);
PostMessage(win, 0x0302, 0, 0);
PostMessage(win, 0x0100, 13, 0);
PostMessage(win, 0x0101, 13, 0);
keybd_event(0x11, 0, 0x0002, 0);
//显示在命令窗中
textBox_show.Text = textBox_show.Text + message + "\r\n\r\n";
textBox_input.Clear();
}
/// <summary>
/// 发送按键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_send_Click(object sender, EventArgs e)
{
//从输入窗口获取文字内容
string message = textBox_input.Text;
message = "[Upper] " + message;
//调用发送核心
chatsend(message);
}
/// <summary>
/// 实现文本框滚动条自动滚动,文本框的事件触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox_show_TextChanged(object sender, EventArgs e)
{
textBox_show.SelectionStart = textBox_show.Text.Length;
textBox_show.ScrollToCaret();
}
/// <summary>
/// 串口功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_switch_Click(object sender, EventArgs e)
{
if (!sp1.IsOpen)
{
try
{
string serialName = comboBox_Com.SelectedItem.ToString();
sp1.PortName = serialName;
string strBaudRate = comboBox_BaudRate.Text;
string strDataBits = "8";
int iBaudRate = Convert.ToInt32(strBaudRate);
int iDataBits = Convert.ToInt32(strDataBits);
sp1.BaudRate = iBaudRate;
sp1.DataBits = iDataBits;
sp1.StopBits = StopBits.One;
sp1.Parity = Parity.None;
if (sp1.IsOpen)
{
sp1.Close();
}
comboBox_Com.Enabled = false;
comboBox_BaudRate.Enabled = false;
sp1.Open();
button_switch.Text = "关闭串口";
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error");
return;
}
}
else
{
comboBox_Com.Enabled = true;
comboBox_BaudRate.Enabled = true;
sp1.Close();
button_switch.Text = "打开串口";
}
}
private void button_refresh_Click(object sender, EventArgs e)
{
PortUpDate();
}
private void PortUpDate()
{
sp1.DataReceived += new SerialDataReceivedEventHandler(sp1_DataReceived);
comboBox_Com.Items.Clear();
string[] serial = SerialPort.GetPortNames();
foreach (var s in serial)
{
comboBox_Com.Items.Add(s);
}
if (comboBox_Com.Items.Count != 0)
comboBox_Com.Text = serial[0];
}
private void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
int len = sp1.BytesToRead;//获取可以读取的字节数
byte[] buff = new byte[len];//创建缓存数据数组
sp1.Read(buff, 0, len);//把数据读取到buff数组
string str = Encoding.Default.GetString(buff);//Byte值根据ASCII码表转为 String
Invoke((new Action(() => //C# 3.0以后代替委托的新方法
{
//textBox_show.AppendText(str);//对话框追加显示数据
//textBox_show.AppendText("\r\n");
if (str != "")
{
str = "[Serial] " + str;
chatsend(str);
}
})));
}
catch (Exception ex)
{
System.Media.SystemSounds.Beep.Play();
MessageBox.Show(ex.Message, "error2", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button_help_Click(object sender, EventArgs e)
{
Form2 helpForm = new Form2();
helpForm.Show(this);
}
}
}
/** (C) COPYRIGHT 2020 WANGXI **END OF FILE**/
可应用环境
- 将串口传来的信息通过QQ或TIM转发出去
-
Ubuntu仿真结束提醒
在Ubuntu虚拟机通过bash运行仿真程序
- 运行结束后通过minicom与Windows的该上位机通信
- https://www.yuque.com/wangxi_chn/qaxke0/ssgzri
- 该上位机转发消息至QQ/Tim远程提醒仿真已结束