原来Mock可以这样子用。
1. 接口ISerialPort
public interface ISerialPort
{
string PortName { get; }
int BaudRate { get; }
int DataBits { get; }
Parity Party { get; }
StopBits StopBits { get; }
int BytesToRead { get; }
bool IsOpen { get; }
void Open();
void Close();
string ReadLine();
int ReadByte();
int Read(ref byte[] buffer, int offset, int count);
string ReadTo(string p);
void Write(byte[] data, int offset, int count);
}2. 用Rhino Mock测试串口
[TestMethod()]
public void ReadAlarmTest()
{
MockRepository mock = new MockRepository();
ISerialPort serialPort = mock.StrictMock<ISerialPort>();
VSerialPort target = new VSerialPort(serialPort);
string excepted ="1,1,1,";
string actual = string.Empty;
StringBuilder sb = new StringBuilder();
target.OnReceiveData += (alarmNo) =>{ sb.Append(alarmNo);sb.Append(',');};
using (mock.Record())
{
byte[] data = GetAlarmData();
byte[] outData = new byte[data.Length];
serialPort.Expect(p => p.ReadByte()).Return(0x21);
serialPort.Expect(p => p.Write(new byte[] {1,0x64,0x0D}, 0, 3));
serialPort.Expect(p => p.BytesToRead).Return(data.Length);
serialPort.Expect(p => p.Read(ref outData, 0, outData.Length)).OutRef(data).Return(data.Length);
serialPort.Expect(p => p.BytesToRead).Return(0);
}
target.ReadAlarm();
actual = sb.ToString();
Assert.AreEqual(excepted, actual);
}
static byte[] GetAlarmData()
{
List<byte> data = new List<byte>();
data.Add(1);
data.AddRange(Encoding.ASCII.GetBytes("ALARM MON #01=001 \r\n$\r\n"));
data.Add(1);
data.AddRange(Encoding.ASCII.GetBytes("ALARM MON #01=001 \r\n$\r\n"));
data.Add(1);
data.AddRange(Encoding.ASCII.GetBytes("ALARM MON #01=001 \r\n$\r\n"));
data.Add(1);
data.AddRange(Encoding.ASCII.GetBytes("$\r"));
data.Add(1);
data.AddRange(Encoding.ASCII.GetBytes("000\r\n"));
return data.ToArray();
}AdventureWorksLT2008Entities db = new AdventureWorksLT2008Entities();
var querys = db.Products.Where(p => p.ProductID % 55 == 0).Select(p => new { Id = p.ProductID, LPrice = p.ListPrice, Name = p.Name });
foreach (var item in querys)
{
Console.WriteLine(item.Id+". "+item.LPrice+" "+item.Name);
}
Console.WriteLine(((System.Data.Objects.ObjectQuery)querys).ToTraceString());
// 715. 49.9900 Long-Sleeve Logo Jersey, L
// 770. 782.9900 Road-650 Black, 52
// 825. 327.2150 HL Mountain Rear Wheel
// 880. 54.9900 Hydration Pack - 70 oz.
// 935. 40.4900 LL Mountain Pedal
// 990. 539.9900 Mountain-500 Black, 42
// SELECT
// [Extent1].[ProductID] AS [ProductID],
// [Extent1].[ListPrice] AS [ListPrice],
// [Extent1].[Name] AS [Name]
// FROM [SalesLT].[Product] AS [Extent1]
// WHERE 0 = ([Extent1].[ProductID] % 55)代码如下:
public static Image BytesToImage(this byte[] bytes)
{ Image image = null;if (bytes != null)
{using (MemoryStream sm=new MemoryStream(bytes))
{image = Image.FromStream(sm);
}
}
return image;}
用.NET 自带的 ImageAnimator类来播放GIF图像
1. 注册动画事件
ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
2. 在OnFrameChanged事件中调用this.Invalidate();
3. 在OnPaint中绘当前帧
ImageAnimator.UpdateFrames(adimatedImage);
e.Graphics.DrawImage(adinatedImage, new Point(0,0));
sapi.dll: %windir%\System32\Speech\Common

using SpeechLib;
SpVoice voice = new SpVoiceClass();
voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
1. Solution:WebApp, WpfAff
2. WebApp add : Silverlight-enabled WCF Service
[OperationContract]
public IList<Customer> GetCustomers()
{
List<Customer> results = new List<Customer>();
results.Add(new Customer {LastName="Brown",FisrtName="Pete" });
results.Add(new Customer { LastName = "Stagner", FisrtName = "Joe" });
results.Add(new Customer { LastName = "Liberty", FisrtName = "Jesse" });
results.Add(new Customer { LastName = "Galloway", FisrtName = "Jon`" });
return results;
}
3. WpfApp:
private void CallService_Click(object sender, RoutedEventArgs e)
{
var client = new Service.CustomerServiceClient();
client.GetCustomersCompleted += (s, ev) =>
{
CustomerList.ItemsSource = ev.Result;
};
client.GetCustomersAsync();
}---------------------------------------------------
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LastName}" />
<TextBlock Text=", " />
<TextBlock Text="{Binding FisrtName}"/>
</StackPanel>
</DataTemplate>
Solution:

