我正在为加速度计实现SPI驱动程序。 SPI部分已完成,但我无法从用户空间读取值。
static char charDevMessage[CD_BUFFER_SIZE] = { 0 };
...
static ssize_t char_dev_read(struct file *filep, char *buffer, size_t len, loff_t *position)
{
int error_count = 0;
struct xyz_values xyz;
size_t size_requested;
xyz = adxl345_get_xyz();
memset(charDevMessage, 0, CD_BUFFER_SIZE);
sprintf(charDevMessage, "%d,%d,%d", xyz.x, xyz.y, xyz.z);
printk(KERN_INFO "MOB: %s, requested size: %d\n", charDevMessage, len);
if (len >= CD_BUFFER_SIZE)
{
size_requested = CD_BUFFER_SIZE;
}
else
{
size_requested = len;
}
error_count = copy_to_user(buffer, charDevMessage, size_requested);
if (error_count == 0)
{
printk(KERN_INFO "MOB: Sent %d characters to the user\n", size_requested);
return (size_requested = 0);
}
else
{
printk(KERN_INFO "MOB: Failed to send %d characters to the user\n", error_count);
return -EFAULT;
}
}
安装驱动程序时已创建节点。但是,当我尝试cat
或尝试通过python读取时,它返回一个空字符串。
dmesg
表示已成功发送到用户空间:
[ 3094.495972] MOB: SPI Character device has been opened 1 time(s) [ 3094.506075] MOB: -349,-512,511 , requested size:49 [ 3094.514487] MOB: Sent 256 characters to the user [ 3094.522646] MOB: Character device successfully closed [ 3120.658568] MOB: SPI Character device has been opened 2 time(s) [ 3120.668609] MOB: 0,0,0 , requested size:48 [ 3120.676392] MOB: Sent 256 characters to the user [ 3120.684740] MOB: Character device successfully closed
我做错了什么?
答案 0 :(得分:2)
你误解了public class TestRing extends Activity
{
boolean isLastObj = false;
Button add;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.testring);
add = (Button) findViewById(R.id.btnAdd);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(!isLastObj)
{
for (int i = 1; i < addObject.size(); i++ )
{
Object newboject = addObject.get(i);
newboject.setTitle("stuff");
newboject.isLast = false;
newboject.add(object);
if(newboject.getIsLast)
{
isLastObj = true; // this will prevent furtheradding of objects
}
}
}
}
});
}
函数的概念:
读者(用户空间)只看到 if (!File.Exists(path))
{
System.IO.File.WriteAllText(path, rxHeader + textBox8.Text);
}
else
{
System.IO.File.AppendAllText(path, textBox8.Text);
MessageBox.Show("Export of " + comboBox5.Text + " table is complete!");
textBox8.Clear();
}
返回的值,并将其解释为已读取的字节数。
对于 .read
(在您的情况下为.read
)的最后一个参数,其解释完全取决于驱动程序的作者。当文件打开时,由.read
指向的值由内核核心初始化为0。在那之后,内核本身永远不会修改它。
如果您想从头开始阅读,可以忽略position
参数:
position
或者,在语义上更好,您可以增加值position
,因此它将反映总读取的字节数。但否则忽略它:
return size_requested;